如何将多种功能合而为一 [英] How to write/code several functions as one

查看:81
本文介绍了如何将多种功能合而为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在:

y = m1*x + c1 , for x<=x1
y = m2*x + c2 , for x>=x1

我的问题是:

  1. 如何将这条组合线的函数写为一​​个方程?
  2. 如何将多个函数(在线性参数空间的各个区域中有效)写为一个方程式?

请同时说明如何数学表达以及如何在总体上(特别是在Matlab中)对此进行编程.

Please explain both how to express this mathematically and how to program this in general and in Matlab specifically.

推荐答案

您可以使用Heaviside步进函数

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

将两个功能组合为一个:

实际上,您要尝试的是

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)

(最大半数)Heaviside函数定义为

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)

因此,您的功能将会

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

,因此,

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)

表示如果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实现:

在Matlab中,您可以编写简单的函数,例如

In Matlab, you can write simple functions such as

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

假设您先前已定义m1m2c1c2.

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

有多种使用/实现Heaviside功能的方法

There are several ways to using/implementing the Heaviside function

  1. 如果您有Matlab的Symbolic Math Toolbox,则可以直接使用heaviside()作为函数.
  2. @AndrasDeak(请参见下面的评论)指出,您可以通过输入

  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);

  • 如果您想要一个类似于Heaviside函数的连续函数,则可以使用定义为

  • 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));
    

  • 独立于Heaviside函数H的实现,您可以通过以下方式创建单层(为简单起见,我使用x1=0):

    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);
    

    然后您可以编写此函数,例如,通过编写plot(-10:10, f(-10:10))从-10到10进行绘制,您将在下面得到该图.

    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.

    概括:

    想象你有

    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)
    

    通过乘以Heaviside函数,您现在可以确定将要计算特定函数的区域.

    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:刚刚意识到上面的评论之一也谈到了Heaviside函数. @AndrasDeak表示敬意.

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

    这篇关于如何将多种功能合而为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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