taylor series method expansion

I am using the following code for taylor series
function [T,Y] = taylor(f,a,b,ya,m)
h = (b - a)/m;
T = zeros(1,m+1);
Y = zeros(1,m+1);
T(1) = a;
Y(1) = ya;
for j=1:m
tj = T(j);
yj = Y(j);
D = feval('df',tj,yj);
Y(j+1) = yj + h*(D(1)+h*(D(2)/2+h*(D(3)/6+h*D(4)/24)));
T(j+1) = a + h*j;
end
At command prompt i am giving the values as follows and getting error
>> syms x y;
>> f=4*x^3+1
f =
4*x^3 + 1
>> taylor(f,0,1,1.5,4)
Error using sym/taylor (line 99)
The value of 'x' is invalid. It must satisfy the function: @(x)isvector(x)&&isAllVars(x).

9 件のコメント

PJS KUMAR
PJS KUMAR 2018 年 9 月 19 日
> f
f =
function_handle with value:
@(x,y)x^2+y^2
>> taylor2(f,0,1,1.5,5)
Error using feval
Undefined function 'df' for input arguments of type 'double'.
Error in taylor2 (line 11)
D = feval('df',tj,yj);
Kindly suggest me how to get rid of this problem
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
... create df.m ?
There is no Mathworks provided function named df
PJS KUMAR
PJS KUMAR 2018 年 9 月 19 日
can u suggest how to create df.m for a function
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
Use a function line,
function result = df(t, y)
... do appropriate calculation here
result = appropriate value to return
PJS KUMAR
PJS KUMAR 2018 年 9 月 19 日
my intention to use 'df' is to find derivative of 'f' and evaluate it at tj and yj. Suggest me alternative code for the following statement in the above program. D = feval('df',tj,yj);
Torsten
Torsten 2018 年 9 月 19 日
function D = df(t,y)
D(1) = 12*t^2;
D(2) = 24*t;
D(3) = 24;
D(4) = 0;
end
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
I am not clear what the parameters of your taylor represent so I do understand the meaning of t and y being passed to df.
I can say you should probably not be mixing feval with syms.
PJS KUMAR
PJS KUMAR 2018 年 9 月 19 日
can we use 'diff' function to assign derivatives to D(1), D(2), D(3) etc.
Walter Roberson
Walter Roberson 2018 年 9 月 19 日
Well you could if you had a variable name to take the derivative with respect to, and you had some reason for D(1) being different from D(2), D(3) etc.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 9 月 18 日

0 投票

You are somehow invoking the symbolic toolbox taylor routine instead of your own. Make sure that you match file name with function name and that your path is correct to prioritize your own function.

カテゴリ

質問済み:

2018 年 9 月 18 日

編集済み:

2018 年 9 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by