legrahamcracker

Adaptive Simpson's Rule with true error for sin(x)

May 22nd, 2021 (edited)
268
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_f_error (func, a, b, tol)
  2. m = (a+b)/2;
  3. ## c = 1/(1/16-1);
  4. ## k = 1/15;
  5. ## f = sin(x) for reference
  6. I = cos(a)-cos(b);
  7.  
  8. [outI_1, outX_1] = composite_two_simpson(func, a, m, (m-a)/2 );
  9. [outI_2, outX_2] = composite_two_simpson(func, m, b, (b-m)/2 );
  10.  
  11. if abs(I - (outI_1 + outI_2)) < tol
  12.   outI = outI_1 + outI_2;
  13.   outX = unique([outX_1, outX_2]);
  14.  
  15. else
  16.   [outI_3, outX_3] = adaptive_two_simpson_f_error(func, a, m, tol/2);
  17.   [outI_4, outX_4] = adaptive_two_simpson_f_error(func, m, b, tol/2);
  18.  
  19.   outI = outI_3 + outI_4;
  20.   outX = unique([outX_3, outX_4]);
  21.  
  22. endif
  23.  
  24. endfunction
  25.  
Add Comment
Please, Sign In to add comment