Taylor Series expansion error

2 次查看(过去 30 天)
PJS KUMAR
PJS KUMAR 2018-9-29
I have the following code for taylor series expansion.
function [sol]=Taylor(fn,a,b,y0,h,nd)
syms x y(x);
xd = a:h:b;
yd(1) = y0;
for i = 1:numel(xd)-1
fstart= fn;
fold = fstart;
D(1) = subs(fold,{x,y},{xd(i),yd(i)});
for j=2:nd
fnew = subs(diff(fold,x),diff(y(x),x),fstart);
D(j) = subs(fnew,{x,y},{xd(i),yd(i)});
fold = fnew;
end
yd(i+1) = yd(i)+h*D(1)+h^2/2*D(2)+h^3/6*D(3)+h^4/24*D(4)+h^5/120*D(5);
end
sol=[xd' yd'];
end
It is giving correct output in one system. But giving the following errors in some other systems. Suggest me the solution to get the output.
>> syms x y(x)
>> f=x+y
f(x) =
x + y(x)
>> taylor(f,1,3,0.8,0.5,5)
Error using sym/taylor (line 99)
The value of 'x' is invalid. It must satisfy the function: @(x)isvector(x)&&isAllVars(x).

回答(1 个)

Walter Roberson
Walter Roberson 2018-9-29
You accidentally invoked MATLAB's taylor() function instead of your Taylor() function.

类别

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