[英]Multiple anonymous function MATLAB
我的目標是計算函數的導數並將此結果用作另一個函數。 顯然,這意味着:
f = @(x) (x-1)*(x-2); %A simple function
derivative = jacobian(f,x) %MATLAB output : "2*x - 3"
df = @(x) derivative %= @(x) 2*x - 3
df(2) %= "2*x -3" instead of 2*2 - 3
我該怎么辦? 我嘗試了syms x,但它沒有幫助。
你想要matlabFunction
:
g = matlabFunction(f)
將符號表達式或函數f
轉換為帶句柄g
的MATLAB函數。
在你的例子中:
>> syms x
>> f = @(x) (x-1)*(x-2);
>> derivative = jacobian(f,x);
>> df = matlabFunction(derivative)
df =
function_handle with value:
@(x)x.*2.0-3.0
>> df(2)
ans =
1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.