In: Computer Science
question should be done on Matlab
4) Find the derivative of sin(2x) at x = 0.8 rad and find the value of h where the error is minimum as compared to the true value i.e. actual value or accurate value. a) Use for loop b) Use element by element operation
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
a)
clc
clear all
close all
format long
x=0.8;
h=logspace(-1,-20);
emin=100;
hN=1;
f=@(x) sin(2*x);
for i=1:length(h)
Dy=(f(x+h(i))-f(x-h(i)))/(2*h(i));
err=abs(Dy-2*cos(2*x));
if(err<emin)
hN=h(i);
emin=err;
end
end
disp('h at which error is min is');
disp(hN);
b)
clc
clear all
close all
format long
x=0.8;
h=logspace(-1,-20);
emin=100;
hN=1;
f=@(x) sin(2*x);
Dy=(f(x+h)-f(x-h))./(2*h);
err=abs(Dy-2*cos(2*x));
hN=h(err==min(err));
disp('h at which error is min is');
disp(hN);
Kindly revert for any queries
Thanks.