如何在Matlab中制作三分支函数图 [英] How to make a graph of a three-branches function in matlab

查看:192
本文介绍了如何在Matlab中制作三分支函数图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Matlab中制作此函数的图形,以便在同一图形(图或子图)中描绘其主体?

How can I make this function's graph in Matlab, so that its body is depicted in the same graph (plot or subplot)?

    t0=0.15 
    x(t)= 1, if 0<=t<(t0/2) 
         -2, if (t0/2)<=t<=(3/2)*t0 
          0, else

推荐答案

您真正要问的问题是如何定义具有分支的函数?",因为一旦定义了函数,绘制就很容易.

The real question you should be asking is "How to define a function that has branches?", since plotting is easy once the function is defined.

  1. 这是一种使用匿名函数的方法:

x_t = @(t,t0)1*(0<=t & t<t0/2)-2*(t0/2<=t & t<=(3/2)*t0); %// the 1* is redundant, I only
                                                          %// left it there for clarity

请注意, &运算符需要数组而不是标量.

Note that the & operator expects arrays and not scalars.

这是使用 heaviside(又称步骤)函数(由于它在过渡点上的行为,因此并不是您想要的,但值得一提):

Here's a way using heaviside (aka step) functions (not exactly what you wanted, due to its behavior on the transition point, but worth mentioning):

x_t = @(t,t0)1*heaviside(t)+(-1-2)*heaviside(t-t0/2)+2*heaviside(t-t0*3/2);

请注意,在这种情况下,一旦离开有效范围,就需要抵消"先前的heaviside.

Note that in this case, you need to "negate" the previous heaviside once you leave its area of validity.

定义此函数后,只需评估并绘制即可.

After defining this function, simply evaluate and plot.

t0 = 0.15;
tt = -0.1:0.01:0.5;
xx = x_t(tt,t0);
plot(tt,xx); %// Or scatter(), or any other plotting function

顺便说一句,t0不必是x_t的输入-如果在x_t之前定义,则将捕获并使用当时在工作空间中存在的t0的值,但是这也意味着,如果以后更改t0,则不会影响x_t.

BTW, t0 does not have to be an input to x_t - if it is defined before x_t, the value of t0 that exists in the workspace at that time will be captured and used, but this also means that if t0 changes later, this will not affect x_t.

这篇关于如何在Matlab中制作三分支函数图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆