Tuesday, May 2, 2017

Simpson's 3/8 rules code implementation in MatLab


Problem of Simpson's 3/8 Rules :


Find the integral of the function, f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5); where lower limit = 0, upper limit = .8 and exact integral is = 1.640533


Solution of Simpson 3/8 rules in Matlab:

----------------------------------------
f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5);

a = input('Enter lower limit : ');
b = input('Enter upper limit limit : ');

x0 = a;
x1 = (b-a)/3;
x2 = 2 * x1;
x3 = b;

fx0 = feval(f, x0);
fx1 = feval(f, x1);
fx2 = feval(f, x2);
fx3 = feval(f, x3);

I = (b - a) * ((fx0 + 3*(fx1 + fx2) + fx3)/8);

exact_integral = input('Enter Exact Integral : ');
E = abs((exact_integral - I)/exact_integral) * 100;
fprintf('\nIntegral = %f\n', I);
fprintf('\nTrue Error = %f\n', E);
----------------------------------------


No comments:

Post a Comment