Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ trap, simp ] = integrate_e(N)
- a= -1;
- b= 5;
- x= a:((b-a)/N):b;
- f= exp(2 * x);
- Iexact = 11013.16522976174;
- trap = 0;
- simp = 0;
- for k=1:N %Note that the vector f has (N+1) elements
- trap = trap+((b-a)/N)*(f(k)+f(k+1))/2;
- end
- for k=1:(N/2)
- simp = simp + ((b-a)/N)/3*(f(2*k-1)+4*f(2*k)+ f(2*k+1));
- end
- fprintf(' Exact integral = %f.\n', Iexact)
- fprintf(' Trapezoidal approximation = %f.\n', trap);
- fprintf(' Simpson approximation = %f.\n', simp);fprintf('\n')
- trap_error = abs(trap-Iexact);
- simp_error = abs(simp-Iexact);
- fprintf(' Trapezoidal error = %f.\n', trap_error);
- fprintf(' Simpson error = %f.\n', simp_error);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement