taylor series and conditional while loop

4 次查看(过去 30 天)
clc
clear all
% inputs
x0 = input('what is the startup value x(i): ');
x1 = input('what is the the value you want to predict f(x) at x(i+1): ');
syms x;
syms f(x);
f(x) = log(x*x);
%solving
tv = log(x1*x1);
iter = 1; et = 100; sol=log(x0*x0);
es = 100;
while 1
solold(iter)=sol;
sol = taylor(f, x, 'Order', iter);
es=(0.5*10^(2-iter));
et=abs((tv - sol)/tv)*100;
if et<es
break;
end
iter = iter + 1;
end
sol
iter
shows the folowing:
Error using symengine
Unable to compute a Taylor expansion.
Error in sym/taylor (line 128)
tSym = mupadmex('symobj::taylor',f.s,x.s,a.s,options);
Error in Q32 (line 18)
sol = taylor(f, x, 'Order', iter);

回答(2 个)

Sourav Ghai
Sourav Ghai 2019-10-22
编辑:Sourav Ghai 2019-10-22
Hi,
You just need to set the property 'ExpansionPoint' to 1 in taylor function
taylor(f, x, 'ExpansionPoint',1,'Order', iter);
This code will display the Taylor series:
clc
clear all
% inputs
x0 = input('what is the startup value x(i): ');
x1 = input('what is the the value you want to predict f(x) at x(i+1): ');
syms x;
syms f(x);
f(x) = log(x*x);
%solving
tv = log(x1*x1);
iter = 1; et = 100; sol=log(x0*x0);
es = 100;
while 1
disp(sol)
sol = taylor(f, x, 'ExpansionPoint',1,'Order', iter);
es=(0.5*10^(2-iter));
et=abs((tv - sol)/tv)*100;
iter = iter + 1;
end

VBBV
VBBV 2021-10-23
clc
clear all
% inputs
% x0 = input('what is the startup value x(i): ');
% x1 = input('what is the the value you want to predict f(x) at x(i+1): ');
x0 = 2;
x1 = 4;
syms x;
syms f;
f = log(x*x);
%solving
tv = log(x1*x1);
iter = 1; et = 100; sol=log(x0*x0);
es = 100;
while 1
solold(iter)=sol;
sol = taylor(f, x, 6,'Order', iter); % use conditional value
es=(0.5*10^(2-iter));
ssol = vpa(subs(sol,x,1.5*iter),4) % substitute it tolerance check
sol = ssol;
et=vpa(abs((tv - ssol)/tv),4);
if et>es
break;
end
iter = iter + 1;
end
ssol = 
3.584
ssol = 
2.584
ssol = 
3.021
vpa(sol,4)
ans = 
3.021
iter
iter = 3
Try something like this

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by