How to do least square error estimation to find the coeffcient values for a quadratic type equation with set of constraints on coefficients

13 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Just Manuel
Just Manuel el 18 de Feb. de 2021
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

Más respuestas (0)

Categorías

Más información sobre Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by