我想使用以下代码绘制一个不连续的分段函数。但是,输出始终显示为连续函数,因为 MATLAB 加入了这些子函数之间的间隙。
i1 = -2:0;
i2 = 0:pi/2;
i3 = pi/2:pi;
f1 = sinh(i1)+2;
f2 = sin(i2)-2;
f3= 2*i3.^2-2*pi*i3+3;
plot([i1 i2 i3],[f1,f2,f3]);
我应该如何以不那么复杂的方式解决这个问题?
附言。我正在使用 MATLAB 2013a,似乎函数 piecewise
此版本中不存在。
在函数之间添加 nan
,这将分离它们:
i1 = -2:0;
i2 = 0:pi/2;
i3 = pi/2:pi;
f1 = sinh(i1)+2;
f2 = sin(i2)-2;
f3= 2*i3.^2-2*pi*i3+3;
plot([i1 nan i2 nan i3],[f1,nan,f2,nan,f3]);
产生相同图形的另一种选择是使用 hold on
分别绘制所有三个图形:
figure;
hold on
plot(i1,f1,'b');
plot(i2,f2,'b');
plot(i3,f3,'b');
或者使用plot(X,Y,X1,Y1,...,Xn,Yn
语法:
figure;
plot(i1,f1,'b',i2,f2,'b',i3,f3,'b')
请注意,对于后两者,您必须指定线条样式,以防止 MATLAB 将它们设为不同的颜色,因此 'b'
。
我是一名优秀的程序员,十分优秀!