필터 지우기
필터 지우기

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