John Alberto
John Alberto

Reputation: 437

Using nlinfit in Matlab?

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] 

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).

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.

Upvotes: 6

Views: 18966

Answers (2)

Dan
Dan

Reputation: 45741

Check out the second example in the docs: http://www.mathworks.com/help/stats/nlinfit.html

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)

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.

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.

Upvotes: 3

user85109
user85109

Reputation:

beta0 is your initial guess at the parameters. The better your guess, the more likely you will see convergence to a viable solution. nlinfit is no more than an optimization. It has to start somewhere.

Upvotes: 0

Related Questions