/* Approximate the derivative f'(x) using a second order approximation of the form R(h) = c1 D(h) + c2 D(2h) where D(h) = (f(x+h) - f(x))/h and constants c1 and c2 are given by Richardson extrapolation. */ #include #include double f(double x){ return 1/(x*x-x+3); } double df(double x){ double t=x*x-x+3; return -(2*x-1)/(t*t); } double D(double x,double h){ return (f(x+h)-f(x))/h; } double R(double x,double h){ /* Please fill in this function */ } #define N 8 double y[N]= { 4.44444444444444e-02, 2.22222222222222e-02, 7.12788259958069e-03, 1.90869551612988e-03, 4.83780672011547e-04, 1.21029365004299e-04, 3.02168640078926e-05, 7.54583882628390e-06 }; int main(){ printf("This is problem 3 on exam part 2.\n"); printf("%21s %21s %21s\n","h","R_h","Error"); double eold=1,x=1,dfx=df(x),h=1,flag=0; for(int n=0;ny[n]*1e-6) flag=1; printf("%21.14e %21.14e %21.14e\n",h,t,e); h/=2; } if(!flag){ printf("Congratulations! Your answer is correct!\n"); } else { printf("Please try again. Your answer is incorrect.\n"); } return 0; }