I have a set of x and y values that I would like to fit a polynomial curve around. The function should take the form of up to a 9th order polynomial;
y = a(1)*X.^1 + a(2)*X.^2 + a(3)*X.^3 + a(4)*X.^4 + a(5)*X.^5 + a(6)*X.^6 + a(7)*X.^7 + a(8)*X.^8 + a(9)*X.^9;
where a(n) are my coefficients.
There are two issues I have. This curve might not always take the form of a 9th order polynomial. It may be a 3rd order, 6th order or whatever (UP TO max 9th order).
I am unsure how to set this up using the optimisation toolbox. Any ideas?
Secondly, can I set a constraint so that the calculated y values are always postive?
Many thanks,
Current code below.
Function;
function F = polyfun(a,redCO2)
F = a(1)*redCO2.^1 + a(2)*redCO2.^2 + a(3)*redCO2.^3 + a(4)*redCO2.^4 + a(5)*redCO2.^5 + a(6)*redCO2.^6 + a(7)*redCO2.^7 + a(8)*redCO2.^8 + a(9)*redCO2.^9;
F = @(a) polyfun(a,X);
a0 = [100, 100, 100, 100, 100, 100, 100, 100, 100]; % Starting guess
a = lsqcurvefit(@polyfun,a0,X,y);