Please help, I don't understand the error. How I can make it correct?

2 次查看(过去 30 天)
%%%%this function is defined, to input the model equation into matlab,
% to be called on later for fitting the function takes three inputs
function SIRmodel(t,y,par)
lambda=par(1);
gamma=par(2);
S=y(1);
I=y(2);
R=y(3);
N=S+I+R;
Sdot=-lambda*I*S;
Idot=lambda*I*S-gamma*I;
Rdot=gamma*I;
f=[Sdot Idot Rdot];
end
%%%% i use the ode45 to solve the differential equation
function sol=SIRSol(par,IC,t)
DeHandle=@(T,Y) SIRModel(t,y,par);
[~,Y]=ode45(DeHandle,t,IC);
sol=Y';
end
%%%%first tke 20 randomly chosen time points
numpts=20;
tdata=[0 sort(20*rand(1,numpts))];
%%%% then we generate normally distribute noise:
width=0.1;
ndataSIR=20*[0 randn(0,width,[1,numpts]); 0 randn(0,width,[1,numpts]); 0 randn(0,width,[1,numpts])];
lambda=0.01;
%%%%i add the noise term to the model outputs
gamma=0.1;
par=[lambda gamma];
IC=[50 1 0];
%%%visualize the data
SIRData=SIRSol(Par, IC, tdata)+ndataSIR;
SIRData=[0 1 0 ; 1 1 1]*SIRData;
figure()
plot(tdata,SIRData(1,:),'r*');
hold on;
plot(tdata,SIRData(2,:),'o');
%%%i define the least-square error term
SIRparSol=@(par) sum(sum((SIRparSol ([par(1) par(2)],IC,t);
sumsquaresSIR=@(par) sum(sum((SIRparSol (par , tdata)-SIRData).2));
%%%call fminsearch with an initial guess
[SIRtheta,fval,exitflag]=fminsearch(SumSquaresSIR,[8 0.02]);
SIRsol=SIRparSol(SIRtheta,tsol);
figure;
plot(tdata, SIRData, '.')
hold on;
plot(tsol, SIRsol,'--');

回答(2 个)

Sulaymon Eshkabilov
There are a few crucial points overlooked in your code.
(1) Placement of input entries
(2) Placement of functions (SIRSol, SIRmodel, SIRparSOL) and from where they are called to run
(3) Syntaxes how to call/execute the functions.
Here is a simple example how to structure such functions and call them respectively. E.g.:
x0 = 0;
xend=13;
Ndata = 200;
[x, y] = Main(x0, xend, Ndata);
DEMO_data = Plot_ALL(x, y);
function [x, y] = Main(x1, x2,N)
x = linspace(x1, x2, N);
y = sin(x);
end
function H=Plot_ALL(x, y)
H=plot(x, y, 'ro--', 'DisplayName', 'x vs. y');
grid on
end

Kevin Holly
Kevin Holly 2023-1-11
The line here need to be fixed:
ndataSIR=20*[0 randn(0,width,[1,numpts]); 0 randn(0,width,[1,numpts]); 0 randn(0,width,[1,numpts])];
You have width as an input. The input variable to randn need to be an integer (here width = 0.1). Even if that is fixed, [1,numpts] is not scalar and won't be accepted as an input. What are you trying to do on this line? Did you mean to use randn as opposed to rand? rand could accept 0.1, but I'm not sure what you are trying to do.
  4 个评论
Kevin Holly
Kevin Holly 2023-1-12
%%%%first tke 20 randomly chosen time points
numpts=20;
tdata=[0 sort(20*rand(1,numpts))];
%%%% then we generate normally distribute noise:
width= 0.1; % width needs to be an integer for it to be an input into randn, so i changed it to 1
ndataSIR=20*[0 normrnd(0,width,[1,numpts]); 0 normrnd(0,width,[1,numpts]); 0 normrnd(0,width,[1,numpts])];
lambda=0.01;
%%%%i add the noise term to the model outputs
gamma=0.1;
par=[lambda gamma];
IC=[50 1 0];
%%%visualize the data
SIRData=SIRSol(par, IC, tdata)+ndataSIR;
Unrecognized function or variable 'y'.

Error in solution>@(T,Y)SIRModel(t,y,par) (line 46)
DeHandle=@(T,Y) SIRModel(t,y,par);

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);

Error in solution>SIRSol (line 47)
[~,Y]=ode45(DeHandle,t,IC);
SIRData=[0 1 0 ; 1 1 1]*SIRData;
figure()
plot(tdata,SIRData(1,:),'r*');
hold on;
plot(tdata,SIRData(2,:),'o');
%%%i define the least-square error term
SIRparSol=@(par) sum(sum((SIRparSol([par(1) par(2)],IC,t))));
sumsquaresSIR=@(par) sum(sum((SIRparSol(par, tdata)-SIRData)*0.2));
%%%call fminsearch with an initial guess
[SIRtheta,fval,exitflag]=fminsearch(SumSquaresSIR,[8 0.02]);
SIRsol=SIRparSol(SIRtheta,tsol);
figure;
plot(tdata, SIRData, '.')
hold on;
plot(tsol, SIRsol,'--');
%%%%this function is defined, to input the model equation into matlab, to be called on later for fitting the function takes three inputs
function SIRmodel(t,y,par)
lambda=par(1);
gamma=par(2);
S=y(1);
I=y(2);
R=y(3);
N=S+I+R;
Sdot=-lambda*I*S;
Idot=lambda*I*S-gamma*I;
Rdot=gamma*I;
f=[Sdot Idot Rdot];
end
%%%% i use the ode45 to solve the differential equation
This function here uses a variable y that is undefined. Do you know what y should be?
function sol=SIRSol(par,IC,t)
DeHandle=@(T,Y) SIRModel(t,y,par);
[~,Y]=ode45(DeHandle,t,IC);
sol=Y';
end

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by