関数ハンドルまたは m ファイルで定義された関数の導関数を代数的に取得する方法はありません。いくつかの点で関数を評価し、導関数を近似することにより、これを数値的に行う必要があります。
おそらくやりたいことは、シンボリック方程式の微分であり、そのためにはSymbolic Math Toolboxが必要です。ニュートン・ラフソン法を使用して根を見つける例を次に示します。
>> syms x %# Create a symbolic variable x
>> f = (x-4)^2-4; %# Create a function of x to find a root of
>> xRoot = 1; %# Initial guess for the root
>> g = x-f/diff(f); %# Create a Newton-Raphson approximation function
>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the initial guess
xRoot =
1.8333
>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the refined guess
xRoot =
1.9936
>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the refined guess
xRoot =
2.0000
xRoot
数回の反復の後、 の値が真の根の値 (2) に近づくことがわかります。また、関数の評価を while ループに配置して、それぞれの新しい推測と前の推測の差がどれだけ大きいかをチェックし、その差が十分に小さいとき (つまり、ルートが見つかったとき) に停止することもできます。
xRoot = 1; %# Initial guess
xNew = subs(g,'x',xRoot); %# Refined guess
while abs(xNew-xRoot) > 1e-10 %# Loop while they differ by more than 1e-10
xRoot = xNew; %# Update the old guess
xNew = subs(g,'x',xRoot); %# Update the new guess
end
xRoot = xNew; %# Update the final value for the root