Advertisement
OreganoHauch

Numerisches Trapez-Integral

Nov 17th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. function plot_trapez()
  2. f = @sin;
  3. a = 0;
  4. b = pi;
  5. fehler = [];
  6. n_values = 1e3:100:1e4;
  7. for n=n_values
  8. fehler(end+1) = abs(2-trapez(f,a,b,n));
  9. end
  10. plot(n_values, fehler)
  11. end
  12.  
  13. function I = trapez(f,a,b,n)
  14. h = (b-a)/n;
  15. x0 = a/2;
  16. xn = (a+n*h)/2;
  17. I = h*(f(x0) + f(xn));
  18. for j=1:n-1
  19. xj = a + j*h;
  20. I = I + f(xj);
  21. end
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement