Use the Function function in matlab and write a piecewise function

Introduction to Function in mathwork: https://ww2.mathworks.cn/help/matlab/ref/function.html

For example

Implement the following piecewise functions:
insert image description here
the drawn image is shown in the figure and
insert image description here
the code is as follows

先建立一个函数文件并输入以下代码:
function m=fenduan(t)    % 信号mt
to = 0.15;
m = 1.*(t>=0 & t<to/3)+(-2).*(t>to/3 & t<=2*to/3)+0.*(t<0 | t>2*to/3);
end

然后在命令行输入:
t=-0.1:0.001:0.2; 
mt = fenduan(t); 
plot(t,mt); 
axis([-0.02 0.12 -2.5 2.5])

Guess you like

Origin blog.csdn.net/qq_44961737/article/details/105725404