Matlab curve fitting toolbox
16 views (last 30 days)
Show older comments
I'm to do some curve fitting using non linear least squares using Matlab.
In my equation that I am to fit/model using my data set, the dependent variable also appears on the right hand side.
Can Matlab handle this?
UJJWAL on 6 Mar 2012
You want to do it programmatically and it can be done but being a new user you may find it a bit difficult. There is a curve fitting toolbox in MATLAB which can be used by you. I am just describing it below for you.
a) Open the curve Fitting toolbox.
b)click on DATA...
c) Select the variables in the workspace that represtn the x and y-axis data and then click on create new data set.
d) Now Click on Ok and then click on Fitting... which is next to Data...
e) In the Type of Fit combo box select Custom Equations and then click on New.
f)A New window will open and there select the tab corresponding to General Equations.
g) Corresponding to the dependent variable y type the equation like cos(a*x+b).
h) As you will write the constants like a and b they will appear in the table below. You can also modify their starting points and their upper and lower limits. If you have any idea of these quantities depennding upon the nature of your program and your requirements then change them accordingly or let MATLAB handle the issue. It will just take some extra time.
Now CLick ok to go back to the previous window.
i) Click on Apply. MATLAB will compute the appropriate values of the constants and display them in the same wiundow under the title results. It will show the values of the constants you have provided computed in the least squares sense. In case you do not want linear fitting there are other options available in the toolbox itself like nearest neighbour or bicubic interpolation.
If you need more explanation ask further. However I think it will help you. If you are particular about the coding then after doing the fitting Click on File and Click on Generate Code. It will generate the code and then you would be able to see that it is a bit cluttered to do it programmatically. Good Luck to you.
Happy to Help
Zhongbo on 7 Apr 2012
I have the same question. UJJWAL gave a clear explanation on how to use curve fitting tool of Matlab, however, the custom equation generator does not allow the dependent variable shown on the right side of the equation, which is the most difficult part in the original equation.
Tom Lane on 7 Apr 2012
Do you have the Statistics or Optimization Toolbox available? (If not, you might get away with using fminsearch.) I'd recommend bringing everything over to one side of the equation, and defining the dependent or response variable to be a vector of zeros.
Here's a simple linear example for illustration, where I want to find the coefficients in
pop = b1 + b2*cdate/1000 + b3*sqrt(pop)
0 = b1 + b2*cdate/1000 + b3*sqrt(pop) - pop
Here's how to do that:
[ones(21,1),cdate/1000,sqrt(pop)]\pop % linear method
f = @(b,xy) b(1) + b(2)*xy(:,1)/1000 + b(3)*sqrt(xy(:,2)) - xy(:,2);
nlinfit([cdate pop], zeros(21,1), f, [1 1 1]) % nonlinear fit
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!