Advertisement
legrahamcracker

Adaptive Simpson's Rule

May 22nd, 2021
2,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 0.63 KB | None | 0 0
  1. function [outI, outX] = adaptive_two_simpson (func, a, b, tol)
  2. m = (a+b)/2;
  3. ##c = 1/(1/16-1);
  4. k = 1/15;
  5.  
  6. [outI_0, outX_0] = composite_two_simpson(func, a, b, (b-a)/2 );
  7. [outI_1, outX_1] = composite_two_simpson(func, a, m, (m-a)/2 );
  8. [outI_2, outX_2] = composite_two_simpson(func, m, b, (b-m)/2 );  
  9.  
  10. if abs(k*(outI_0 - (outI_1 + outI_2))) < tol
  11.   outI = outI_1 + outI_2;
  12.   outX = unique([outX_1, outX_2]);
  13.  
  14. else
  15.   [outI_3, outX_3] = adaptive_two_simpson(func, a, m, tol/2);
  16.   [outI_4, outX_4] = adaptive_two_simpson(func, m, b, tol/2);
  17.  
  18.   outI = outI_3 + outI_4;
  19.   outX = unique([outX_3, outX_4]);
  20.  
  21. endif
  22.  
  23. endfunction
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement