## Numerical Derivative Approximations Using MATLAB?”

i have used taylor series to find the first derivative of the following function F and got 2 diffrent approximations ,

i wanted to check which one has a lower error more precision from the 2 approximations,

i started by understanding how to graph the function and its derviative wrote the 2 expressions i got , then tried to graph each graphs error , i have not yet added axis names and so on , but i reached a certain place where i am not sure if the first approximation values do indeed make sense i expected a diffrent graph , secondly i was asked to do a log log graph to comapre the errors but the values i got were already, straight lines so i seem to be missing something here is the code for now

we were asked to look at h between 10^-1 to 10^-15 did not specifiy how many look at probable this is one of the reasons that i would have beem able to use log log if i kept the x axis 10^-15 to 10^-1 and not set 20 numbers in the set

,thanks for the guidness in advance

%testing a bit too far

f = @cos;

x = 1;

h=0:0.1:8*pi;

plot(x+h,f(x+h),’*’,x-h,f(x-h),’*’)

d_f =@(x) -sin(x);

exact = d_f(x);

h=0:0.1:8*pi;

hold on

plot(x+h,d_f(x+h),’*’,x-h,d_f(x-h),’*’)

exact = d_f(x);

hold off

d_f(1)

f(1)

%testing near the point of interest

h2=0:0.001:10^-2

plot(x+h2,f(x+h2),’*’,x-h2,f(x-h2),’*’)

plot(x+h2,d_f(x+h2),’*’,x-h2,d_f(x-h2),’*’)

%indentifying the appoximations we got

approx_1=@(f, x, h) (f(x + h) – f(x)) / h;

approx_2=@(f, x, h) (f(x + h) – f(x-h)) / (2*h);

d_f(1)

h = linspace(10^-15,10^-1,20)

plot(x+h,approx_1(f,x,h),’*’,x-h,approx_1(f,x,-h),’*’)

plot(x+h,approx_2(f,x,h),’*’,x-h,approx_2(f,x,-h),’*’)

v_1_up=approx_1(f,x,h)-d_f(1)

v_2_up=approx_2(f,x,h)-d_f(1)

v_1_down=approx_1(f,x,-h)-d_f(1)

v_2_down=approx_2(f,x,-h)-d_f(1)

plot(h,v_1_up,’*’,h,v_1_down,’*’)

plot(h,v_2_up,’*’,h,v_2_down,’*’)i have used taylor series to find the first derivative of the following function F and got 2 diffrent approximations ,

i wanted to check which one has a lower error more precision from the 2 approximations,

i started by understanding how to graph the function and its derviative wrote the 2 expressions i got , then tried to graph each graphs error , i have not yet added axis names and so on , but i reached a certain place where i am not sure if the first approximation values do indeed make sense i expected a diffrent graph , secondly i was asked to do a log log graph to comapre the errors but the values i got were already, straight lines so i seem to be missing something here is the code for now

we were asked to look at h between 10^-1 to 10^-15 did not specifiy how many look at probable this is one of the reasons that i would have beem able to use log log if i kept the x axis 10^-15 to 10^-1 and not set 20 numbers in the set

,thanks for the guidness in advance

%testing a bit too far

f = @cos;

x = 1;

h=0:0.1:8*pi;

plot(x+h,f(x+h),’*’,x-h,f(x-h),’*’)

d_f =@(x) -sin(x);

exact = d_f(x);

h=0:0.1:8*pi;

hold on

plot(x+h,d_f(x+h),’*’,x-h,d_f(x-h),’*’)

exact = d_f(x);

hold off

d_f(1)

f(1)

%testing near the point of interest

h2=0:0.001:10^-2

plot(x+h2,f(x+h2),’*’,x-h2,f(x-h2),’*’)

plot(x+h2,d_f(x+h2),’*’,x-h2,d_f(x-h2),’*’)

%indentifying the appoximations we got

approx_1=@(f, x, h) (f(x + h) – f(x)) / h;

approx_2=@(f, x, h) (f(x + h) – f(x-h)) / (2*h);

d_f(1)

h = linspace(10^-15,10^-1,20)

plot(x+h,approx_1(f,x,h),’*’,x-h,approx_1(f,x,-h),’*’)

plot(x+h,approx_2(f,x,h),’*’,x-h,approx_2(f,x,-h),’*’)

v_1_up=approx_1(f,x,h)-d_f(1)

v_2_up=approx_2(f,x,h)-d_f(1)

v_1_down=approx_1(f,x,-h)-d_f(1)

v_2_down=approx_2(f,x,-h)-d_f(1)

plot(h,v_1_up,’*’,h,v_1_down,’*’)

plot(h,v_2_up,’*’,h,v_2_down,’*’) i have used taylor series to find the first derivative of the following function F and got 2 diffrent approximations ,

i wanted to check which one has a lower error more precision from the 2 approximations,

i started by understanding how to graph the function and its derviative wrote the 2 expressions i got , then tried to graph each graphs error , i have not yet added axis names and so on , but i reached a certain place where i am not sure if the first approximation values do indeed make sense i expected a diffrent graph , secondly i was asked to do a log log graph to comapre the errors but the values i got were already, straight lines so i seem to be missing something here is the code for now

we were asked to look at h between 10^-1 to 10^-15 did not specifiy how many look at probable this is one of the reasons that i would have beem able to use log log if i kept the x axis 10^-15 to 10^-1 and not set 20 numbers in the set

,thanks for the guidness in advance

%testing a bit too far

f = @cos;

x = 1;

h=0:0.1:8*pi;

plot(x+h,f(x+h),’*’,x-h,f(x-h),’*’)

d_f =@(x) -sin(x);

exact = d_f(x);

h=0:0.1:8*pi;

hold on

plot(x+h,d_f(x+h),’*’,x-h,d_f(x-h),’*’)

exact = d_f(x);

hold off

d_f(1)

f(1)

%testing near the point of interest

h2=0:0.001:10^-2

plot(x+h2,f(x+h2),’*’,x-h2,f(x-h2),’*’)

plot(x+h2,d_f(x+h2),’*’,x-h2,d_f(x-h2),’*’)

%indentifying the appoximations we got

approx_1=@(f, x, h) (f(x + h) – f(x)) / h;

approx_2=@(f, x, h) (f(x + h) – f(x-h)) / (2*h);

d_f(1)

h = linspace(10^-15,10^-1,20)

plot(x+h,approx_1(f,x,h),’*’,x-h,approx_1(f,x,-h),’*’)

plot(x+h,approx_2(f,x,h),’*’,x-h,approx_2(f,x,-h),’*’)

v_1_up=approx_1(f,x,h)-d_f(1)

v_2_up=approx_2(f,x,h)-d_f(1)

v_1_down=approx_1(f,x,-h)-d_f(1)

v_2_down=approx_2(f,x,-h)-d_f(1)

plot(h,v_1_up,’*’,h,v_1_down,’*’)

plot(h,v_2_up,’*’,h,v_2_down,’*’) plot, data MATLAB Answers — New Questions