Simpson's Rule in MATLAB: Single vs Composite

  • Context: MATLAB 
  • Thread starter Thread starter tironel
  • Start date Start date
  • Tags Tags
    Matlab
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
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
18K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K