フィルターのクリア

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 件のコメント
Chameleon
Chameleon 2020 年 9 月 21 日
Thank you so much for your help! I really appreciate it :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by