Simpson's Rule in MATLAB: Single vs Composite

  • Context: MATLAB 
  • Thread starter Thread starter tironel
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 10K views
tironel
Messages
10
Reaction score
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
 
Physics news on Phys.org
You completed the assignment by copying the MATLAB code in the Simpson's Rule wiki virtually line by line?

I would guess that's why you don't know the answer to this question. Had you written it, you might have understood it better.