MATLAB: How to create a fit with more than 2 independent variables

curve fittingMATLAB

Hello!
I have extracted data from experiments and need to fit a function for them. The parameters to be fit change with time, but the idea is to keep them constant for each fit. In order to do that, I take 5 points at a time from the experimental data and fit the function for it.
At the beginning, my equation was as follows and I only had the 'x' variable taken from the experimental data. G and eta_v are the parameters I wanted to fit in order to find the function y:
y = ((0.0047 – (eta_v +0.0047)*(eta_v/G))*exp(-G*x/eta_v)+(eta_v+0.0047)*eta_v/G)
I am using the fittype function with Nonlinear Least Squares method for the fitting. This was pretty easy, as soon as the fitting is done, I get the next 5 points for x and do the fitting again. I always want to plot the values of G and eta_v as a function of x.
The problem is that now I removed some hypotheses, and there are a few more variables that I need to use. They are shown in the equation below as 'w' and 'z'. I extract them from the experiments as well. The idea is the same as above, to find the values of eta_v and G that fit y from the experimental data. However, I am having a hard time to write it in my code, and for plotting afterwars I only need y, G and eta_v as a function of 'x' (just like in the equation above), so it is a linear plot that I already know how to make.
y = ((eta_v + 0.0047)*(z + eta_v*0.0047/((eta_v + 0.0047)*G)*w)+(0.0047*z – (eta_v + 0.0047)*(z + eta_v*0.0047/((eta_v + 0.0047)*G)*w))*exp(-G*x/eta_v))
Can someone shed some light into how I can perform this fitting, please? I cannot put the values of 'w' and 'z' directly in the equation because they change from each fitting.
Thanks!

Best Answer

You might be able to do it with fminsearch, which is a very general fitting tool.
The basic idea is that you write a function that returns an error score for any possible vector of parameter values [eta_v G w z], and fminsearch will try to find the best values of those four parameters to minimize error.
In your case the error score could be, for example, the sum of squared differences between the actual y values and the fitted y values, for whatever set of (x,y) values you are trying to fit (5 points at a time).
You call fminsearch with starting guesses for [eta_v G w z] and with the name of your error function, and it does the rest.
Hope that helps.
Related Question