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

3 visualizzazioni (ultimi 30 giorni)
MAHANTESH CHIKKADESAI il 18 Feb 2021
Commentato: Just Manuel il 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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Just Manuel il 18 Feb 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
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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Just Manuel il 2 Mar 2021
Cheers
Manuel

Accedi per commentare.

### Categorie

Scopri di più su Choose a Solver in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by