taylor series and conditional while loop
Afficher commentaires plus anciens
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);
Réponses (2)
Sourav Ghai
le 22 Oct 2019
Modifié(e) : Sourav Ghai
le 22 Oct 2019
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
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
vpa(sol,4)
iter
Try something like this
Catégories
En savoir plus sur Code Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!