How to plot this matrix?

조회 수: 2 (최근 30일)
Vidhan Malik
Vidhan Malik 2016년 2월 13일
댓글: Geoff Hayes 2016년 2월 14일
I am trying to plot two different lines on the same graph and am having some trouble. This is the code i have written so far:
n = 16.7185;
for i = 1:1:40
n = n+0.1;
T2 = T1*n^l;
T4 = T3/n^l;
efficiency = 1-1/n;
m(i,2)= W/(T3-T4-T2+T1)/Cp;
q(i,2) = W/efficiency;
plot(i,m(i,2));
plot(i,q(i,2));
hold all
end
hold off
grid on
The values for l,W,T1 and T3 are predefined and I am getting no erroes. Where exactly am I going wrong? When i try to see the plot it comes out as blank and when I go into the detailed view to modify the axes I see that it is trying to plot a line for each data point but I don't understand why it would do that.
  댓글 수: 1
Vidhan Malik
Vidhan Malik 2016년 2월 13일
편집: Geoff Hayes 2016년 2월 13일
Changed code to this but still getting blank graph
n = 16.7185;
for i = 1:1:40
n = n+0.1;
T2 = T1*n^l;
T4 = T3/n^l;
efficiency = 1-1/n;
m(i,1) = n;
q(i,1) = n;
m(i,2)= W/(T3-T4-T2+T1)/Cp;
q(i,2) = W/efficiency;
plot(n,m(i,2));
plot(n,q(i,2));
hold all
end

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

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 2월 13일
Vidhan - what are the values for your predefined variables? If I randomly assign some numbers (to them) then I see two lines. Once the loop has finished, what does the data in the arrays m and q look like?
Note that each time that you call plot, the code is creating a graphics object on the parent figure. Since you are collecting all of your data into two arrays, then you may as well just plot the data once the arrays have been populated. For example,
m = zeros(40,2);
q = zeros(40,2);
n = 16.7185;
for k = 1:1:40
n = n+0.1;
T2 = T1*n^l;
T4 = T3/n^l;
efficiency = 1-1/n;
m(k,1) = n;
q(k,1) = n;
m(k,2)= W/(T3-T4-T2+T1)/Cp;
q(k,2) = W/efficiency;
end
figure;
hold on;
plot(m(:,1),m(:,2),'b.');
plot(q(:,1),q(:,2),'r.');
(Note that I replaced i with k since MATLAB uses i and j to represent the imaginary number. It is good practice to avoid using indexing variables with the same name.)
  댓글 수: 2
Vidhan Malik
Vidhan Malik 2016년 2월 13일
These are my predefined values
y=1.4;
l=(y-1)/y;
T1=300;
T3=1500;
W=500;
Cp=1.004;
I seem to only be able to see one plot though, how do you get the two lines on the same graph?
Geoff Hayes
Geoff Hayes 2016년 2월 14일
Vidhan - even with your pre-defined values, I still see two lines drawn (using the code from above).

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 2월 13일
In http://www.mathworks.com/help/matlab/ref/hold.html Mathworks states that for 'hold all' "This syntax will be removed in a future release. Use hold on instead". Also you are applying it too late to catch the first 'm' point.
In any case it would be better to first accumulate 'm' and 'q' in arrays and then plot them afterwards all at once. You wouldn't need to do a 'hold' at all then.
for i = 1:40
....
end
plot(1:40,m,'r-',1:40,q,'g-') % Plot in red and green

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by