Re: [問題] 用plot作圖 但是圖跑不出來

作者: sppmg (sppmg)   2018-06-29 02:47:29
就以下程式改寫。
看起來你誤會 plot 是打點上去,並且不會清除。
: for x=linspace(-2,6)
: if x<-1
: plot(x,exp(x+1));
: elseif -1<=x<5
: plot(x,2+cos(pi*x));
: elseif x<=5
: plot(x,10*(x-5));
: end
: end
matlab 中可以用:
x = linspace(-2,6)
y(numel(x)) = 0 ; % 預配置記憶體空間
% 用 logical array 標記選定元素
bmap = { ...
x < -1 , ...
-1 <= x & x < 5, ... % 用 & 而非 && ,因為兩邊是 logical array
x > 5}; % 你這裡是不是打錯成 < ?
% 儲存 function handle,方便迴圈呼叫、動態呼叫
func = { ...
@(x)exp(x+1), ...
@(x)2+cos(pi*x), ...
@(x)10*(x-5) } ;
for n = 1:numel(bmap)
% 把選定區段經計算後存到 y 的相同位置。
y(bmap{n}) = func{n}( x(bmap{n}) ) ;
end
plot(x,y) ;
作者: sunev (Veritas)   2018-06-29 09:24:00
作者: guahuan (寡歡的鬱鬱)   2018-06-29 17:02:00
謝謝你!!!

Links booklink

Contact Us: admin [ a t ] ucptt.com