Changing variable in the loop

조회 수: 1 (최근 30일)
Ani Asoyan
Ani Asoyan 2020년 4월 2일
댓글: Ameer Hamza 2020년 4월 3일
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에서 Create Large-Scale Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by