필터 지우기
필터 지우기

Plotting data from for loop

조회 수: 1 (최근 30일)
Alec Carruthers
Alec Carruthers 2017년 4월 4일
댓글: KSSV 2017년 4월 5일
Here is my code:
T=290;
R=287;
G=1.4;
Cv= R/(G-1);
e=Cv*T;
V= (0:1:1000);
Ke= .5*(V.^2);
for i = 1:1:length(V)
Ratio(i)= Ke(i)/e;
plot(Ratio);
hold on
end
Basically I want to plot "Ratio" with all of the stored Ke values on the graph. When I run the code, it gives me a thousand sets of a thousand entries instead of just 1 set with a thousand entries. All of the sets are the same, I just don't know why it is producing that many sets.

답변 (1개)

KSSV
KSSV 2017년 4월 4일
It is because, you are plotting Ratio inside the loop and you have not mentioned the X data. You can plot Ratio outside the loop.
T=290;
R=287;
G=1.4;
Cv= R/(G-1);
e=Cv*T;
V= (0:1:1000);
Ke= .5*(V.^2);
Ratio = zeros(size(V)) ;
for i = 1:1:length(V)
Ratio(i)= Ke(i)/e;
end
plot(Ratio);
It can be vectorised without loop as below:
% Vector
Ratio = Ke/e ;
  댓글 수: 2
Alec Carruthers
Alec Carruthers 2017년 4월 4일
Thank you for the help. Your code gives me the same data, but it is only one set instead of a thousand.
KSSV
KSSV 2017년 4월 5일
How it can be thousand?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by