taylor series and conditional while loop

4 visualizaciones (últimos 30 días)
beshayer alturaiyef
beshayer alturaiyef el 19 de Oct. de 2019
Respondida: VBBV el 23 de Oct. de 2021
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);

Respuestas (2)

Sourav Ghai
Sourav Ghai el 22 de Oct. de 2019
Editada: Sourav Ghai el 22 de Oct. de 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

VBBV
VBBV el 23 de Oct. de 2021
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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by