2012-04-30 45 views
0

好吧,這聽起來很容易,但無論我嘗試了多少次,仍然無法正確繪製它。我只需要在同一個圖表上有三行,但仍然有問題。在FOR循環中繪製多行代碼MATLAB

iO = 2.0e-6; 
k = 1.38e-23; 
q = 1.602e-19; 


for temp_f = [75 100 125] 
    T = ((5/9)*temp_f-32)+273.15; 
     vd = -1.0:0.01:0.6; 
     Id = iO*(exp((q*vd)/(k*T))-1); 
     plot(vd,Id,'r',vd,Id,'y',vd,Id,'g'); 
     legend('amps at 75 F', 'amps at 100 F','amps at 125 F');  

end;  

ylabel('Amps'); 
xlabel('Volts'); 
title('Current through diode'); 

現在我所知道的繪圖功能是目前在他們不工作,一些類型的變量,需要建立像(VD,ID1,「R」,VD,ID2,「Y」,VD, ID3, 'G');但是我真的無法理解改變它的概念,我正在尋求幫助。

回答

3

您可以使用「hold on」功能使每個繪圖命令與最後一個窗口在同一個窗口上繪製。

跳過for循環會更好,只需一步完成。

iO = 2.0e-6; 
k = 1.38e-23; 
q = 1.602e-19; 

temp_f = [75 100 125]; 
T = ((5/9)*temp_f-32)+273.15; 

vd = -1.0:0.01:0.6; 
% Convert this 1xlength(vd) vector to a 3xlength(vd) vector by copying it down two rows. 
vd = repmat(vd,3,1); 

% Convert this 1x3 array to a 3x1 array. 
T=T'; 
% and then copy it accross to length(vd) so each row is all the same value from the original T 
T=repmat(T,1,length(vd)); 

%Now we can calculate Id all at once. 
Id = iO*(exp((q*vd)./(k*T))-1); 

%Then plot each row of the Id matrix as a seperate line. Id(1,:) means 1st row, all columns. 
plot(vd,Id(1,:),'r',vd,Id(2,:),'y',vd,Id(3,:),'g'); 
ylabel('Amps'); 
xlabel('Volts'); 
title('Current through diode'); 

而且應該得到你想要的。

+0

問題是我必須爲問題使用for循環,仍然在基本編程沒有複雜。上述的繪圖方法是否工作? – user1364968