taylor series and conditional while loop

조회 수: 21(최근 30일)
beshayer alturaiyef
beshayer alturaiyef 2019년 10월 19일
답변: 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): ');
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

Community Treasure Hunt

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

Start Hunting!

Translated by