what's the reason for getting an output at the point itself though it is not in range

3 views (last 30 days)
i have used taylor series to find the first derivative of the following function cos(x) and got 2 diffrent approximations ,
i wanted to check which one has more precision,
i started by understanding how to graph the function and its derviative wrote the 2 approximations for it, then tried to graph each graphs error , i have not yet added axis names and so on , but this graph
plot(x+h,approx_1(f,x,h),'*',x-h,approx_1(f,x,-h),'*') is for the first approximation of the value of the point
and the second plot(x+h,approx_2(f,x,h),'*',x-h,approx_2(f,x,-h),'*')
yet i get a number at the point it self though the range of h does not include 0 what may be the cause
,thanks for the guidness in advance
%testing a bit too much
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)
ans = -0.8415
f(1)
ans = 0.5403
%testing near the point of interest but closer
h2=0:0.001:10^-2
h2 = 1x11
0 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(x+h2,f(x+h2),'*',x-h2,f(x-h2),'*')
plot(x+h2,d_f(x+h2),'*',x-h2,d_f(x-h2),'*')
%identifiy
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)
ans = -0.8415
h = linspace(10^-15,10^-1,20)
h = 1x20
0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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_1_up = 1x20
-0.1577 -0.0014 -0.0028 -0.0042 -0.0056 -0.0070 -0.0084 -0.0098 -0.0111 -0.0125 -0.0138 -0.0152 -0.0165 -0.0178 -0.0191 -0.0204 -0.0217 -0.0230 -0.0243 -0.0256
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v_2_up=approx_2(f,x,h)-d_f(1)
v_2_up = 1x20
-0.0467 0.0000 0.0000 0.0000 0.0001 0.0001 0.0001 0.0002 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 0.0008 0.0009 0.0010 0.0011 0.0013 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v_1_down=approx_1(f,x,-h)-d_f(1)
v_1_down = 1x20
0.0643 0.0014 0.0029 0.0043 0.0057 0.0072 0.0087 0.0101 0.0116 0.0131 0.0146 0.0161 0.0176 0.0191 0.0207 0.0222 0.0237 0.0253 0.0268 0.0284
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v_2_down=approx_2(f,x,-h)-d_f(1)
v_2_down = 1x20
-0.0467 0.0000 0.0000 0.0000 0.0001 0.0001 0.0001 0.0002 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 0.0008 0.0009 0.0010 0.0011 0.0013 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%plot(h,v_1_up,'*',h,v_1_down,'*')
%plot(h,v_2_up,'*',h,v_2_down,'*')

Answers (1)

Walter Roberson
Walter Roberson on 1 Jul 2024
h=0:0.1:8*pi;
approx_1=@(f, x, h) (f(x + h) - f(x)) / h;
Your h is a vector. You need to use ./ instead of /
  2 Comments
dareen
dareen on 2 Jul 2024
Edited: dareen on 4 Jul 2024
there are some weird stuff at the point itself in the approximaiton i should not be getting a value there as for the rest i have written a code that does whats asked from me log log ploting where the x axis is h i used x+h before so i got some really weird results

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!