insert two criteria function to lsqcurvefit
Mostrar comentarios más antiguos
Hi, I have some data points as (X,Y) and I want to use lsqcurvefit function to find the best fit by least square method. My guidance function is a non-linear function Gamma2(h) as bellow:


As above equation shows, it's a two criteria function and because the value of a0 is not clearly known, I have to use lsqcurvefit function simultaneously to reach the a0, but I don't know how to do it.
Please help me to solve problem.
Thanks, Mani
Respuestas (2)
Star Strider
el 26 de Nov. de 2014
This works with simulated data:
% PARAMETERS: b(1) = C0, b(2) = a0
gamma2 = @(b,h) b(1).*[(1.5*(h/b(2)) - 0.5*(h./b(2)).^3).*(h<=b(2)) + (h>b(2))];
% SIMULATE FUNCTION
b = [10; 5]; % Created Parameters
h = linspace(0,7,25); % ‘h’ (Independent Variable)
gam2data = gamma2(b,h)+0.5*randn(1,length(h)); % Created Data (Dependent Variable)
B0 = [1; 1];
B = lsqcurvefit(gamma2, B0, h, gam2data); % Estimate Parameters
figure(1)
plot(h, gam2data,'bp')
hold on
plot(h, gamma2(B,h), '-r')
hold off
grid
8 comentarios
Mani Ahmadian
el 27 de Nov. de 2014
Star Strider
el 27 de Nov. de 2014
Editada: Star Strider
el 27 de Nov. de 2014
You have to provide a numerical vector of weights the same length as your data vectors. I would use nlinfit for this if you have it (Statistics Toolbox) since it can do weighted nonlinear parameter estimation. It is also relatively easy with fminsearch.
Your function does not describe your data. It gives a non-significant fit that is essentially a horizontal line through the mean of the y-values. You need more data over a wider range of ‘h’ values, ideally starting near zero. I doubt if a weight vector would improve the fit.
Mani Ahmadian
el 28 de Nov. de 2014
Star Strider
el 28 de Nov. de 2014
Editada: Star Strider
el 28 de Nov. de 2014
That weight vector doesn’t change anything other than the value of ‘C0’. At this point, you simply don’t have enough data over a wide enough span to estimate your function parameters correctly.
I used fminsearch because it handles functions such as yours more efficiently and it also is easier to program a weight vector into the cost function:
W = ones(size(h))./h;
WLSCF = @(b) sum(W.*(Gamma2-gamma2(b,h)).^2); % Weighted Sum-Of-Squares Cost Function
B0 = rand(2,1)*10;
B = fminsearch(WLSCF, B0); % Estimate Parameters
figure(1)
plot(h, Gamma2,'bp')
hold on
plot(h, gamma2(B,h), '-r')
hold off
grid
Mani Ahmadian
el 28 de Nov. de 2014
Star Strider
el 28 de Nov. de 2014
My pleasure.
It’s not the weights. You need more data, preferably 5 or more data points at ‘h’ between 0 and 1, largely because the data you now have are extremely noisy. (I cannot guarantee that you will be able to fit your function reliably even then. It depends on how noisy your additional data are.)
Mani Ahmadian
el 29 de Dic. de 2014
Editada: Mani Ahmadian
el 29 de Dic. de 2014
Star Strider
el 29 de Dic. de 2014
Your equation does not appear to accurately describe your data. I tried several options, including setting lower bounds on ‘b(1)’=C0=max(gam2data), and could not get a good fit.
I suggest you consider a different model. Your current model does not appear to describe the process that is generating the data you are fitting to it.
Beware differentiability issues. Since your curve is not differentiable with respect to a0, the least squares cost function might not be either. It may be more trustworthy to use a derivative-free method like fminsearch instead,
gamma2 = @(b,h) b(1).*[(1.5*(h/b(2)) - 0.5*(h./b(2)).^3).*(h<=b(2)) + (h>b(2))];
A0=fminsearch( @(a0) norm(gamma2(a0,X)-Y) , initial_a0 )
1 comentario
Star Strider
el 26 de Nov. de 2014
I agree, but we’ve been there before in set parameters of nlinfit function. I decided not to fight it this time.
Categorías
Más información sobre Nonlinear Regression en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
