Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

plot values in the loop

14 Ansichten (letzte 30 Tage)
Ani Asoyan
Ani Asoyan am 3 Apr. 2020
Geschlossen: darova am 3 Apr. 2020
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 Kommentare
Mehmed Saad
Mehmed Saad am 3 Apr. 2020
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 am 3 Apr. 2020
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

Antworten (2)

Jakob B. Nielsen
Jakob B. Nielsen am 3 Apr. 2020
do
plot(k,d,'o-')
instead, that will give you o's connected by lines.
  2 Kommentare
Mehmed Saad
Mehmed Saad am 3 Apr. 2020
i don't think so sir
it wont
try it
Ani Asoyan
Ani Asoyan am 3 Apr. 2020
sorry it didn't work

KSSV
KSSV am 3 Apr. 2020
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 Kommentare
KSSV
KSSV am 3 Apr. 2020
Bearbeitet: KSSV am 3 Apr. 2020
Show us the full code which gave this result.
Ani Asoyan
Ani Asoyan am 3 Apr. 2020
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

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by