匿名函数的导数 [英] Derivative of Anonymous Function

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

问题描述

我具有以下匿名功能:

f = @(x)x^2+2*x+1

我正在使用它,因此我可以通过以下方式使用它:

I'm using this so that I use it in the following way:

f(0) = 1

但是,如果我想找到这样一个函数的派生同时又保持其匿名函数功能怎么办?我尝试执行以下操作,但不起作用:

But what if I want to find the derivative of such a function while still keeping it's anonymous function capability? I've tried doing the following but it doesn't work:

f1 = @(x)diff(f(x))

但这只是返回

[]

关于如何实现此目标的任何想法?

Any thoughts on how to accomplish this?

我当然可以在3秒钟内手动完成此操作,但这不是重点...

Of course I could manually do this in 3 seconds but that's not the point...

推荐答案

对n个元素的向量进行比较时,它仅输出n-1个元素的另一个向量,这些向量具有连续的差..因此,当您放置1个元素时向量,您会得到一个空的.

When you do diff of a vector of n elements it just outputs another vector of n-1 elements with the consecutive differences.. so when you put a 1 element vector you get an empty one.

一种可行的方法是确定一个epsilon并使用牛顿的差商:

A way to go would be to decide an epsilon and use the Newton's difference quotient:

epsilon = 1e-10;
f = @(x) x^2+2*x+1;
f1 = @(x) (f(x+epsilon) - f(x)) / epsilon;

或者只是做数学并写下公式:

or just do the math and write down the formula:

f1 = @(x) 2*x+2;

http://en.wikipedia.org/wiki/Numerical_differentiation

这篇关于匿名函数的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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