MATLAB Answers

Index exceeds the number of array elements

1 view (last 30 days)
Brenna McLaughlin
Brenna McLaughlin on 28 Nov 2020
Answered: the cyclist on 28 Nov 2020
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 Comments

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 28 Nov 2020
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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by