Help fixing error code, please!

Hello! Any and all help would be greatly appreciated!
I'm trying to run my code (below), but keep getting the following error code:
Indexin position 1 is invalid. Array indices must be positive integers or logical values.
Error in M_Model (line 56)
RHS(:,pp)=((1-alpha)*Ft(xp(:,pp),tt+1))+
(alpha*(1-Y)*Ft(xpp(:,pp),tt+1))+(alpha*Y*w*Ft(xppp(:,pp),tt+1))+(alpha*Y*(1-w)*Ft(xpppp(:,pp),tt+1));
So here is the code to my chop-it function, with the code I'm trying to run below that. Thank you VERY much in advance for any help/insights on how to fix this!!
%CHOPIT
function [state2]=chopit(state1,bottom,top)
%CHOPIT truncates state values at a low of xcrit and a high of Cap
%input is vector of state values, followed by single values for xcrit and
%cap
%outputs a vector of truncated state values
state2=(state1<=bottom).*bottom+(state1>bottom).*(state1<=top).*state1+(state1>top).*top;
end
%CODE
%specify constants
term = 250; % terminal time
Cap = 10; % cap or max state
xcrit = 3 ; % critical value of x [range 1 to 10]
nchoice = 2 ; % number of choices (varied cost/benefit of fight)
%initialize arrays and vectors used in the program
RHS = zeros(Cap,nchoice); % at each time step, expected fitness of each choice for individual of a given state
opt = zeros(Cap,term); % optimal choice at each state at each time
Ft = zeros(Cap,term); % an array with max expected fitness at each state at each time
%x=1-10 (rows) at each time t=1-20 (columns)
%at each time step, for each state, this will be the max fitness over the
%two choices, which we will calculate below so for now, fill this array
%with zeros
x = linspace(1,Cap,Cap)'; %column vector of state values
%initialize x' and x" arrays with a row for each state and column for each
%patch
xp = zeros(Cap,nchoice); % xprime for each state value, each patch
xpp = zeros(Cap,nchoice); % x double prime for each state value, each patch
%Specify patch parameter values
alpha = [0.25 0.25]; % probability of encountering another male
beta = [0.95 0.95]; % probability of surviving to next time-step
Y = [0.5 0.5]; % probablity of fighting
lambda = [0.5 0.5]; % rate of decay in RHP if no fight
w = [(1/(1+(exp(x)))) (1/(1+(exp(x))))]; % probability of winning a fight
c = [3 3]; % cost of losing a fight
v = [1 1]; % gain of winning a fight
%Specify values of xprime, x double prime, etc
for pp = 1:nchoice %loop through the choices
xp(:,pp) = chopit(x-lambda(pp),xcrit,Cap);
xpp(:,pp) = chopit(x-lambda(pp),xcrit,Cap);
xppp(:,pp) = chopit(x+v(pp),xcrit,Cap); %#ok<AGROW>
xppp(:,pp) = chopit(x-c(pp),xcrit,Cap);
end
%specify terminal fitness
Ft((1:xcrit),end)=0; % fitness=0 at or below xcrit
Ft((4:7),end) = 0.7; % fitness = 0.7 for sneak
Ft((8:Cap),end)=1 ; % fitness=1 for territorial
%make a loop that begins at one step before terminal time and goes back to t=1
for tt= term-1:-1:1 % loop backwards through time
for pp=1:nchoice; % loop through each patch
%dynamic programming equation
RHS(:,pp)=((1-alpha(pp))*Ft(xp(:,pp),tt+1))+ (alpha(pp)*(1-Y(pp))*Ft(xpp(:,pp),tt+1))+(alpha(pp)*Y(pp)*w(pp)*Ft(xppp(:,pp),tt+1))+(alpha(pp)*Y(pp)*(1-w(pp))*Ft(xpppp(:,pp),tt+1));
RHS(1:xcrit,pp)=0;
end
%now find maximum RHS (i.e. max fitness & associated choice)
%the fit and opt arrays will hold all model output
%want max function to return a column vector with the max value of each
%row of RHS. Recall RHS consists of expected fitness of each choice
%(columns) for each state value (rows) & want each row max
[Ft(:,tt),opt(:,tt)]= max(RHS,[],2);
end

13 comentarios

darova
darova el 15 de Abr. de 2020
See this
darova
darova el 15 de Abr. de 2020
This line is too long to check it
RHS(:,pp)=((1-alpha(pp))*Ft(xp(:,pp),tt+1))+ (alpha(pp)*(1-Y(pp))*Ft(xpp(:,pp),tt+1))+(alpha(pp)*Y(pp)*w(pp)*Ft(xppp(:,pp),tt+1))+(alpha(pp)*Y(pp)*(1-w(pp))*Ft(xpppp(:,pp),tt+1));
Also we don't have your data to run the code. Any ideas?
Macie Smith
Macie Smith el 15 de Abr. de 2020
Oh wierd! That's the line I'm having issues with too...
All the data is already in the code!
darova
darova el 15 de Abr. de 2020
Sorry, didn't notice that. Can you show your original formula or something?
darova
darova el 15 de Abr. de 2020
I found something
darova
darova el 15 de Abr. de 2020
But the problem still exists
darova
darova el 15 de Abr. de 2020
Here is the trick i did
And and found this
Macie Smith
Macie Smith el 15 de Abr. de 2020
OH! Great catch!
Macie Smith
Macie Smith el 15 de Abr. de 2020
hmmm yes still getting the error
Macie Smith
Macie Smith el 15 de Abr. de 2020
Hmm I'm not sure that I understand what you did? Did you get it to work?!?!
darova
darova el 15 de Abr. de 2020
It's called 'debugging'. I moved carret before the error occurs and pressed F12, then run the code. Now you can see values of each variables (before error)
Try this trick to make it work
Walter Roberson
Walter Roberson el 15 de Abr. de 2020
Ft(xp(:,pp),tt+1)) asks to recall xp(:,pp) and use it as the first index into the array Ft. Are you certain that xp(:,pp) will be strictly positive integers?
Macie Smith
Macie Smith el 15 de Abr. de 2020
Oh my god darova you are a saint!!! Thank you SO much!!!

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 15 de Abr. de 2020

0 votos

Probably one of the most Frequently Asked of the Frequently Asked Questions. So See the FAQ for a thorough discussion:

Categorías

Preguntada:

el 15 de Abr. de 2020

Comentada:

el 15 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by