# Changing variable in the loop

1 view (last 30 days)
Ani Asoyan on 2 Apr 2020
Commented: Ameer Hamza on 3 Apr 2020
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

Ameer Hamza on 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
Ameer Hamza on 3 Apr 2020
Sure. If you haven't already got the answer, then you can ask me.