2010-12-05 133 views
3

我是MATLAB新手,所以我甚至不知道這是否可能,但這裏是... 我想在一個圖中使用繪圖函數打印多行。問題是,我希望能夠指定多少線圖中應顯示通過簡單地改變一個變量,例如:{這是僞代碼,我想要做}如何在MATLAB數組中保存多個函數?

number_of_lines = 4; 
x = 0:0.5:5; 

function_output[number_of_lines]; 

for n=0:number_of_lines 
    function_output[n] = sin(n + x); 
end 

for n=0:number_of_lines 
    plot(x,function_output[n]); 
end 

我知道上面的僞代碼並不完全是MATLAB,但我只想知道這樣的算法是否可以在MATLAB中完成。

回答

2

下面就來實現MATLAB的例子一個辦法:

function_output = zeros(numel(x), number_of_lines); % Initialize a 2-D array 
for n = 1:number_of_lines     % MATLAB uses 1-based indexing 
    function_output(:, n) = sin(n + x).'; %' Compute a row vector, transpose 
              % it into a column vector, and 
              % place the data in a column of 
              % the 2-D array 
end 
plot(x, function_output); % This will plot one line per column of the array 

這裏有一些文檔鏈接,你應該通讀學習和理解上面的代碼:

+0

感謝您的帖子。 – 2010-12-05 07:12:05

1

您是否看過MATLAB手冊? - 很多例子都寫得很好。複製示例腳本,並將其粘貼到命令窗口,看看會發生什麼......

http://www.mathworks.com/help/techdoc/creating_plots/f9-53405.html

您可以編寫腳本或者使用他們的繪圖工具: http://www.mathworks.com/help/techdoc/creating_plots/f9-47085.html

---腳本示例

number_of_lines = 4;

x = 0:0.5:5;

function_output = ones(number_of_lines,1)* nan;

figure; hold on;

對於n = 1:NUMBER_OF_LINES

function_output(N,1)=積(X,SIN(N + X), '顏色',[1-N/NUMBER_OF_LINES 0 N/NUMBER_OF_LINES]);

傳說(function_output)

有樂趣。

+0

問題:如果我只想在x是3時繪製圖形,應該如果if語句是if(x == 3)? – 2010-12-05 07:17:22