plotting using for loop

2 ビュー (過去 30 日間)
manu vincent
manu vincent 2020 年 12 月 4 日
編集済み: Stephen23 2020 年 12 月 4 日
Hi,
I am new to matlab, i want to plot a curve using for loop but instead a range of values like that obtained without using for loop i am getting only a single value. can some body tell me what i am doing wrong or if i am missing something in my code.
clear
clc
close all
figure(1) ;
for d= 0:0.01:1
if(d==1)
break;
end
vin=20;
vout=(vin*4.*d)./(1.-d);
end
plot(d,vout)
p.s whats the difference between x=[0:0.1:1] and x=0:0.1:1
thanks;
  1 件のコメント
Stephen23
Stephen23 2020 年 12 月 4 日
編集済み: Stephen23 2020 年 12 月 4 日
"whats the difference between x=[0:0.1:1] and x=0:0.1:1"
Good question. In MATLAB square brackets are a concatentation operator (not a "list" operator like in some languages), so they are used to join arrays together. Which mena that in this code the square brackets do nothing useful at all:
x = [0:0.1:1]
% ^ ^ superfluous because nothing is being concatenated together
That is also why the MATLAB Editor shows a warning that the square brackets are superfluous and recommends to remove them. To define a vector of values you only need to use the colon operator:
x = 0:0.1:1

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

回答 (4 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 4 日
編集済み: KALYAN ACHARJYA 2020 年 12 月 4 日
Edited:
For difference, please read the Stephen Cobeldick 's valuable comment.
more
x=[0:0.1:1]
x1=0:0.1:1
x2=(0:0.1:1)
Better to follow x1 ways, it's simplest. Code Part, you can avoid loop here (Recommended)
d=0:0.01:1;
vin=20;
vout=(vin*4*d)./(1-d);
plot(d,vout)
At the max x value is 1, it automatically stop once x reach to it's max, no need of break here.
  2 件のコメント
Stephen23
Stephen23 2020 年 12 月 4 日
"Tecnically, there is no difference."
One calls the concatenation operator with one input (thus concatenating one vector with nothing else), whereas the other does not. Quite a large difference, really, because it indicates that the user does not understand what square brackets actually do and also that they ignore warnings provided by the MATLAB Editor.
Both of which are pretty big hints that the user writes substandard code. Much like CLEAR ALL et al.
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 4 日
Sir Stephen Cobeldick Thanks for your vauable comment. Thank you for teaching us as you always do.

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


KSSV
KSSV 2020 年 12 月 4 日
編集済み: KSSV 2020 年 12 月 4 日
You need not to use a loop to get what you want. Already you are using .* i.e. eleemnt by element operation so , you can proceed with vectors.
close all
figure(1) ;
d= 0:0.01:1 ;
vin=20;
vout=(vin*4.*d)./(1.-d);
plot(d,vout)

Pen-Li (Ben) Yu
Pen-Li (Ben) Yu 2020 年 12 月 4 日
A for loop does not define an array. You need to define d directly.
d= 0:0.01:1
vin=20;
vout=(vin*4.*d)./(1.-d);
figure(1)
plot(d,vout)

Stephen23
Stephen23 2020 年 12 月 4 日
編集済み: Stephen23 2020 年 12 月 4 日
"...can some body tell me what i am doing wrong or if i am missing something in my code."
The main problem is that you are not using indexing to store the output values.
d = 0:0.01:1;
n = numel(d);
vin = 20;
vout = nan(1,n); % preallocate
for k = 1:n % loop over indices
vout(k) = (vin*4.*d(k))./(1.-d(k));
end
plot(d,vout)
For such a simple calculation a loop is not required:
vtwo = (vin*4.*d)./(1.-d);
plot(d,vtwo)

カテゴリ

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