MATLAB: Am I getting this error trying to plot

errorgraphplottaylor seriesvector

Here is my code – it will not let me plot. I am trying to plot everything on one graph and cannot do so.
% HW 3 Part 3
% Coarse solution
h1 = .01;
a = 2;
b = 10;
T = a:h1:b;
M = length(T);
ya = 0;
Y1 = zeros(1,M);
Y1(2) = ya;
for j=1:M-1
f = (1/((T(j))^2)) - (20 * (Y1(j))) / (T(j));
fx = (-2/((T(j))^3)) + ( 20 * Y1(j)) / ((T(j))^2);
fy = (-20 / (T(j)));
fxy = (20 / ((T(j))^2));
fyy = 0;
fxx = (6*((T(j))^-4)) - (40 * (Y1(j)) * ((T(j))^(-3)));
d0 = Y1(j);
d1 = f;
d2 = fx + f*fy;
d3 = fxx + fxy*f + (fx+fy*f)*fy + f*(fxy+fyy*f);
Y1(j+1) = d0 + h*d1 + .5*h^2*d2 + h^3*d3/6;
end
% Fine solution
h2 = .001;
a = 2;
b = 10;
T = a:h2:b;
M = length(T);
ya = 0;
Y2 = zeros(1,M);
Y2(2) = ya;
for j=1:M-1
f = (1/((T(j))^2)) - (20 * (Y2(j))) / (T(j));
fx = (-2/((T(j))^3)) + ( 20 * Y2(j)) / ((T(j))^2);
fy = (-20 / (T(j)));
fxy = (20 / ((T(j))^2));
fyy = 0;
fxx = (6*((T(j))^-4)) - (40 * (Y2(j)) * ((T(j))^(-3)));
d0 = Y2(j);
d1 = f;
d2 = fx + f*fy;
d3 = fxx + fxy*f + (fx+fy*f)*fy + f*(fxy+fyy*f);
Y2(j+1) = d0 + h*d1 + .5*h^2*d2 + h^3*d3/6;
end
% Finer solution
h3 = .0001;
a = 2;
b = 10;
T = a:h3:b;
M = length(T);
ya = 0;
Y3 = zeros(1,M);
Y3(2) = ya;
for j=1:M-1
f = (1/((T(j))^2)) - (20 * (Y3(j))) / (T(j));
fx = (-2/((T(j))^3)) + ( 20 * Y3(j)) / ((T(j))^2);
fy = (-20 / (T(j)));
fxy = (20 / ((T(j))^2));
fyy = 0;
fxx = (6*((T(j))^-4)) - (40 * (Y3(j)) * ((T(j))^(-3)));
d0 = Y3(j);
d1 = f;
d2 = fx + f*fy;
d3 = fxx + fxy*f + (fx+fy*f)*fy + f*(fxy+fyy*f);
Y3(j+1) = d0 + h*d1 + .5*h^2*d2 + h^3*d3/6;
end
%Exact solution
t=a:h3:b;
y = (1./(19.*t)) - (524288./(19.*(t.^20)));
plot(t,y,'k',T,Y1,'bo-',T,Y2,'ro-',T,Y3,'go-')
legend('Exact','h=0.01','h=0.001','h=0.0001')
title('The Taylor Method with 3 meshes')

Best Answer

You are using different T values when you construct Y1, Y2, Y3, but you try to use the same T value to plot all three of them. You should have a T1, T2, T3