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

3 ビュー (過去 30 日間)
dareen
dareen 2024 年 7 月 1 日
編集済み: dareen 2024 年 7 月 4 日
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,'*')

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 7 月 1 日
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 件のコメント
dareen
dareen 2024 年 7 月 2 日
it really slipped over my mind thank you
dareen
dareen 2024 年 7 月 2 日
編集済み: dareen 2024 年 7 月 4 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFilter Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by