Matlab Function for Composite Simpson

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function designed to implement the composite Simpson's rule for numerical integration. Participants are addressing issues related to errors encountered in the code, particularly within the while loop, and are seeking improvements or corrections to the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their MATLAB function and expresses confusion about errors occurring in the while loop, indicating that the function does not produce expected results when inputting data.
  • Another participant questions the use of the "sum" function in the assignment for S, suggesting that it may not be appropriate since sum() is typically used for arrays.
  • A participant points out that the function is supposed to return the value of I, but the integral is calculated in S, which may lead to confusion regarding the function's output.
  • Concerns are raised about not adhering to the specified number of points for integration in the implementation, which could affect the accuracy of the results.
  • One participant acknowledges a typo in their code that caused issues and mentions that they have fixed it, leading to the function working correctly afterward.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the code, particularly regarding the use of the "sum" function and the output variable. While some issues have been resolved, others remain contested, indicating that the discussion is not fully settled.

Contextual Notes

There are unresolved questions about the proper implementation of the composite Simpson's rule, particularly regarding the handling of arrays and the expected output of the function. The discussion highlights the need for clarity on the integration points used in the calculation.

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
2K
  • · 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
1K