Simpson's Rule in MATLAB: Single vs Composite

  • MATLAB
  • Thread starter tironel
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses completing a homework assignment by programming a Composite 1/3 Simpson's rule in MATLAB and the individual is unsure about how to compare their results with a single application of Simpson's rule. They provide their code and question if setting n=1 would result in the same output as a single application of Simpson's rule. They also mention that they may have a better understanding if they had written the code themselves.
  • #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
 
Physics news on Phys.org
  • #2
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.
 

Related to Simpson's Rule in MATLAB: Single vs Composite

1. What is Simpson's Rule and how does it work in MATLAB?

Simpson's Rule is a numerical integration method used to approximate the area under a curve. In MATLAB, it works by dividing the area into smaller segments and using a polynomial interpolation to find the area for each segment. The sum of these areas gives an approximate value for the total area under the curve.

2. What is the difference between Single and Composite Simpson's Rule in MATLAB?

Single Simpson's Rule uses a single segment to approximate the area under a curve, while Composite Simpson's Rule uses multiple segments. In other words, Single Simpson's Rule is a special case of Composite Simpson's Rule with only one segment.

3. How do I implement Simpson's Rule in MATLAB?

In MATLAB, Simpson's Rule can be implemented using the simpson function. This function takes in the function to be integrated, the lower and upper limits of integration, and the number of segments (for Composite Simpson's Rule) as inputs and outputs the approximate area under the curve.

4. When should I use Single Simpson's Rule vs Composite Simpson's Rule in MATLAB?

Single Simpson's Rule is best used when the function being integrated is relatively simple and smooth, while Composite Simpson's Rule is best used for more complex and/or oscillating functions. Generally, the more segments used, the more accurate the approximation will be.

5. Are there any limitations to using Simpson's Rule in MATLAB?

Simpson's Rule is an approximation method and will not give an exact solution. The accuracy of the approximation also depends on the number of segments used - using too few segments may result in a significant error. Additionally, Simpson's Rule may not work well for functions with sharp corners or discontinuities.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
17K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
965
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top