在Matlab中使用nlinfit? [英] Using nlinfit in Matlab?
问题描述
我在Matlab中无法理解和应用nlinfit
函数的使用.所以,假设我得到了向量
I'm having trouble understanding and applying the use of nlinfit
function in Matlab. So, let's say I'm given vectors
x = [1, 2, 3, 4, 5]
y = [2.3, 2.1, 1.7, .95, .70]
,然后要求我将此数据拟合为指数形式(我不知道数字是否会起作用,我将它们补足了),其中y = A*e^(Bx) + C
(A/B/C
是常数).
and I'm asked to fit this data to an exponential form (I don't know if the numbers will work, I made them up) where y = A*e^(Bx) + C
(A/B/C
are constants).
我的理解是nlinfit
接受4个参数,两个向量,一个modelfunction
在这种情况下应该是我上面的公式,然后是beta0
,我一点都不明白.我的问题是如何在nlinft
中实现modelfunction
,以及如何找到beta0
(当仅使用要绘制/拟合的2个向量时)以及应该如何实现?有人可以给我看一个例子,以便我适合自己使用此功能吗?我怀疑我将来会经常使用它,并且真的想学习它.
My understanding is that nlinfit
takes 4 arguments, the two vectors, a modelfunction
which in this case should be the equation I have above, and then beta0
, which I don't understand at all. My question is how do you implement the modelfunction
in nlinft
, and how do you find beta0
(when only working with 2 vectors you want to plot/fit) and how should it be implemented? Can someone show me an example so that I can apply this function for any fit? I suspect I'll be using this a lot in the future and really want to learn it.
推荐答案
查看文档中的第二个示例: http://www.mathworks.com/help/stats/nlinfit.html
Check out the second example in the docs: http://www.mathworks.com/help/stats/nlinfit.html
基本上,您将函数句柄作为您的modelfunction
参数传递.要么在文件中创建一个函数,然后在函数名称前传递一个@
,要么创建一个匿名函数,如下所示:
Basically you pass a function handle as your modelfunction
parameter. Either make a function in a file and then just pass it the function name with an @
in front or else make an anonymous function like this:
nlinfit(x, y, @(b,x)(b(1).*exp(b(2).*x) + b(3)), beta0)
您会注意到,在上文中,我将所有参数都固定在一个向量中.函数的第一个参数必须是要尝试求解的所有点的矢量(例如,在您的情况下为A
,B
和C
),第二个必须为x
.
You'll notice that in the above I have stuck all your parameters into a single vector. The first parameter of your function must be a vector of all the points you are trying to solve for (i.e. A
, B
and C
in your case) and the second must be x
.
就像木片所说的那样,beta0
是您的起点,因此,您对A
,B
和C
参数的最佳猜测(不一定是很大的).所以类似[1 1 1]
或rand(3,1)
的东西,这是非常特定于问题的.您应该玩一些.请记住,这是一个本地搜索功能,因此可能会卡在本地最优值上,因此您的出发点实际上可能非常重要.
As woodchips has said beta0
is your starting point so your best guess (doesn't have to be great) of your A
, B
and C
parameters. so something like [1 1 1]
or rand(3,1)
, it is very problem specific though. You should play around with a few. Just remember that this is a local search function and thus can get stuck on local optima so your starting points can actually be quite important.
这篇关于在Matlab中使用nlinfit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!