Changing variable in the loop
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi..this is my code....I wrote a loop for 3 iterations and in the loop I wrote if condition(in the very end) and based on that I want to change the variable 'd' .... but it seems that d is not changing, how can I fix it?
x1=0; x2=1; d=0.2; x_e1=0; x_e2=1; N1=0; N2=1;
A=[x1, x2 ]; B=[N1, N2]; C=[x_e1, x_e2];
[m,n,p] = ndgrid(A,B,C);
Z = [m(:),n(:),p(:)];
for k=1:3
a=2; b=2; c=0.5; e=0.5;
u_g = @(x, x_e, N)(-0.5*a*x.^2+b*(x-x_e)-c*N+e*u_p(x,x_e,N, d));
g=(u_g(Z(:,1),Z(:,3), Z(:,2) ).');
p=(u_p(Z(:,1), Z(:,3), Z(:,2),d).');
gp=reshape(g,[4,2]).' ;
pp=reshape(p, [4,2]).' ;
num2strcell = @(m) arrayfun(@num2str, m, 'UniformOutput', false);
payoffs=strcat(num2strcell(gp), num2strcell(pp))
n=4; m=2;
for i=1:n
A(i)=max(pp(:,i));
end
for j=1:m
B(j)=max(gp(j,:));
end
attainedA=(max(pp, [] ,1)==pp);
attainedB=( max(gp,[],2)==gp );
gov=gp(attainedA & attainedB)
if gov==(gp(1,1) | gp(2,1) | gp(1,2) | gp(2,2) )
d=d-9
if gov==(gp(1,3) | gp(2,3) | gp(1,4) | gp(2,4) )
d=d+15
end
end
d
end
d is a variable in this function below
function u=u_p(x,x_e,N,d)
u = -(x-x_e).^2+N*d;
end
Thank you
0 Commenti
Risposta accettata
Ameer Hamza
il 2 Apr 2020
The conditions for if loops are incorrect. Change to this
if (gov==gp(1,1) || gov==gp(2,1) || gov==gp(1,2) || gov==gp(2,2) )
d=d-9
if (gov==gp(1,3) || gov==gp(2,3) || gov==gp(1,4) || gov==gp(2,4) )
d=d+15
end
end
11 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!