【问题标题】:How to convrt 1x1 sym to function/expression?如何将 1x1 符号转换为函数/表达式?
【发布时间】:2019-09-15 05:42:37
【问题描述】:

您可以在下面找到已实现的牛顿方法。

function y = NewtonRoot(Fun, DFun, Xest,Err, imax)
%Fun - function
%DFun- derivative of F
%Xest - initial estimate of solution
%Err - maximum error
%y - solution

%EXAMPLE: NewtonRoot(@(x)x^2-4,@(x)2*x,1.3, 0.001, 100)

for i= 1: imax
    Xi = Xest - feval(Fun,Xest)/feval(DFun,Xest);
    if abs((Xi-Xest)/Xest) < Err
        y = Xi;
        break
    end
    Xest= Xi;
end

if i== imax
    fprint('Solution was not obtained in %i iterations.\n', imax)
    y=('No answer');
end

它正在工作:

NewtonRoot(@(x)x^2-4,@(x)2*x,1.3, 0.001, 100)

但事实上我想计算一个更复杂函数的导数。 因此,我尝试使用 diff 功能,但它不工作......你能帮帮我吗?

这是我的试探性:

syms y(x) x
y=@(x)x^2-4
dy = diff(y,x)

NewtonRoot(y,@(x)diff(y,x),1.3, 0.001, 100)

【问题讨论】:

    标签: matlab type-conversion diff derivative function-handle


    【解决方案1】:

    您可以使用matlabFunction 函数,该函数允许将符号表达式转换为函数句柄。因此,对于这个例子:

    syms y(x) x
    y=@(x)x^2-4;
    dy = diff(y,x);
    
    NewtonRoot(y, matlabFunction( diff(y,x)), 1.3, 0.001, 100)
    

    这显然效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多