I am aiming to use a composite simpsons rule on a function but am only interested in summing in when that function is less then a given value eg y = 10
function S=simpson_rule(f,x,dn)
while (f(x)>10)
f(x) = 0
end
h=(x(2)-x(1))/(2*dn); % finding column width
s1=0; %initialising series values
s2=0;
f1 = f(x(1)); %value of function at start
f2 = f(x(2)); %value of function at end
for k=1:dn % odd series
xa=x(1)+h*(2*k-1);
s1=s1+f(xa);
end
for k=1:(dn-1) %even series
xb=x(1)+h*2*k;
s2=s2+f(xb);
end
S=h*(f1+f2+4*s1+2*s2)/3; %simpsons rule formula based on https://slideplayer.com/slide/15091553/
This what ive got but it still sums values higher then y = 10
any advice welcome

댓글 수: 1

Rik
Rik 2021년 5월 20일
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2021년 4월 9일

0 개 추천

If I might suggest, your goal is actively a poor one. Why?
Simpson's rule is a higher order rule. It tries to give you a higher degree of accuracy. However, the idea of introducing what is essentially multiple points of non-differentiability into your function will kill any gains you would have achieved using the higher order nature of Simpson's rule. There is simply no purpose in using Simpson's rule on a function that is not well behaved to gain from the higher order.
So first, just use trapezoidal rule.
Next, are you asking to sum only the part of your function value that does not exceed 10? That is, if the function is larger than 10, will you just use 10 at that point? Or will you not integrate at all over any region where the function value is greater than 10? These two are VERY different goals, and will have very different results.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2021년 4월 9일

댓글:

Rik
2021년 5월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by