Taylor Series matlab using partial derivatives not running

1 vue (au cours des 30 derniers jours)
Kaylene Widdoes
Kaylene Widdoes le 18 Fév 2016
I am not sure how to run the loop for this. It's giving me an error saying I overwrote a variable in the loop but I'm not sure I did. Any help is appreciated. Also, trying to save and graph the function results so any help there too would be great.
Code:
%%Part 3 - Taylor Series Method
clear all
a = 2; b = 10;
h=.01;
t=a:h:b;
N=length(t);
y(2)=0;
for k=1:N-1;
f = (1/((t(k))^2)) - (20 * (y(k))) / (t(k));
ft = (-2/((t(k))^3)) + ( 20 * y(k)) / ((t(k))^2);
fy = (-20 / (t(k)));
fty = (20 / ((t(k))^2));
fyy = 0;
ftt = (6*((t(k))^-4)) - (40 * (y(k)) * ((t(k))^(-3)));
d0=y(k);
d1=f
d2=ft+fy+f;
d3=ftt+(2*f*fty)+(fyy*(f^2))+(fy*ft)+(((fy)^2)*f);
YK1 = d0 + (h*d1) + (((h^2)*d2)/2) + ((h^3)*(d3/6));
end

Réponses (1)

Anish Mitra
Anish Mitra le 22 Fév 2016
I believe that the following error is generated on executing the above code :
Index exceeds matrix dimensions.
Error in taylor_demo (line 10)
f = (1/((t(k))^2)) - (20 * (y(k))) / (t(k));
This is due the variable 'y' only being defined with 2 values. Hence, when k = 3, it is unable to access the value y(3).
A good way to debug errors in code is to use the MATLAB Debugger. Using the following command will stop at the point of the error in the code, and you can then display the temporary results at the time of error.
>> dbstop if error
This is useful in checking the values of the different variables. To clear the debugging conditions once you are done, the following command can be used.
>> dbclear all
There are also many other options associated with the debugger. I would recommend going over the documentation :

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by