Changing variable in the loop

1 次查看(过去 30 天)
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
Ameer Hamza 2020-4-2
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 个评论
Ani Asoyan
Ani Asoyan 2020-4-2
Can I ask one more question please?
Ameer Hamza
Ameer Hamza 2020-4-3
Sure. If you haven't already got the answer, then you can ask me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by