Summing using simspons rule for a given range

4 visualizaciones (últimos 30 días)
Oliver Wilson
Oliver Wilson el 9 de Abr. de 2021
Comentada: Rik el 20 de Mayo de 2021
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 comentario
Rik
Rik el 20 de Mayo de 2021
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.

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 9 de Abr. de 2021
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.

Categorías

Más información sobre Google en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by