Composite Simpsons 1/3 matlab code

In summary: An example of this would be if you had a function that calculated the area of a circle and you wanted to use the function to generate the coordinates for a new circle.
  • #1
haz
3
0
hey guys,
So I'm working on a MATLAB function that uses simpsons 1/3 rule to find an integral.
This is what I have done so far, but I'm not 100% confident.. I seem to get double when I use greater segments.
If anyone would be able to have a look at give me some tips that would be very much appreciated.

function [ I ] = simpsons3( func,a,b,n )
%Finds estimate integral from a to b of function using simpson 1/3 rule

% % INPUTS
% % func = function
% % a = lower limit
% % b = upper limit
% % n=number of segments used for integration
% % OUTPUTS
% % I = integral estimate

h = (b-a)/(n-1);
sumE = 0;
for i = a+2*h:h:b-2*h
sumE = sumE + func(i);
end
sumO = 0;
for i = a+h:h:b-h
sumO = sumO + func(i);
end
I = (h/3)*(func(a)+4*sumE+2*sumO+func(b));
 
Physics news on Phys.org
  • #2
haz said:
So I'm working on a MATLAB function that uses simpsons 1/3 rule to find an integral.
This is what I have done so far, but I'm not 100% confident.. I seem to get double when I use greater segments.
Always good to test with a function for which you know the analytical result and compare.

haz said:
Code:
function [ I ] = simpsons3( func,a,b,n )
%Finds estimate integral from a to b of function using simpson 1/3 rule

% % INPUTS
% % func = function
% % a = lower limit
% % b = upper limit
% % n=number of segments used for integration
% % OUTPUTS
% % I = integral estimate

h = (b-a)/(n-1);
Minor point, but if n is the number of intervals, then
Code:
h = (b-a)/n;

haz said:
Code:
sumE = 0;
for i = a+2*h:h:b-2*h
sumE = sumE + func(i);
end
sumO = 0;
for i = a+h:h:b-h
sumO = sumO + func(i);
end
First, it is a bad idea to use real values for a for loop counter. Use integers and calculate the independent variable from h at each step.

Second, you are doubly counting the interior points. Each sum should add points that are 2h apart, not h.

Third, such an implementation of the Simpson rule only works if the number of intervals is even (or n odd according to your definition of h), otherwise it will return a result with a greater error.
 
  • #3
Thankyou very much for your reply.
I actually was able to make these adjustments before reading your reply but you have given me confidence so thankyou!
I am however confused as to why it is a bad idea to set up my for loop counters as I have done. Would you be able to tell be me why it is bad and give me examples as too when this would fail.
 
  • #4
Because of rounding errors, you can end up with unexpected behaviour at the termination of a for loop. Say you have a for loop that counts to 15, but when you add the step sizes h, you end up with the last values being 15.00000001. This being > 15, the loop will exit without the last point being counted.
 
  • #5
%simpson 1/3 rule formula
disp(I);

% % The simpson 1/3 rule requires an even number of segments, so I added an
% % if statement to check if n is even. If it's not, I use the trapezoidal
% % rule for the last segment.
if mod(n,2) == 0
I = I + (h/2)*(func(b-h)+func(b));
disp(I);
else
I = I + (h/3)*(func(b-h)+4*func(b-h)+func(b));
disp(I);
end

Dear colleague,

Your code seems to be on the right track! However, I have a few suggestions that might help improve your function.

Firstly, I noticed that you are using a for loop to calculate the sum of even and odd segments separately. This can be simplified by using vectorization. You can create a vector for the even and odd segments, and then use the sum function to calculate their sums. This will not only make your code more efficient, but also easier to read and debug.

Secondly, I would recommend adding some error handling to your code. For example, checking if the input values for a, b, and n are valid (e.g. n must be even, a and b must be within the function's domain), and displaying an error message if they are not. This will make your function more robust and user-friendly.

Lastly, I would suggest using more descriptive variable names. This will make your code easier to understand and maintain in the long run.

Overall, it looks like you have a good understanding of the Simpson's 1/3 rule and how to implement it in MATLAB. Keep up the good work and don't hesitate to reach out for further assistance. Best of luck with your project!
 

1. What is "Composite Simpsons 1/3 matlab code" and what is it used for?

"Composite Simpsons 1/3 matlab code" refers to a specific algorithm used in the programming language MATLAB to perform numerical integration. It is commonly used to approximate the area under a curve by dividing it into smaller segments and using the Simpson's 1/3 rule on each segment.

2. How does the Composite Simpsons 1/3 matlab code work?

The algorithm works by first dividing the interval of integration into smaller segments. Then, it uses the Simpson's 1/3 rule to approximate the area under the curve on each segment. Finally, it sums up all the approximations to get the overall area under the curve.

3. What are the advantages of using the Composite Simpsons 1/3 matlab code?

One advantage is that it provides a more accurate approximation of the area under a curve compared to other numerical integration methods. It also allows for the integration of complex functions that cannot be easily integrated analytically.

4. Are there any limitations to using the Composite Simpsons 1/3 matlab code?

One limitation is that it may not provide accurate results for highly oscillatory functions. Additionally, it may require a larger number of function evaluations compared to other numerical integration methods.

5. How can I use the Composite Simpsons 1/3 matlab code in my own projects?

You can use the code by first defining the function you want to integrate in MATLAB. Then, input the necessary parameters such as the interval of integration and the number of segments. Finally, run the code to get an approximation of the area under the curve.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
121
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top