%% Write a MATLAB program to integrate the equation f(x) = dx/(1+x^2) within
%% the limits of 0 to 6 by taking 6 subintervals by using simpsons 1/3 rule.
% Given
x0 = 0;
xn = 6;
n = 6;
h = (xn-x0)/n;
y0 = 1/(1+x0^2);
yn = 1/(1+xn^2);
% sum of integral terms is given by y0+yn+4*(y1+y3+....yn-1)+2*(y2+y4+...Yn-2)
s = 0;
for i = 1:2:n-1
s = s + 4*1/(1+(x0+i*h)^2);
end
for i = 2:2:n-1
s = s + 2*1/(1+(x0+i*h)^2);
end
s = s+y0+yn;
% integral is given by h/3(sum of integral terms)
integral = (h/3)*s;
fprintf (' The integral by simpsons 1/3 rule is = %2.5f \n',integral);
%% the limits of 0 to 6 by taking 6 subintervals by using simpsons 1/3 rule.
% Given
x0 = 0;
xn = 6;
n = 6;
h = (xn-x0)/n;
y0 = 1/(1+x0^2);
yn = 1/(1+xn^2);
% sum of integral terms is given by y0+yn+4*(y1+y3+....yn-1)+2*(y2+y4+...Yn-2)
s = 0;
for i = 1:2:n-1
s = s + 4*1/(1+(x0+i*h)^2);
end
for i = 2:2:n-1
s = s + 2*1/(1+(x0+i*h)^2);
end
s = s+y0+yn;
% integral is given by h/3(sum of integral terms)
integral = (h/3)*s;
fprintf (' The integral by simpsons 1/3 rule is = %2.5f \n',integral);
No comments:
Post a Comment