taylor series and conditional while loop
조회 수: 2 (최근 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);
댓글 수: 0
답변 (2개)
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
댓글 수: 0
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
vpa(sol,4)
iter
Try something like this
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!