필터 지우기
필터 지우기

Why am I getting this error trying to plot?

조회 수: 1 (최근 30일)
Kaylene Widdoes
Kaylene Widdoes 2016년 2월 18일
답변: Walter Roberson 2016년 2월 18일
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')

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 18일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by