Register to reply

Composite Simpsons 1/3 matlab code

by haz
Tags: 1 or 3, code, composite, matlab, simpsons
Share this thread:
haz
#1
May12-14, 01:57 AM
P: 4
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));
Phys.Org News Partner Science news on Phys.org
Security CTO to detail Android Fake ID flaw at Black Hat
Huge waves measured for first time in Arctic Ocean
Mysterious molecules in space
DrClaude
#2
May12-14, 08:35 AM
Sci Advisor
PF Gold
DrClaude's Avatar
P: 1,325
Quote Quote by haz View Post
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.

Quote Quote by haz View Post
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
h = (b-a)/n;
Quote Quote by haz View Post
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.
haz
#3
May13-14, 04:04 AM
P: 4
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.

DrClaude
#4
May13-14, 05:08 AM
Sci Advisor
PF Gold
DrClaude's Avatar
P: 1,325
Composite Simpsons 1/3 matlab code

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.


Register to reply

Related Discussions
Composite Simpsons 1/3 matlab code General Engineering 0
Help with MATLAB code; discrepancy between graph max and Matlab's max Math & Science Software 1
Please I need a Help with matlab code (Controlling the LEGO NXT Using MatLab) Math & Science Software 5
Matlab R2011a's new feature: portable C/C++ code directly from MATLAB Math & Science Software 1