Please help me run this - I can't figure out this error!
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I receive the following error code toward the bottom for xp:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check
for mismatched delimiters.
Here's my code with annotation:
term=20; %terminal time
Cap=10; %cap or max state
xcrit=3 ; %critical value of x [range 1 to 10]
nchoice=2 ; %number of choices (fight or no 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 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 choice
xp=zeros(Cap,nchoice); %xprime for each state value, each choice
xpp=zeros(Cap,nchoice); %x double prime for each state value, each choice
%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 1]; %rate of decay in RHP
w = [0.5 0]; %probability of winning a fight
c = [1.5 0]; %cost of losing a fight
v = [2 0]; %gain if win a fight
%Specify values of xprime and x double prime.
for pp=1:nchoice %loop through the patches 1 to npatch
xp(:,pp)=chopit(x+(w.*v)(pp)-(1-w)*c(pp),xcrit,Cap);
xpp(:,pp)=chopit(x-lambda(pp),xcrit,Cap);
end
Code for the chop-it function:
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
Any help would be greatly appreciated!!!
2 Kommentare
Stephen23
am 16 Apr. 2020
Original question from Macie Smith, copied from Google Cache:
"Please help me run this - I can't figure out this error!"
I receive the following error code toward the bottom for xp:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check
for mismatched delimiters.
Here's my code with annotation:
term=20; %terminal time
Cap=10; %cap or max state
xcrit=3 ; %critical value of x [range 1 to 10]
nchoice=2 ; %number of choices (fight or no 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 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 choice
xp=zeros(Cap,nchoice); %xprime for each state value, each choice
xpp=zeros(Cap,nchoice); %x double prime for each state value, each choice
%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 1]; %rate of decay in RHP
w = [0.5 0]; %probability of winning a fight
c = [1.5 0]; %cost of losing a fight
v = [2 0]; %gain if win a fight
%Specify values of xprime and x double prime.
for pp=1:nchoice %loop through the patches 1 to npatch
xp(:,pp)=chopit(x+(w.*v)(pp)-(1-w)*c(pp),xcrit,Cap);
xpp(:,pp)=chopit(x-lambda(pp),xcrit,Cap);
end
Code for the chop-it function:
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
Any help would be greatly appreciated!!!
Antworten (1)
dpb
am 11 Apr. 2020
xp(:,pp)=chopit(x+(w.*v)(pp)-(1-w)*c(pp),xcrit,Cap);
Missing an operator between (w.*v) and (pp) or what variable pp is supposed to be subscripting. Don't know what is intended, but
(w.*v)(pp)
is illegal syntax.
2 Kommentare
dpb
am 11 Apr. 2020
Bearbeitet: dpb
am 11 Apr. 2020
Can't have done...only place () allowed w/o being an explicit subscript of a variable or as an argument to a function would be as subscript to a cell dereference like {...}(pp) and no such thing here.
Explain what it is you think you're writing here--
w = [0.5 0];
v = [2 0];
so
(w.*v) ==> [1 0]
a vector. But you can't write a subscript to a vector; only to a variable. You must create an intermediate variable here or write
x+(w(pp)*v(pp))
if that's what you're trying to obtain
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!