Composite Simpsons 1/3 matlab code

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function implementing Simpson's 1/3 rule for numerical integration. Participants are sharing code snippets, troubleshooting issues related to the implementation, and discussing best practices for coding in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about their implementation, noting that the results seem to double when using greater segments.
  • Another participant suggests testing the function with known analytical results for validation.
  • A participant points out that if 'n' is the number of intervals, the calculation of 'h' should be adjusted to h = (b-a)/n instead of h = (b-a)/(n-1).
  • Concerns are raised about using real values for loop counters, with a recommendation to use integers to avoid potential rounding errors.
  • It is mentioned that the current implementation may double count interior points and that the Simpson's rule requires an even number of intervals for accurate results.
  • A participant expresses gratitude for the feedback and seeks clarification on the potential issues with using real values in loop counters.
  • Another participant explains that rounding errors can lead to unexpected behavior in loop termination, potentially causing some points to be excluded from the calculation.

Areas of Agreement / Disagreement

Participants generally agree on the need for adjustments to the code and the importance of using integer loop counters, but there is no consensus on the best approach to resolve the initial issues with the implementation.

Contextual Notes

Participants note potential limitations related to the definition of 'n' and the implications of using real values in loop counters, which could affect the accuracy of the integration results.

haz
Messages
3
Reaction score
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
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.
 
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.
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K