Plot inside a for loop

21 ビュー (過去 30 日間)
Yogesh Mangar
Yogesh Mangar 2012 年 3 月 31 日
hi all, I need your help. I am trying to plot a graph inside a for loop but only dots is appearing on the plot. the dots are not link with each other here is my code:
how can I make a clear line graph with all the dots joined. Help me please its urgent!!!
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 4 月 2 日
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency

サインインしてコメントする。

採用された回答

Kostas
Kostas 2012 年 4 月 1 日
well, it is like that because you don't create the x vector,it is just a value. You could try one of the both
x=0:1:250; %create x vector
x=x'; % transpose to be a column vector
s=size(x,1); % size of x
for i=1:s %loop over all values of x
a(i)=x(i)*...;
b(i)=x(i)*...;
.
y(i)=a(i)+b(i);
end
plot(x,y)
or instead of loop create a and b vectors direct
x=0:1:250; %create x vector
x=x'; % transpose to be a column vector
a=x*...;
b=x*...;
y=a+b;
plot(x,y)
  1 件のコメント
Yogesh Mangar
Yogesh Mangar 2012 年 4 月 4 日
thanks

サインインしてコメントする。

その他の回答 (1 件)

saima
saima 2012 年 3 月 31 日
because each time loop runs your value of x and y becomes a scaler. try to use other variable than x and try to accumulate the values and then use the plot outside the loop
  2 件のコメント
saima
saima 2012 年 3 月 31 日
can help you elaborately if you explain your code in a little detail
Kostas
Kostas 2012 年 3 月 31 日
try something like this
k=0; %initialize k
for x = 0:250
.
k=k+1 %raise k value +1 in each iteration
a(k) = x * ....;
b(k) = x * ....;
.
y(k) = a + b; % accumulate all values to the vector y, has the same size like x vector
end
x=x(:);
plot(x,y) %x and y are vectors

サインインしてコメントする。

カテゴリ

Help Center および 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