绘制分段连续函数

Hah*_*ahn 3 matlab plot

我想使用以下代码绘制不连续的分段函数.但是,输出始终显示为连续函数,因为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]);
Run Code Online (Sandbox Code Playgroud)

我该如何以一种不那么复杂的方式解决这个问题?

PS.我正在使用MATLAB 2013a,似乎piecewise该版本中不存在该功能.

Adr*_*aan 9

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]);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

导致相同图形的另一个选项是使用hold on以下方法分别绘制所有三个图形:

figure;
hold on
plot(i1,f1,'b');
plot(i2,f2,'b');
plot(i3,f3,'b');
Run Code Online (Sandbox Code Playgroud)

或者使用plot(X,Y,X1,Y1,...,Xn,Yn语法:

figure;
plot(i1,f1,'b',i2,f2,'b',i3,f3,'b')
Run Code Online (Sandbox Code Playgroud)

请注意,对于后两者,您必须指定线条样式,以防止MATLAB使它们成为单独的颜色,因此'b'.