what's the reason for getting an output at the point itself though it is not in range
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
f(1)
%testing near the point of interest but closer
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),'*')
%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)
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,'*')
0 Kommentare
Antworten (1)
Walter Roberson
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Filter Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!