Matlab Function for Composite Simpson

In summary, the function calculates the integral of a function over a specified range of values. However, there are errors in the code that need to be fixed.
  • #1
ver_mathstats
260
21
Homework Statement
We need to evaluate an integral sin(1/x) where b = 1 and a = 0.1 using composite Simpson in Matlab for n = 2, 4, 8... until the absolute error is within 10^-4, then we are required to apply the adaptive Simpson rule and compare the two. The program needs to output the number and error.
Relevant Equations
composite simpson rule
Matlab:
function I=main_simpson(a,b,tol)
f = @(x) sin(1./x);
SO = 0;
N = 10;
S = 1;
while (abs(S-SO)>tol)
    SO = S;
    h = (b-a)/(2*N);
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
    N = 2*N;
end
end
<Moderator's note: please use CODE tags when posting code.>

I keep experiencing errors when trying to use my composite Simpson code, could anyone tell me how I can improve this? The error is always coming from the while loop but I'm really unsure why and I do not know how to proceed or nothing happens when I input data into my function so I am really confused. Any help is appreciated.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
ver_mathstats said:
S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
Why is "sum" there on the right side of the assignment? Matlab isn't my strong suit, but I believe that sum() is intended to be used on arrays.

The above should be written as S = (h/3) * (f(xi)+4*f(xi1)+f(xi2));, I believe.
ver_mathstats said:
I keep experiencing errors when trying to use my composite Simpson code, could anyone tell me how I can improve this? The error is always coming from the while loop but I'm really unsure why and I do not know how to proceed or nothing happens when I input data into my function so I am really confused.
What sort of errors? If you're getting error messages that possibly include the line number where the error occurred, that's useful information that should be included.
 
  • #3
You have
Matlab:
function I=main_simpson(a,b,tol)
So the function will return the value of I, which is not what you calculate. You calculate the integral into S.

Note also that you do not follow what the question asks with regards to the number of points for integration at each attempt.

Mark44 said:
Why is "sum" there on the right side of the assignment? Matlab isn't my strong suit, but I believe that sum() is intended to be used on arrays.
In the code snippet below, the variable i is set as an array of integers, so xi, xi1 and xi2 will be arrays, as well as f(xi) and so on.
Matlab:
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
 
  • #4
DrClaude said:
You have
Matlab:
function I=main_simpson(a,b,tol)
So the function will return the value of I, which is not what you calculate. You calculate the integral into S.

Note also that you do not follow what the question asks with regards to the number of points for integration at each attempt.In the code snippet below, the variable i is set as an array of integers, so xi, xi1 and xi2 will be arrays, as well as f(xi) and so on.
Matlab:
    i = 0:N-1;
    xi = a+2*i*h;
    xi1 = a+2*(i+0.5)*h;
    xi2 = a+2*(i+1)*h;
    S = (h/3)*sum(f(xi)+4*f(xi1)+f(xi2));
Yes I realized it was such a silly typo causing issues which is my mistake, I fixed it and now it works
 

What is a Matlab function for Composite Simpson?

A Matlab function for Composite Simpson is a numerical integration method used to approximate the area under a curve by dividing the interval into smaller subintervals and applying Simpson's rule to each subinterval.

How does the Composite Simpson method work?

The Composite Simpson method works by dividing the interval into an even number of subintervals and using a weighted average of three points within each subinterval to approximate the area under the curve. This method is more accurate than the simpler Trapezoidal rule.

What are the advantages of using Composite Simpson over other numerical integration methods?

One advantage of using Composite Simpson is that it can provide a more accurate approximation of the area under a curve compared to other methods, such as the Trapezoidal rule. It also requires fewer function evaluations, making it more efficient for certain types of functions.

What are the limitations of using Composite Simpson?

One limitation of using Composite Simpson is that it can only be used for functions that are smooth and continuous. It may also not provide accurate results for functions with sharp changes or discontinuities. Additionally, the method may become less accurate when the number of subintervals is too large.

How can I use the Matlab function for Composite Simpson in my research or projects?

The Matlab function for Composite Simpson can be used in a variety of research and project applications, such as computing the area under a curve in physics, engineering, or economics. It can also be used to approximate integrals in mathematical models and simulations. Simply input the function and interval of interest into the function and it will return an approximation of the area under the curve.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
827
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
  • Engineering and Comp Sci Homework Help
Replies
6
Views
862
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
946
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top