- #1
tironel
- 11
- 0
Ok I just completed a homework assignment by programming a Composite 1/3 Simpson's rule in matlab. However, I am asked to compare my results from a single application of Simpaon's rule with my results. I do not know how to program a single application of Simpson's rule in MATLAB or does MATLAB have a built in function? However I will attach my code below and if I set n=1 then it seems to me that it would be the same result as a single application of Simpson's rule, am I correct?
Code:
function F =P_simpson13(func,a,b,n)
%a=initial value
%b=end value
%n=number of double intervals of size 2h
n = 2 * n;
h = (b - a) / n;
S = func(a); % S is continuing sum
for i = 1 : 2 : n-1 %all odd steps
x = a + h .* i;
S = S + 4 * func(x);
end
for i = 2 : 2 : n-2 %all even
x = a + h .* i;
S = S + 2 * func(x);
end
S = S + func(b);
F = h * S / 3;
F;
end