2017-10-17 78 views
0

所以我有一個MATLAB分配,我們需要使用數值計算MATLAB中的導數?

DF來計算函數的導數(x)的/ DX =(F(X0 + H) - F(X0-H))/ 2H

所以我把這個變成了一個新的函數,並且想要傳入我想要的衍生物的函數。

我對MATLAB很新,所以幫助將不勝感激。下面是我得到了什麼,試圖計算在x = 0.6衍生:

%% Problem 2 
syms x; 
funct1 = @(x) (x^3)*exp(2*x) 
x0  = 0.6; 
der1 = FunDer(@funct1,x0); 

%% The saved function in a separate file 
function [ der ] = FunDer(@funct1,x0) 
    % function to calculate derivative 
    h = 1e-5; 
    x1 = x0+h; 
    x2 = x0-h; 
    der = (subs(@funct1,x,x1) - subs(@funct1,x,x2))/(2*h); 
end 
+0

在較新的Matlab版本中,函數不需要單獨放入單獨的文件中。 – Bernhard

回答

1

正如你已經使用了一個匿名函數,你不必使用符號。檢查下面的修改代碼:

%% Problem 2 
% syms x; 
funct1 = @(x) (x^3)*exp(2*x) 
x0=0.6; 
der1=FunDer(funct1,x0) 

%%The saved function in a separate file 
function [ der ] = FunDer(funct1,x0) 
%function to calculate derivative 
h=0.00001; 
x1=x0+h; 
x2=x0-h; 
der = (funct1(x1)-funct1(x2))/(2*h); 
end