Matlab Function for Composite Simpson

ver_mathstats
Messages
258
Reaction score
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:
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.
 
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));
 
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
 

Similar threads

Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K