Error using plot Vectors must be the same length. Error in Eulerrk (line 54) plot (t,h(:,1),A,xR,yR(:,1))
2 views (last 30 days)
Show older comments
clc
clear all
%%RK Method
%h=[3600 1800 900 450 225];
h = 3600;
A0=1;
B0=3;
P0=0;
K=5*10^-5;
Yb=1;
Yp=0.15;
xR = (0:h:43200).';
fyt = @(t,y) [(-K*y(1)*y(2)),...
(-Yb*(K*y(1)*y(2))),...
(Yp*(K*y(1)*y(2)))];
yR = zeros(numel(xR),3)
yR(1,1) = A0;
yR(1,2) = B0;
yR(1,3) = P0;
for i=1 : numel(xR)-1
k1 = fyt(xR(i),yR(i,:));
k2 = fyt(xR(i)+0.5*h,yR(i,:)+0.5*h*k1);
k3 = fyt(xR(i)+0.5*h,yR(i,:)+0.5*h*k2);
k4 = fyt(xR(i)+h,yR(i,:)+h*k3);
yR(i+1,:) = yR(i,:) + (h/6)*(k1+2*k2+2*k3+k4);
end
% e = abs((A_t - y(1)/y(1))*100;
% plot (e)
% hold on
%end
% title('%Error compared with analytical Cl value');
% xlabel('Time(t)');
% ylabel('Error(%)');
% legend('Cl_e 3600','Cl_e 1800','Cl_e 900',Cl_e 450'.'Cl_e 225')
%% Euler
%for h = [3600 1800 900 450 225];
h= 3600;
t = zeros (h,1);
A = zeros (h,1);
B = zeros(h, 1);
P = zeros(h,1);
A(1) = 1;
B(1) = 3;
C(1) = 0;
K = 5*10^-5;
for k = 2:13
t(k) = t(k-1)+3600;
A(k) = A(k-1)+(-K*A(k-1)*B(k-1))*3600;
B(k) = B(k-1)+(-Yb*(K*A(k-1)*B(k-1)))*3600;
P(k)= P(k-1)+ Yp*(K*A(k-1)*B(k-1))*3600;
end
figure(1)
subplot(1,3,1)
plot (t,h(:,1),A,xR,yR(:,1))
xlabel('t')
ylabel('A')
subplot(1,3,2)
plot (t,h(:,2),B,xR,yR(:,2))
xlabel('t')
ylabel('B')
subplot(1,3,3)
plot(t,h(:,3),C,xR,yR(:,3))
xlabel('t')
ylabel('P')
legend('RK4','Euler','Location','Best')
% e = abs((A_t - y(1)/y(1))*100;
% plot (e)
% hold on
%end
% title('%Error compared with analytical Cl value');
% xlabel('Time(t)');
% ylabel('Error(%)');
% legend('Cl_e 3600','Cl_e 1800','Cl_e 900',Cl_e 450'.'Cl_e 225')
I'm getting error in plotting absolute percent error in conc A at final time tf = 12 hours against time step for both euler's and Rk method
0 Comments
Answers (1)
Walter Roberson
on 19 Mar 2022
h= 3600;
That is a scalar.
t = zeros (h,1);
t is a vector of length 3600.
for k = 2:13
t(k) = t(k-1)+3600;
You assign potentially meaningful values to t(2:13), leaving t(1) and t(14:3600) untouched
plot (t,h(:,1),A,xR,yR(:,1))
h is a scalar. h(:,1) is going to be the same as the scalar. So you are asking to plot the vector t of length 3600 as the independent variable, and the scalar 3600 as the dependent variable.
Caution: after that point in the plot() call, you have three numeric parameters. When you use plot() you can pass in a single numeric parameter (and x will be implied) but if you have more than one numeric parameter the numeric parameters must occur in x, y pairs.You can never have an odd number of numeric parameters for plot()
If you were using plot3() then you would always have to have triples. The number of parameters could be odd for plot3(), but it would have to be a multiple of 3, and 5 is not a multiple of 3, so we can tell you did not intend to use plot3()
2 Comments
Walter Roberson
on 19 Mar 2022
You started your k loop at 1, probably.
Consider something like
num_t = 500;
t = (0:num_t-1)*h;
with no loop needed to assign to t.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!