Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
%% 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)

Akzeptierte Antwort

Star Strider
Star Strider am 21 Sep. 2020
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by