Fit data with dependent parameters

Hi,
There are two rows of data, x and y. I would like to fit y = f(x), where
f(x) = a*x^3 + b*x^2 + (2a+3b)*x,
i.e. parameters are not independent.
I tried to use the function "fittype", but it does not work (Licensing error: -101,147).
I would like to know if there is any other way to solve it.
Thank you!

 Risposta accettata

Yours is a linear problem, however the easiest way to estimate the parameters is likely an unconstrained nonlinear solver, such as fminsearch:
x = ...;
y = ...;
objfcn = @(b,x) b(1).*x.^3 + b(2).*x.^2 + (2*b(1) + 3*b(2)).*x;
[B,resnorm] = fminsearch(@(b) norm(y - objfcn(b,x)), [1;1]);
xv = linspace(min(x), max(x));
figure
plot(x, y, 'pb')
hold on
plot(xv, objfcn(B,xv), '-r')
hold off
grid

6 Commenti

Vogel
Vogel il 16 Lug 2018
Thanks Star Strider. That was a great solution.
As always, my pleasure!
Vogel
Vogel il 24 Lug 2018
Hi Star Strider,
I have realized that the change of the values of the initial points [1;1] exhibits a considerable influence on the result of the fitted parameters and the function. Do you know how are they usually chosen? By manipulating them, I can get the a more convenient form for the desired curve. But how far is this cheating?
Thanks again
It is not ‘cheating’. The ‘surface’ (or ‘hypersurface’) of the parameter space can have a number of local minima as well as the global minimum that results in the optimal (lowest error) parameter set in the nonlinear objective function. Nonlinear parameter estimation can be extremely sensitive to the correct choice of initial parameters, because of the presence of local minima.
I usually deal with this by using a genetic algorithm (the Global Optimization Toolbox ga function). With the proper (totally random) and large enough (I usually begin with 500 individuals) initial population, ga has always (in my experience) found the best parameter set. It can take a while for the algorithm to converge, especially if constrained, and for difficult problems. The form of the objective function to use with ga is the same as that used with fminsearch in this example, specifically:
@(b) norm(y - objfcn(b,x))
I structure my code to display the parameter results in the Command Window, so I have a record of them.
Vogel
Vogel il 25 Lug 2018
Thanks a lot for your clear explanation. I am going to check it.
As always, my pleasure.

Accedi per commentare.

Più risposte (1)

Vogel
Vogel il 17 Lug 2018

0 voti

Hi again,
I have new questions, which probably have more to do with mathematics. But maybe Matlab can help.
Do you know if it is possible to add some constrains?
- I would like to obtain a fitted curve that intersects the points [x=0,y=0] and [x=1, y=1]. I tried to do it by applying some boundary conditions (this is why the coefficients are interdependent). However, the resulting curve does not intersect exactly the points, it just approximates them. (Actually the equation is much more complex than the posted above. It has exponential functions).
- This is more complicated: Is it possible to impose that the second derivative of the resulting curve must be positive at a certain point x (in my case at x=0)?
Thank you in advance

4 Commenti

I have no idea how to do either of those, especially for a nonlinear function.
Vogel
Vogel il 18 Lug 2018
Thank you anyway :)
You could probably do it with lsqlin, but better you post this as a new question, detailing your actual model function.
Vogel
Vogel il 18 Lug 2018
Thank you Matt J. I will try it.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by