How to do least square error estimation to find the coeffcient values for a quadratic type equation with set of constraints on coefficients
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MAHANTESH CHIKKADESAI
le 18 Fév 2021
Commenté : Just Manuel
le 2 Mar 2021
I have measured and actual values need to find the values of a,b,c.
for y=ax^2+bx+c
constraints are: a+b+c=1
0<a<1, 0<b<1,0<c<1
0 commentaires
Réponse acceptée
Just Manuel
le 18 Fév 2021
I love this answer from Star Strider: https://ch.mathworks.com/matlabcentral/answers/126146-curve-fitting-without-the-toolbox
This allows you to fit any curve with vanilla matlab.
You just need to write a function that constrains your parameters and your own cost function.
Try this:
% i don't know your data, so i'll demonstrate fitting a second
% order over an inverse cosine
x = -1:0.01:1;
y = 1 - cos(x .* 1.5);
% define 2nd order polynomial function
fun = @(b, x) b(1) .* x.^2 + b(2) .* x + b(3);
% i use square to constrain parameters to be positive
% and divide by the sum to have the parameters to sum up to 1
constrainParams = @(b) b.^2 ./ sum(b.^2);
% cost function to optimize is just a least square function
cost = @(b) sum((fun(constrainParams(b),x) - y).^2);
% find parameters using fminsearch (in the domain of all real
% numbers) with an initial guess (probably you have one that
% fits your data).
b = fminsearch(cost, [1 0 0]);
% then you just have to apply the constrain function to those
% parameters
params = constrainParams(b);
% there you have a, b and c
a = params(1);
b = params(2);
c = params(3);
% quickly verify it with a plot
plot(x,y, x,fun(params, x));
Cheers
Manuel
1 commentaire
Just Manuel
le 2 Mar 2021
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Optimization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!