Index exceeds the number of array elements
조회 수: 1 (최근 30일)
이전 댓글 표시
function bestconstant = brenna1(usstates)
%% Create some "experimental data" for exponential growth
out1=projectscrapefunWY(usstates);
data3=out1(:,1);
data4=out1(:,2);
trange = data4; % trange is going to be data4 from projectscrape
Yexp = data3; % case numbers from projectscrapefun
plot(trange, Yexp) % plot it
%% ODE Information
trange2 = [0 trange(end)]; % range to solve ode over
y0 = 578759; % Initial population of wyoming
%% Initializtion guess
c0 = [0.25 0.25 0.25 0.25 0.25]; % order of matrix: beta, lamda, mu, kappa, gamma
% do we add all the parameters here as the first guess?
Model1 = ode45(@(t,z)ourODEmodel(t,z,c0), trange2, y0); % Run the ODEsolver
%% using trange2, y0, and our initial guess of c0 as a parameter! note @(t,z)
%% in the syntax
%% new functionality below
simY = deval(Model1, trange); % Evaluate the solution at the experimental time steps
%% neat!
hold on
plot(trange, simY, '-r') ; % plot this too
legend('experiment','model with initial guess')
%% Set up optimization function
Objective = @(x) objFcn(x, trange, Yexp,trange2,y0);
lb = [0 0 0 0 0];
ub = [1 1 1 1 1];
% I have no idea how to set the lower and upper bonds for each value in the
% parameter matrix
bestc = lsqnonlin(Objective, c0, lb, ub);
% do we repeat this line for each of the parameters so that it prints the
% best for each?
%% Plot best result
Model1 = ode45(@(t,z)ourODEmodel(t,z,bestc), trange2, y0);
bestY = deval(Model1, trange);
plot(trange, bestY, '-g')
legend('Exp Data','Initial Param','Best Param');
function f = ourODEmodel(t, z, c)%add other parameters to the variables needed for this function?
S=z(1);
E=z(2);
I=z(3);
R=z(4);
f(1) = (-c0(1)*S*I) +c0(2) -(c0(3)*S); % sdot write in all the function from the word document here?
f(2)=(c0(1)*S*I)-(c0(3)*c0(4))*E; % edot
f(3)=c0(4)*E-(c0(5)+c0(3))*I; %idot
f(4)=c0(5)*I-c0(3)*R; %rdot
function accuracy = objFcn(x,trange,Yexp,trange2,y0)
Model1 = ode45(@(t,z)ourODEmodel(t,z,x), trange2, y0);
simY = deval(Model1, trange);
simY=simY(3,:); %only keeps the third entry for I, infected only what we have experimental for
accuracy = simY-Yexp; % subtract experiment from best model
In this code I am using optimization to fit parameters for coupled differential equations and building predictive models therein. The first error I am getting is that the index exceeds the number of array elements. If someone could help pinpoint where this error is that would be greatly appreciated.
댓글 수: 0
답변 (1개)
the cyclist
2020년 11월 28일
This is not strictly an answer to your question, but I recommend learning how to use the debugger. Then you will be able to pinpoint the line of code that is causing the error yourself.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!