Unable to perform assignment because the left and right sides have a different number of elements.

1 次查看(过去 30 天)
%% Euler framåt
clc
clear all
close all
%parametrar (samma som ovan)
tMax = 20;
timeSpan = [0 tMax];
S0 = 1.0;
I0 = 0.0;
R0 = 0.0;
dt = 0.01;
time_vector = 0:dt:tMax;
nIterations = length(time_vector);
tau = 0.6;
h = 0.5;
rho = 0.8;
r = 0.2;
beta = (h*exp(-h*tau))/(1-exp(-h*tau));
%Euler
S = zeros(size(time_vector));
I = zeros(size(time_vector));
R = zeros(size(time_vector));
S(1) = S0;
I(1) = I0;
R(1) = R0;
%vi utnyttjar att: nIterations = length(time_vector) = numel(S)
for i=1:nIterations-1
S(i+1)= S(i)-dt.*(h.*S+rho.*I+beta.*R);
I(i+1)= I(i)+dt.*(h.*S-rho.*I-r.*I);
R(i+1)= R(i)+dt.*(r.*I-beta.*R);
end
%plotta euler framåt
figure(2)
plot(0:tMax,S)
hold on
plot(0:tMax,I)
hold on
plot(0:tMax,R)

采纳的回答

Star Strider
Star Strider 2020-9-21
Subscript the vectors in the loop that calculates the new values:
for i=1:nIterations-1
S(i+1)= S(i)-dt.*(h.*S(i)+rho.*I(i)+beta.*R(i));
I(i+1)= I(i)+dt.*(h.*S(i)-rho.*I(i)-r.*I(i));
R(i+1)= R(i)+dt.*(r.*I(i)-beta.*R(i));
end
Then make appropriate changes to the plotting routine:
%plotta euler framåt
t = linspace(0, tMax, numel(S));
figure(2)
plot(t,S)
hold on
plot(t,I)
plot(t,R)
hold off
grid
legend('S(t)','I(t)','R(t)')
.
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by