gpt4 book ai didi

matlab - 绘制分段连续函数

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:25 25 4
gpt4 key购买 nike

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

enter image description here

产生相同图形的另一种选择是使用 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'

关于matlab - 绘制分段连续函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53557317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com