Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nlinfit in Matlab?

Tags:

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.

like image 551
John Alberto Avatar asked Sep 17 '13 01:09

John Alberto


People also ask

How do you calculate goodness of fit in Matlab?

To examine goodness-of-fit statistics at the command line, either: In the Curve Fitter app, export your fit and goodness of fit to the workspace. On the Curve Fitter tab, in the Export section, click Export and select Export to Workspace. Specify the gof output argument with the fit function.

How do you do regression in Matlab?

b = regress( y , X ) returns a vector b of coefficient estimates for a multiple linear regression of the responses in vector y on the predictors in matrix X . To compute coefficient estimates for a model with a constant term (intercept), include a column of ones in the matrix X .


1 Answers

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.

like image 157
Dan Avatar answered Sep 19 '22 01:09

Dan