Adaptive step size trapezoidal integration

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Integration
Click For Summary

Discussion Overview

The discussion revolves around the implementation and behavior of an adaptive step size trapezoidal integration algorithm in a programming context. Participants explore how the algorithm evaluates function values at specific points and the implications of these evaluations on the accuracy of integration results.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a code snippet for an adaptive step size trapezoidal integration function and questions its correctness and the evaluation points based on a given graph.
  • Another participant points out that the algorithm does not inherently know where to place evaluation points and relies on the program's logic to determine these locations.
  • There is a discussion about the intervals that need to be checked for convergence, with some participants suggesting that certain intervals are overlooked.
  • Participants express confusion regarding the order of evaluations and the output of the algorithm, particularly in relation to the intervals with sharp turns in the graph.
  • Some participants highlight potential flaws in the algorithm and suggest that it may yield incorrect results for specific integrals.
  • There is a back-and-forth regarding the specific values that the algorithm evaluates and the order in which they are displayed, with corrections and clarifications being made throughout the discussion.

Areas of Agreement / Disagreement

Participants express differing views on the evaluation points and the correctness of the algorithm. There is no consensus on the accuracy of the results or the best approach to understanding the algorithm's behavior.

Contextual Notes

Participants note that the algorithm's performance may depend on the specific function being integrated and the tolerance settings used, which could lead to varying results in different scenarios.

gfd43tg
Gold Member
Messages
949
Reaction score
48
https://www.physicsforums.com/attachments/72086

https://www.physicsforums.com/attachment.php?attachmentid=72087&d=1407804589

Hello

Here is the code for the adaptive stepsize function
Code:
function I = arttrap(fh,a,b,tol,fa,fb)
if nargin == 4
    fa = fh(a); fb = fh(b);
end
m = (a+b)/2;
fm = fh(m);
h = b-a;
% Compute I based on 1 subdivision
I1 = 0.5*(fa+fb)*h;
% Compute I based on 2 subdivisions
I2 = (fa + 2*fm + fb)*h/4;
% Compare both estimates
if abs(I1-I2)<tol
    I = I2;
else
    ILeft = artrap(fh,a,m,tol/2,fa,fm);
    IRight = artrap(fh,m,b,tol/2,fm,fb);
    I = ILeft + IRight;
end
disp(['a = ' num2str(a) ', b=' num2str(b)]);

I am wondering if I did it correctly, or better yet if just by looking at the graph I would be able to tell the values of ##x## which are evaluated. This can't be plotted to test, so it's clear this was a contrived graph to test the student's understanding of how adaptive stepsize integration works. I am just wondering what would indicate the places that would be checked.

From what I see, 2 and 10 would be looked at. Then also 6 and 8, since there is a sharp turn at those locations, and a step could be placed there. But my calculations are saying that it's also evaluated at 7 and 9.
 
Last edited:
Physics news on Phys.org
Your two links don't work.
 
ImageUploadedByPhysics Forums1407817992.040101.jpg


ImageUploadedByPhysics Forums1407818013.184780.jpg


Okay my bad, there they are
 
Maylis said:
From what I see, 2 and 10 would be looked at. Then also 6 and 8, since there is a sharp turn at those locations, and a step could be placed there. But my calculations are saying that it's also evaluated at 7 and 9.

The computer doesn't magically "know" where the kinks in the graph are and put the steps and the "best" places. It just does what the program tells it to do.

I think you are right about evaluating at 7 and 9. The interval 6 to 10 doesn't converge so it is split into 6 ro 8, and 8 to 10. The computer doesn't "know" what the graph looks like, so it has to evaluate at the mid point of those intervals. Both intervals converge so they are not split any further.

The marks on the graph, and the answer on the second sheet, forgot about checking the interval 2 to 6.

BTW this is a poor algorithm for doing integration (but it's a nice exercise related to trees and recursive functions). See why you get the wrong answer, if you use it to evaluate ##\int_0^{2\pi} \sin^2\!\! x\, dx##.
 
To be clear, when AlephOne said "The marks on the graph, and the answer on the second sheet, forgot about checking the interval 2 to 6.", he's saying you've missed a value (which you have).
 
So that means my answers are right? Even the 2nd question?
 
Maylis said:
So that means my answers are right? Even the 2nd question?
You're list (2,10,6,7,8,9) is missing a number.
You're estimation of what will be printed is also wrong.

Here's what will happen:

artrap(2,10) called.
--fh evaluated at 2, 10, and then 6
--artrap(2,6) called.
----fh evaluated at <you fill this in>
----display 2, 6
--artrap(6,10) called.
----fh evaluated at <you fill this in>
----artrap(6,8) called.
...

Notice that the first display is "a=2, b=6", not "a=2, b=10" which is what you have.

So in the attachments, both H.1 and H.2 have mistakes.
 
So it evaluates at 2,10,6,4,8,7,9.

I am confused about where it has its end points. This code gets confusing because when it splits up the step, it doesn't meet tolerance requirements, so it splits itself again, then I don't understand how to get it to go back.

Does it display
1. a=2 b=6
2. a=6 b=8
3. a=8 b=10
?
If so, is that just coincidentally the places on the graph with the sharp turnsartrap(2,10) called.
--fh evaluated at 2, 10, and then 6
--artrap(2,6) called.
----fh evaluated at <2,4,6>
----display 2, 6
--artrap(6,10) called.
----fh evaluated at <6,8,10>
----artrap(6,8) called.
---- fh evaluated at <6,7,8>
----artrap(8,10) called
---- fh evaluated at <8,9,10>
 
Last edited:
Maylis said:
So it evaluates at 2,10,6,4,8,7,9.
Yes.
Maylis said:
I am confused about where it has its end points. This code gets confusing because when it splits up the step, it doesn't meet tolerance requirements, so it splits itself again, then I don't understand how to get it to go back.

Does it display
1. a=2 b=6
2. a=6 b=8
3. a=8 b=10
?
If so, is that just coincidentally the places on the graph with the sharp turns
Those are the first three lines displayed. But there are two more.
The first call to artrap is with a=2, b=10. Since the last line in artrap reports its input, you should know that the last thing that will be reported is "a=2, b=10". You might want to work backwards through the list.
Maylis said:
artrap(2,10) called.
--fh evaluated at 2, 10, and then 6
--artrap(2,6) called.
----fh evaluated at <2,4,6>
----display 2, 6
--artrap(6,10) called.
----fh evaluated at <6,8,10>
----artrap(6,8) called.
---- fh evaluated at <6,7,8>
----artrap(8,10) called
---- fh evaluated at <8,9,10>
Not quite.
artrap(2,10) called.
--fh evaluated at 2, 10, and then 6
--artrap(2,6) called.
----fh evaluated at 4 (and only 4)
----display 2, 6
--artrap(6,10) called.
----fh evaluated at 8 (and only 8)
----artrap(6,8) called.
------fh evaluated at 7 (and only 7)
------display <you fill this in>
----artrap(8,10) called.
------fh evaluated at 9 (and only 9)
------display <you fill this in>
----display <you fill this in>
--display <you fill this in>


Notice that I am using two dashes for each level in the stack. It's important to keep track of this.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 20 ·
Replies
20
Views
2K
Replies
11
Views
2K
  • · Replies 15 ·
Replies
15
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
3K
Replies
15
Views
2K
Replies
3
Views
2K