2010-12-05 52 views
0

我想知道如何從間隔中抓取一個特定的數字來測試它,然後可以在一個圖形下構建不同的功能。例如(在這種情況下,變量「x」),如何在MATLAB中對圖的因變量執行語句?

x 0:.5:5; 

Ids=ones(x); 
figure;hold on; 

for n = 1:5 
    if(x < 3.0) %problem here 
     Ids(n) = plot(x,x.^x); 
    else 
     if (x > 4.0) %and here 
      Ids(n) = plot(x,-x.^x); 
     end 
    end 
end 

編輯

我真的想在MATLAB做的是能夠做到以下分段函數:

y(x) = { 0     (t - 5) < 0 
     { (t - 5)*(t - x)  x < (t - 5) 
     { (t + x^2)   x >= (t - 5) 

自從x = 0:.5:10t = 0:.1:10以來,我似乎不知道如何繪製此功能。我知道如何在沒有t的情況下做到這一點,但當包含t並且與x相比有不同的間隔時,我會迷路。

+0

Y_Y,你能澄清你想做什麼?正如gnovice寫道的,從你的代碼中不清楚你想要做什麼。你能用'文字'來寫你想做什麼嗎? gnovice的答案中給出的函數f(x)是你要找的嗎? – 2010-12-06 04:52:04

回答

1

這是從你的代碼有點不清楚你正在嘗試做的,但現在看來,要建立並繪製函數f(x)具有以下形式:

f(x) = [ x  for 3 <= x <= 4 
     [ x^x for x < 3 
     [ -x^x for x > 4 

如果這是你想要的這樣做,你可以做以下使用logical indexing

x = 0:0.5:5; %# 11 points spaced from 0 to 5 in steps of 0.5 
y = x;  %# Initialize y 
index = x < 3;     %# Get a logical index of points less than 3 
y(index) = x(index).^x(index); %# Change the indexed points 
index = x > 4;     %# Get a logical index of points greater then 4 
y(index) = -x(index).^x(index); %# Change the indexed points 
plot(x,y);      %# Plot y versus x 
1

你可能會尋找分段多項式:否則http://www.mathworks.com/help/techdoc/ref/mkpp.html

,我會建議做兩個向量,「X」和「Y」,可以這麼說,並通過X迭代和應用的條件下灌裝y和結果,然後將y繪製在x上。這將避免需要保持陰謀。

如果要爲圖形制作動畫,請將plot()添加到for循環,然後加上「drawnow」。這已經有一段時間了,因爲我不得不對動畫進行動畫處理,所以我會建議用於汲取和動畫的教程。

+0

如何使用分段函數'mkpp'來做到這一點? – 2010-12-05 09:26:43

相關問題