【问题标题】:Generating piecewise functions in MATLAB R2016A without SMT在没有 SMT 的 MATLAB R2016A 中生成分段函数
【发布时间】:2017-02-19 04:09:07
【问题描述】:

在符号数学工具箱中使用不分段的 r2016a。希望分段使用以下方法,但使用各种方法都失败了。预先感谢您的建议。

功能:

 (3*x^2)/100 + 30   if x<22.3607    
 x^2/100 + 40       if 22.3607<=x<=109.5445    
 (3*x^2)/400 + 70   if x>109.5445  

选项 1) SMT 可能允许:

y(x) = piecewise([x<22.3607, (3*x^2)/100 + 30], [22.3607<=x<=109.5445, x^2/100 + 40], [x>109.5445, (3*x^2)/400 + 70)]);

结果:

未定义的函数或变量'分段'。

选项2)创建函数:

%%%%%%%%%%%%%%%%%%%%%
function y = y(x)
   if x<=22.3607;
       y = (3*x^2)/100 + 30;
   else if 22.3607<x<=109.5445;
       y = x^2/100 + 40;
   else if 109.5445<x;
       y = (3*x^2)/400 + 70;
       end
       end
   end
%%%%%%%%%%%%%%%%%%%%%

结果:

q_piecewise 输入参数不足。

q_piecewise 中的错误(第 3 行) 如果 x

选项 3) 使用重型:

y = ['(heaviside(x)-heaviside(x-22.3607))*((3*x^2)/100 + 30) + ' ...
 '(heaviside(x-22.3607)-heaviside(x-109.5445))*(x^2/100 + 40) + ' ...
 '(heaviside(x-109.5445)-heaviside(x-800))*((3*x^2)/400 + 70)'];

yinv = finverse(y,x)

结果(试图计算逆):

yinv = 10.0*(x - 40.0)^(1/2)

我希望能够将 x 的值输入这个分段方程并接收 y 的值。理想情况下,我也希望对上述分段函数的逆执行此操作。另外,我想计算 diff() 和 int(),所以我相信符号函数最适合这个。想法?谢谢!!

  • 布莱恩

【问题讨论】:

    标签: piecewise


    【解决方案1】:

    来自沃尔特·罗伯森:

    在 R2016b 之前没有分段的 MATLAB 接口。可以使用 evalin(symengine) 在 mupad 级别构造分段对象,但不能创建它的函数。

    f = evalin(symengine,'piecewise([x<5,x^2],[x>=5,-x])')
    

    你可以 subs() 到 f 或者你可以 int(f, x)

    在 MATLAB 级别通过程序构建分段需要一些不明显的步骤。我需要进行一些搜索才能找到我在哪里发布了所需的帮助函数...

    找到了。

    http://www.mathworks.com/matlabcentral/answers/309163-can-i-use-numeric-odesolve-as-a-replacement-to-ode45#answer_240756

    您需要构造列表的辅助函数,并且您需要 feval(symengine, 'piecewise',LIST,LIST,...)

    whete LIST 是由辅助函数构造的。

    【讨论】:

      【解决方案2】:

      或者,来自 Star Strider:

      y = @(x) ((3*x.^2)/100 + 30) .* (x<22.3607) +  (x.^2/100 + 40) .* ((22.3607<=x) & (x<=109.5445)) + ((3*x.^2)/400 + 70) .* (x>109.5445);
      
      t = linspace(0, 150, 500);
      
      figure(1)
      plot(t, y(t))
      grid
      xlabel('t')
      ylabel('y(t)')
      title('Original Fincton')
      
      yi = linspace(min(y(t)), max(y(t)), 20);                    % Interpolate Inverse
      ti = interp1(y(t),t, yi, 'linear');
      
      figure(2)
      plot(y(t), t)                                               % Plot Inverse
      hold on
      plot(yi, ti, 'pg', 'MarkerFaceColor','g')                   % Inverse Interpolated Values
      hold off
      grid
      xlabel('y(t)')
      ylabel('t')
      title('Inverse Function')
      

      它利用逻辑索引,然后只添加向量的片段来创建一条连续的线。

      反函数与 interp1 函数一起使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多