Matlab Function for Composite Simpson

Click For Summary
SUMMARY

The forum discussion centers on a MATLAB function for implementing the Composite Simpson's Rule for numerical integration. The user encountered errors primarily within the while loop, specifically related to the calculation of the integral. A key issue identified was the incorrect use of the sum function, which should aggregate the results of the function evaluations rather than being applied directly to the expression. After correcting the syntax and ensuring the function returns the correct integral value, the user successfully resolved the errors.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with numerical integration techniques, specifically Simpson's Rule
  • Knowledge of function handles in MATLAB
  • Basic array manipulation in MATLAB
NEXT STEPS
  • Review MATLAB function handles and their applications
  • Study the implementation of numerical integration methods in MATLAB
  • Learn about error handling and debugging techniques in MATLAB
  • Explore advanced numerical methods for integration, such as adaptive quadrature
USEFUL FOR

Mathematics students, MATLAB programmers, and engineers looking to implement numerical integration techniques effectively.

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:
Physics news on Phys.org
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
1K
  • · 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
913