【问题标题】:is there a way to do implicit differentiation in matlab有没有办法在matlab中进行隐式微分
【发布时间】:2015-02-16 15:30:42
【问题描述】:

我经常使用 matlab 来帮助我解决数学问题。

现在我正在寻找一种在 matlab 中进行隐式微分的方法。 例如,我想区分 y^3*sin(x)+cos(y)*exp(x)=0dy/dx

我知道如何通常使用数学方法来做到这一点,但我一直在努力寻找使用 matlab 的简单方法。当我需要正态微分(从 f(x) 中找到微分)时,我使用了符号数学工具箱并做了这样的事情:

syms x
y = myfunctionOf(x)
diff(y)

我查看了doc diff 并在符号工具箱中进行了快速查找,但没有发现对上述案例有帮助。但是我就是不相信matlab没有这么简单的功能。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    这里有一些代码可以做你想做的事情,所有解释都在 cmets 中,注意这段代码假设你希望 Matlab 为你做几乎所有的数学思考。

    %// Firstly you need to define a function `f` in terms of `x` and `y`. 
    syms x y;
    f = y^3*sin(x)+cos(y)*exp(x);
    
    %// Then you need to tell Matlab that y is a function of x,
    %// you do this by replacing y with y(x)
    yOfx = sym('y(x)');
    f_yOfx = subs(f, y, yOfx);
    
    %// Then you need to differentiate with respect to x
    df = diff(f_yOfx, x);
    
    %// df will have diff(y(x), x) terms in it, 
    %// we want to solve for this term, 
    %// to make it easier we should first replace it with a variable
    %// and then solve
    syms Dy;
    df2 = subs(df, diff(yOfx, x), Dy);
    dyOver_dx = solve(df2, Dy);
    
    %// Finally if we do not want all of the y(x) terms, 
    %// then replace them with y
    dyOver_dx = subs(dyOver_dx, yOfx, y)
    

    当然,如果我们不介意做一些文书工作,我们可以得到dy/dx = -(partial f/partail x)/(partial f/partial y),从中我们可以得到更短的代码

    %// Implicit differentiation identity
    also_dyOver_dx = -diff(f, x)/diff(f, y);
    

    这里检查两个答案是否相同。

    simplify(dyOver_dx - also_dyOver_dx) %// == 0
    

    【讨论】:

      【解决方案2】:

      最好的方法总是最简单的!

      syms x y(x)
      f = y^3*sin(x)+cos(y)*exp(x);
      diff(f,x)
      

      你也可以包含漂亮的命令以获得更好的可视化!

      pretty(ans)  %"Pretty print" output
      

      另外,还有一种方法:

      syms x y f
      f = y^3*sin(x)+cos(y)*exp(x);
      -diff( f, x )/diff( f, y )
      pretty(ans)  %"Pretty print" output
      

      尽情享受吧!

      【讨论】:

        【解决方案3】:

        您可以尝试使用:

        diff(expr, sym('v')) //This differenciates the expression respect to v
        

        【讨论】:

        • 这是完全错误的。这执行微分,认为所有其他变量都是常数。但这种情况并非如此。例如,我想区分 x^2+y^2 =0。我应该收到的是 2*x + 2*y*y' = 0,但是根据你的表达,这将是 2*x
        猜你喜欢
        • 2019-10-22
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        相关资源
        最近更新 更多