cos1xx2 cos1xx2

# Cos1xx2 cos1xx2

• pstataintfun
• 67
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 39 - 43 out of 67 pages.

>> plot_approx_deriv(0.2,2,20,’sin(1./x)’,’-cos(1./x)./(x.^2)’);>> plot_approx_deriv(0.2,2,40,’sin(1./x)’,’-cos(1./x)./(x.^2)’);>> plot_approx_deriv(0.2,2,80,’sin(1./x)’,’-cos(1./x)./(x.^2)’);>> plot_approx_deriv(0.2,2,160,’sin(1./x)’,’-cos(1./x)./(x.^2)’);we get the following plots:0.20.40.60.811.21.41.61.82-10-5051015xyExact Derivative vs. Approximate Derivative, N=10exactapproximate0.20.40.60.811.21.41.61.82-10-5051015xyExact Derivative vs. Approximate Derivative, N=20exactapproximate0.20.40.60.811.21.41.61.82-10-5051015xyExact Derivative vs. Approximate Derivative, N=40exactapproximate0.20.40.60.811.21.41.61.82-10-5051015xyExact Derivative vs. Approximate Derivative, N=80exactapproximate0.20.40.60.811.21.41.61.82-10-5051015xyExact Derivative vs. Approximate Derivative, N=160exactapproximate3Write a code that implements composite Simpson’s rule (algorithm 4.1 in thebook). Test your code for the interval [0,1] on the functionf(x) =ex+e-x2withN= 10,20,40,80,160. An example code template is:function I = comp_Simpsons(a,b,N,fstring)f = inline(fstring);% code for composite Simpson’s rule goes here.2
4Write a code that has as input an interval [a, b], a functionf, the antideriva-tiveF, and avectorofN’s, and plots the error for each of theN’s versusthe step sizesh3
tolabel your plots clearly. How does the slope of your log-log plot com-pare to the error term given for composite Simpson’s rule?4
Code%%%%% begin approx_deriv.m %%%%%function yp = approx_deriv(a,b,N,fstring)f = inline(fstring);x = linspace(a,b,N+1);y = f(x);h = (b-a)/N;yp = zeros(size(x));yp(1) = (-3*y(1)+4*y(2)-y(3))/(2*h);yp(2:N) = (-y(1:N-1)+y(3:N+1))/(2*h);yp(N+1) = (y(N-1)-4*y(N)+3*y(N+1))/(2*h);end%%%%% end of approx_deriv.m %%%%%%%%%% begin plot_approx_deriv.m %%%%%function [] = plot_approx_deriv(a,b,N,fstring,fpstring)fp = inline(fpstring);
• • • 