Voldemord

[Matlab] Algorytm całkowania metodą trapezów

Apr 24th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.29 KB | None | 0 0
  1. %TrapMethod('x^2 +4',1,6);
  2. %   91.6667
  3.  
  4. function [ r ] = TrapMethod(f, a ,b )
  5.  
  6. f=@(x)eval(f);
  7. n = 1000;
  8. s = 0;
  9. dx = (b - a)/n;
  10. i = 1;
  11.     while i < n
  12.         s = s+f(a + i * dx);
  13.         i = i +1;
  14.     end
  15.    
  16.     s = (s + (f(a) + f(b))/2) * dx;
  17.     r = s;
  18.     disp(s);
  19. end
Advertisement
Add Comment
Please, Sign In to add comment