2017-10-17 75 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