Taylor Series expansion error
8 views (last 30 days)
Show older comments
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).
0 Comments
Answers (1)
Walter Roberson
on 29 Sep 2018
You accidentally invoked MATLAB's taylor() function instead of your Taylor() function.
0 Comments
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!