MATLAB Simpson's Rule in MATLAB: Single vs Composite

  • Thread starter Thread starter tironel
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around the implementation of the Composite 1/3 Simpson's Rule in MATLAB and the comparison with a single application of Simpson's Rule. The user is uncertain about how to program a single application of Simpson's Rule and questions whether setting n=1 in their composite function would yield the same result. The provided MATLAB code calculates the integral using the composite method, and there is a suggestion that the user may have copied the code from a source without fully understanding it. This raises concerns about the user's grasp of the underlying concepts necessary to answer their own questions regarding the functionality of the code.
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.
 

Similar threads

Replies
3
Views
17K
Replies
2
Views
3K
Replies
3
Views
1K
Replies
8
Views
2K
Replies
4
Views
3K
Replies
4
Views
1K
Replies
3
Views
2K
Back
Top