%QUESTION3 Implements the central differnce method to find the gradient of%y=e^x-2sin^2(2x) at the point where x=1.clear allclcformat longx=1; % defining xy=1; % defining y, (y=x)h=0.1 % first value of hx=x+h; % getting x+h within f(x)y=y-h; % getting x-h within f(x)f = @(x) exp(x)-2*(sin(2*x)).^2; % defining f(x)g = @(y) exp(y)-2*(sin(2*y)).^2; % defining f(y) to later use as f(x-h)Grad=(f(x)-g(y))/2*h % Central difference method, Grad = Gradientfor n=1:2 % 2 iterations after h=0.1h=h/10 % new value for hx=x+h; % to replace f(x) with f(x+h)y=y-h; % to replace f(x) with f(x-h)Grad=(f(x)-g(y))/2*h % Central difference methodend
Here is my code, but my output is incorrect. I did the calculations on paper and they're quite different to my answers. My first value of "Grad" is correct but off by a factor of 1/100. My other two answers are completely wrong.