Write a MATLAB program to solve the following integral using
Simpson’s 1/3rd Rule.
I=∫ p0 (ax3+bx2+cx+d) dx=?
Assign suitable values of a, b, c and d.
Also assume suitable values of p and step size h.
Compare your program result with original one.
a = 1;Â b = 2;Â c = 3;Â d = 4;
p = 1;
% integrate a*x^3 + b*x^2 + c*x + d from 0 to p using Simpson 1/3 rule
f = @(x) a*x.^3 + b*x.^2 + c*x + d;
n = 2; % interval numbers
h = p /n;
s = 0;
for i=1:2:n
   s = s + f(h*(i-1)) + 4 * f(h*i) + f(h*(i+1));
end
s = s * h /3;
% original integral is a/4*x^4 + b/3*x^3 + c/2*x^2 + d*x
i_real = a/4*p.^4 + b/3*p.^3 + c/2*p.^2 + d*p;
fprintf('Calculated Integral value: %f\n', s);
fprintf('Real Integral value is:Â Â Â %f\n', i_real);
Comments
Leave a comment