How to write/code several functions as one

后端 未结 1 1930
盖世英雄少女心
盖世英雄少女心 2020-12-22 13:27

I am trying to write a line composed of two segments as a single equation in \"\":

y = m1*x + c1 , for x<         


        
相关标签:
1条回答
  • 2020-12-22 13:54

    You can write this equation as a single line by using the Heaviside step function, https://en.wikipedia.org/wiki/Heaviside_step_function.

    Combining two functions into one:

    In fact, what you are trying to do is

    f(x) = a(x)   (for x < x1)
    f(x) = q      (for x = x1), where q = a(x1) = b(x1)
    f(x) = b(x)   (for x > x1)
    

    The (half-maximum) Heaviside function is defined as

    H(x) = 0      (for x < 0)
    H(x) = 0.5    (for x = 0)
    H(x) = 1      (for x > 0)
    

    Hence, your function will be

    f(x) = H(x1-x) * a(c) + H(x-x1) * b(x)
    

    and, therefore,

    f(x) = H(x1-x) * (m1*x+c1) + H(x-x1) * (m2x+c2)
    

    If you want to implement this, note that many programming languages will allow you to write something like

    f(x) = (x<x1)?a(x):b(x)
    

    which means if x<x1, then return value a(x), else return b(x), or in your case:

    f(x) = (x<x1)?(m1*x+c1):(m2x+c2)
    

    Matlab implementation:

    In Matlab, you can write simple functions such as

    a = @(x) m1.*x+c1,
    b = @(x) m2.*x+c2,
    

    assuming that you have previously defined m1, m2, and c1, c2.

    There are several ways to using/implementing the Heaviside function

    1. If you have the Symbolic Math Toolbox for Matlab, you can directly use heaviside() as a function.
    2. @AndrasDeak (see comments below) pointed out that you can write your own half-maximum Heaviside function H in Matlab by entering

      iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}();
      H = @(x) iif(x<0,0,x>0,1,true,0.5);
      
    3. If you want a continuous function that approximates the Heaviside function, you can use a logistic function H defined as

      H = @(x) 1./(1+exp(-100.*x));
      

    Independently of your implementation of the Heaviside function H, you can, create a one-liner in the following way (I am using x1=0 for simplicity) :

    a = @(x) 2.*x + 3;
    b = @(x) -1.5.*x + 3;
    

    Which allows you to write your original function as a one-liner:

    f = @(x) H(-x).*a(x) + H(x).*b(x);
    

    You can then plot this function, for example from -10 to 10 by writing plot(-10:10, f(-10:10)) you will get the plot below.

    Generalization:

    Imagine you have

    f(x) = a(x)   (for x < x1)
    f(x) = q      (for x = x1), where q = a(x1) = b(x1)
    f(x) = b(x)   (for x1 < x < x2)
    f(x) = r      (for x = x2), where r = b(x2) = c(x2)
    f(x) = c(x)   (for x2 < x < x3)
    f(x) = s      (for x = x2), where s = c(x3) = d(x3)
    f(x) = d(x)   (for x3 < x)
    

    By multiplying Heaviside functions, you can now determine zones where specific functions will be computed.

    f(x) = H(x1-x)*a(c) + H(x-x1)*H(x2-x)*b(x) + H(x-x2)*H(x3-x)*c(x) + H(x-x3)*d(x)
    

    PS: just realized that one of the comments above talks about the Heaviside function, too. Kudos to @AndrasDeak .

    0 讨论(0)
提交回复
热议问题