oh - %first order x1=x+h; x2=x1+h; fp1=(y(x1)-y(x))/h;...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function oh(y,x,h) %Problem 19.1 % %This function computes the (a)forward and (b)backward difference %approximations of O(h) and O(h^2), and the (c)central difference %approximations of O(h^2) and O(h^4) for the first derivatives of y and %x using a value h. Also calculates the true percent relative error %for each approx. % %y=some function, x=some function, h=step value if nargin~=3, error('Must input three arguments'); end fprintf('\n first-order second-order\n') f1= -sin(x); % (a) Forward difference approx
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %first order x1=x+h; x2=x1+h; fp1=(y(x1)-y(x))/h; et1=((f1-fp1)/f1)*100; %second order fp2=(-y(x2)+4*y(x1)-3*y(x))/(2*h); et2=((f1-fp2)/f1)*100; fprintf('\nForward %9.8f %9.8f\n %5.3f %4.3f\n',fp1,fp2,et1,et2) % (b) Backwards difference approx %first order xi=x-h; xi1=xi-h; fp1=(y(x)-y(xi))/h; et1=((f1-fp1)/f1)*100; %second order fp2=(3*y(x)-4*y(xi)+y(xi1))/h^2; et2=((f1-fp2)/f1)*100; fprintf('\nBackward %9.8f %9.8f\n %5.3f %4.3f\n',fp1,fp2,et1,et2)...
View Full Document

Ask a homework question - tutors are online