Error plotting (Vector must be the same length)

조회 수: 1 (최근 30일)
Seungryul Lee
Seungryul Lee 2022년 4월 13일
편집: Davide Masiello 2022년 4월 14일
clear all
clc
f=@(t,y) 4*y.^2;
F = @(t) 1/(4*(1-t));
a=0; b=0.95;
h=0.05;
n = (b-a)/h;
t = a:h:b;
y = zeros(1,length(t));
y(1) = 0.25;
%EULERS METHOD
for i = 2:n+1
y(i) = y(i-1) + h*f(t(i-1),y(i-1));
end
E=y;
fprintf(' Step number_i time_t Approximated solution Exact solution Relative Error\n')
for j = 1:n+1
m=abs(E(j)-F(t(j)));
n = abs(F(t(j)));
kE = m/n;
A(j) = F(t(j));
fprintf('%5d %18f %15f %20f %15f \n',j,t(j),E(j),F(t(j)),kE)
end
plot(t,A,'.-k', t, E, 'o-r')
xlabel('x');
ylabel('y(t)');
grid on
title('Part(a) Plot');
legend('Exact solution','Eulers solution')
**It all works for the table, but I'm getting an error for plotting. It says the vectors should be in the same length, but I don't get the problem.
Please help me.
Thank you.

답변 (1개)

Davide Masiello
Davide Masiello 2022년 4월 13일
편집: Davide Masiello 2022년 4월 14일
The vector A has 19 elements, whereas t and E have 20.
clear all
clc
f=@(t,y) 4*y.^2;
F = @(t) 1/(4*(1-t));
a=0; b=0.95;
h=0.05;
n = (b-a)/h;
t = a:h:b;
y = zeros(1,length(t));
y(1) = 0.25;
%EULERS METHOD
for i = 2:n+1
y(i) = y(i-1) + h*f(t(i-1),y(i-1));
end
E=y;
fprintf(' Step number_i time_t Approximated solution Exact solution Relative Error\n')
Step number_i time_t Approximated solution Exact solution Relative Error
for j = 1:n+1
m=abs(E(j)-F(t(j)));
n = abs(F(t(j)));
kE = m/n;
A(j) = F(t(j));
fprintf('%5d %18f %15f %20f %15f \n',j,t(j),E(j),F(t(j)),kE)
end
1 0.000000 0.250000 0.250000 0.000000 2 0.050000 0.262500 0.263158 0.002500 3 0.100000 0.276281 0.277778 0.005387 4 0.150000 0.291548 0.294118 0.008738 5 0.200000 0.308548 0.312500 0.012648 6 0.250000 0.327588 0.333333 0.017237 7 0.300000 0.349051 0.357143 0.022658 8 0.350000 0.373418 0.384615 0.029114 9 0.400000 0.401306 0.416667 0.036866 10 0.450000 0.433515 0.454545 0.046266 11 0.500000 0.471102 0.500000 0.057795 12 0.550000 0.515490 0.555556 0.072118 13 0.600000 0.568636 0.625000 0.090183 14 0.650000 0.633305 0.714286 0.113373 15 0.700000 0.713520 0.833333 0.143776 16 0.750000 0.815343 1.000000 0.184657 17 0.800000 0.948299 1.250000 0.241361 18 0.850000 1.128154 1.666667 0.323108 19 0.900000 1.382700 2.500000 0.446920
plot(t(1:end-1),A,'.-k', t(1:end-1), E(1:end-1), 'o-r')
xlabel('x');
ylabel('y(t)');
grid on
title('Part(a) Plot');
legend('Exact solution','Eulers solution')
  댓글 수: 2
Seungryul Lee
Seungryul Lee 2022년 4월 13일
do you know how to use only first 19 elements of t and E, so that I can plot it?
How can I get rid of the last element of t and E
Davide Masiello
Davide Masiello 2022년 4월 13일
편집: Davide Masiello 2022년 4월 14일
Yes
plot(t(1:end-1),A,'.-k', t(1:end-1), E(1:end-1), 'o-r')
I updated it in the answer too, so you can see the resulting plot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by