Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

plot values in the loop

조회 수: 1 (최근 30일)
Ani Asoyan
Ani Asoyan 2020년 4월 3일
마감: darova 2020년 4월 3일
Hello
my code is something like this
p=3
for k=1:p
%something
if %something
d=d+1
elseif %something
d=d-2
end
plot(k,d,'o')
hold on
end
I get three values of d corresponding to three values of k. I want to plot these values so that the x axies will be the values of k and the y axis will be the values of d
The thing is, with this code I get only points, but I want to link them with lines. How can I do that?
Thank you
  댓글 수: 3
Mehmed Saad
Mehmed Saad 2020년 4월 3일
the easiest way is to store your variables in a vector with each iteration and plot them at the end of for loop, in this way they will be joined together
Ani Asoyan
Ani Asoyan 2020년 4월 3일
I've tried with vectors, but the thing is,,, d is a variable that is defined in a separate function file,, and when I make it a vector, it gives me errors

답변 (2개)

Jakob B. Nielsen
Jakob B. Nielsen 2020년 4월 3일
do
plot(k,d,'o-')
instead, that will give you o's connected by lines.
  댓글 수: 2
Mehmed Saad
Mehmed Saad 2020년 4월 3일
i don't think so sir
it wont
try it
Ani Asoyan
Ani Asoyan 2020년 4월 3일
sorry it didn't work

KSSV
KSSV 2020년 4월 3일
p=3 ;
k = 1:p ;
val = zeros(1,p) ;
for i = 1:length(p)
%something
if %something
d=d+1
val(i) = d ;
elseif %something
d=d-2
val(i) = d ;
end
end
plot(k,val,'-o')
  댓글 수: 4
KSSV
KSSV 2020년 4월 3일
편집: KSSV 2020년 4월 3일
Show us the full code which gave this result.
Ani Asoyan
Ani Asoyan 2020년 4월 3일
clear all
close all
clc
%defining government's steps
x1=0;
x2=1;
%defining public's steps
x_e1=0;
x_e2=1;
%defining reforms
N1=0;
N2=1;
%combinations
A=[x1, x2 ];
B=[N1, N2];
C=[x_e1, x_e2];
[m,n,p] = ndgrid(A,B,C);
Z = [m(:),n(:),p(:)];
p=3
d=0.5
k=1:p;
val=zeros(1,p);
for i=1:length(p)
%parameterizing function of government's payoff
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));
%linking government's payoff function to data
g=(u_g(Z(:,1),Z(:,3), Z(:,2) ).');
%linking public's payoff function to data
p=(u_p(Z(:,1), Z(:,3), Z(:,2),d).');
gp=reshape(g,[4,2]).' ;%government's payoff matrix
pp=reshape(p, [4,2]).' ;%public's payoff matrix
num2strcell = @(m) arrayfun(@num2str, m, 'UniformOutput', false); %function to convert matrix to cell array of strings
payoffs=strcat(num2strcell(gp), num2strcell(pp))
n=4; m=2;
for i=1:n
A(i)=max(pp(:,i));
attainedA=(max(pp, [] ,1)==pp);
end
for v=1:m
if all(A==pp(v,:))
fprintf('a dominant strategy for public is %d\n',v');
else fprintf ('No dominant strategy for public');
end
end
for j=1:m
B(j)=max(gp(j,:));
attainedB=( max(gp,[],2)==gp );
end
for l=1:n
if all (B==gp(:,l))
fprintf('a dominant strategy for government is %d\n',l');
else fprintf ('No dominant strategy for government');
end
end
result = payoffs(attainedA & attainedB)
gov=gp(attainedA & attainedB)
pub=pp(attainedA & attainedB)
T = array2table(payoffs,'VariableNames',{'X1_N1','X2_N1','X1_N2','X2_N2'},'RowNames',{'x_e1','x_e2'})
if (gov==gp(1,1) || gov==gp(2,1) || gov==gp(1,2) || gov==gp(2,2) )
d=d-0.2
val(i)=d;
elseif (gov==gp(1,3) || gov==gp(2,3) || gov==gp(1,4) || gov==gp(2,4) )
d=d+3
val(i)=d
end
d
end
plot(k,val,'-o')
and there is also one function
function u=u_p(x,x_e,N,d)
u = -(x-x_e).^2+N.*d;
end

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by