在 Octave 中创建和绘制分段函数

Create and plot a piecewise function in Octave

所以我想绘制这个函数

为-1

首先我创建了分段函数

function x = pieceWise(t)
  if t >= 0 & t <3
    x = exp(-t);
  else
    x = 0;
endif

然后在这里调用并绘制它

x = linspace(-1,5,1000);
y = pieceWise(x);
plot(x,y)

但输出始终为 0

任何提示都会很有帮助。谢谢

你应该向量化你的代码:

1;
function x = pieceWise(t)
  x = zeros (size (t));
  ind = t >= 0 & t < 3;
  x(ind) = exp(-t(ind));
endfunction

x = linspace (-1, 5, 1000);
y = pieceWise (x);
plot (x, y)
grid on
print out.p

给予