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 日

0 投票

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 日
okay,,,but the result is the same, it doesn't change in the next iteration
Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
Are you sure that you correctly replaced the code. I get different value of d in each iteration
>> test_fun
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-0.40.2' } {'0.1-0.8'}
{'-2.5-1'} {'-10' } {'-2.9-0.8'} {'-1.40.2'}
gov =
-1
d = % <---- 1st iteration
-8.8000
d =
-8.8000
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-4.9-8.8'} {'-4.4-9.8'}
{'-2.5-1'} {'-10' } {'-7.4-9.8'} {'-5.9-8.8'}
gov =
-1
d = % <---- 2nd iteration
-17.8000
d =
-17.8000
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-9.4-17.8' } {'-8.9-18.8' }
{'-2.5-1'} {'-10' } {'-11.9-18.8'} {'-10.4-17.8'}
gov =
-1
d = % <---- 3rd iteration
-26.8000
d =
-26.8000
Ani Asoyan
Ani Asoyan 2020 年 4 月 2 日
I'm sorry but d doesn't change in my code, how did you get it ? :D ... is it maybe because I wrote the function of d separately and didn't write it in the loop?
Ani Asoyan
Ani Asoyan 2020 年 4 月 2 日
These are my results
>> Untitled3
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-0.40.2' } {'0.1-0.8'}
{'-2.5-1'} {'-10' } {'-2.9-0.8'} {'-1.40.2'}
gov =
-1
d =
0.2000
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-0.40.2' } {'0.1-0.8'}
{'-2.5-1'} {'-10' } {'-2.9-0.8'} {'-1.40.2'}
gov =
-1
d =
0.2000
payoffs =
2×4 cell array
{'00' } {'0.5-1'} {'-0.40.2' } {'0.1-0.8'}
{'-2.5-1'} {'-10' } {'-2.9-0.8'} {'-1.40.2'}
gov =
-1
d =
0.2000
>>
Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
Can you paste your updated code here?
Ani Asoyan
Ani Asoyan 2020 年 4 月 2 日
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-2
if gov==(gp(1,3) || gp(2,3) || gp(1,4) || gp(2,4) )
d=d+3
end
end
d
end
Ani Asoyan
Ani Asoyan 2020 年 4 月 2 日
oh I'm sorry , I'll fix it , thank you so much
Ani Asoyan
Ani Asoyan 2020 年 4 月 2 日
my bad, I was not focused, sorry , thanks
Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
No Problem! Glad to be of help.
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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by