How to do least square error estimation to find the coeffcient values for a quadratic type equation with set of constraints on coefficients
6 views (last 30 days)
Show older comments
MAHANTESH CHIKKADESAI
on 18 Feb 2021
Commented: Just Manuel
on 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 Comments
Accepted Answer
Just Manuel
on 18 Feb 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 Comment
Just Manuel
on 2 Mar 2021
More Answers (0)
See Also
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!