如何在Matlab中评估函数的导数? [英] how to evaluate derivative of function in matlab?

查看:125
本文介绍了如何在Matlab中评估函数的导数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该非常简单.我有一个函数f(x),我想在MATLAB中为给定的x评估f'(x).

This should be very simple. I have a function f(x), and I want to evaluate f'(x) for a given x in MATLAB.

我所有的搜索都提出了符号数学,这不是我所需要的,我需要数值微分.

All my searches have come up with symbolic math, which is not what I need, I need numerical differentiation.

例如如果我定义:fx = inline('x.^2')

我想找到f'(3),应该是6,我不想找到2x

I want to find say f'(3), which would be 6, I don't want to find 2x

推荐答案

要获取数值差(对称差),请计算(f(x+dx)-f(x-dx))/(2*dx)

To get a numerical difference (symmetric difference), you calculate (f(x+dx)-f(x-dx))/(2*dx)

fx = @(x)x.^2;
fPrimeAt3 = (fx(3.1)-fx(2.9))/0.2;

或者,您可以创建函数值的向量并应用 DIFF ,即

Alternatively, you can create a vector of function values and apply DIFF, i.e.

xValues = 2:0.1:4;
fValues = fx(xValues);
df = diff(fValues)./0.1; 

请注意,diff采用前向差,并且假定dx等于1.

Note that diff takes the forward difference, and that it assumes that dx equals to 1.

但是,根据您的情况,最好将fx定义为多项式,并评估函数的导数,而不是函数值.

However, in your case, you may be better off to define fx as a polynomial, and evaluating the derivative of the function, rather than the function values.

这篇关于如何在Matlab中评估函数的导数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆