Is it possible to use Matlab to get expressions for the coefficients in polynomial regression? (I.e., not just constant coefficients)

I'm trying to modify one section of an old code so that it uses my new data for one of its parameters. The code expresses an equation as a fourth-order power series in X and y. I know how to do polynomial regression on a curve like the one below to get an expression like:
Phi=k0+k1y+k2y2+k3y3+k4y4
However, my problem is that the code uses a more sophisticated expression for k. I.e.,
I need to express the k this way (in terms of a coefficients) because they're needed elsewhere in the code. Right now, the a coefficients are provided in a table in the code, but I have no idea how to calculate them.
I'm comfortable using Matlab and would be grateful if someone could tell me whether there is a way to do polynomial regression for a fourth-order power series (based on curves like the one in the top figure above) and get the coefficients in terms of a coefficients as shown in the figure above. I have looked around but it does not seem like it is possible. For example "multiple linear regression" and "multivariate regression" don't seem to do what I need. Maybe they do though and I'm not experienced enough to see it? Thank you for any advice.

2 Comments

So X and Y are independent variables and phi = f(X,Y) ?
I ask this because in your ansatz for phi
phi = k0 + k1*Y + k2*Y^2 + k3*Y^3 + k4*Y^4
X as variable does not appear.
Torsten, thank you for your help and apologies for the wait (fell asleep!). Yes, they are independent variables. Yes, I also don't understand why X does not appear in my expression, but I think it's OK. Each curve (like in the top figure) is for a specific X. So the phi–Y relationship, I can figure out how to get the correct "a" coefficients," should be correct for X as well.

Sign in to comment.

 Accepted Answer

M = [X.^(-1).*Y,X.^(-1).*Y.^2,Y.^2,X.*Y.^2,X.^(-1).*Y.^3,X.*Y.^3,X.^2.*Y.^3,X.^(-2).*Y.^4,X.^(-1).*Y.^4,Y.^4];
b = PHI - 1.0;
a = M\b;
where X,Y and PHI are column vectors of the same size.
Here, a(i) = ai in your problem description.

7 Comments

Thank you very much for this, Torsten. I don't see how to get each "a" (a1, a2, a3,...) but I will have my coffee and look again!
Thank you, Bruno. There's some fundamental point I'm missing (don't know why I'm struggling so much with this!). I'm sitting down and will try to relate Torsten's expression to the equations I provided and hope to at least understand where my confusion is coming from for a later update. I think your point is that his expression for "a" is for ai.
You get your coefficients a1,a2,...,a10 in the form of a vector "a" with 10 elements.
The ith element of the vector "a" (namely a(i)) is your ai.
What surprises me in your model is k0(xhat) = 1. I would have expected k0(xhat) = a0.
Torsten, yes, I also saw when I was searching around online earlier in the week that all similar expressions have k0(xhat) = a0. I didn't think much of it at the time but if you're surprised, I will stay alert for a possible mistake in the code.
Thank you. It makes sense that the a coefficients are going to be in the form of a vector, but I'm hung up on what Bruno has done to get his "M = " expression. I need to stare at this and really think about what's going on. Like I said, there's something fundamental I am not getting! Thanks for your patience.
It makes sense that the a coefficients are going to be in the form of a vector, but I'm hung up on what Bruno has done to get his "M = " expression.
M*a = b
written out gives
x^(-1)*y*a(1)+x^(-1)*y^2*a(2)+y^2*a(3)+x*y^2*a(4) ... = PHI - 1
thus your model function equation.
You want to solve this equation for a, thus
a = M\b
Thank you both so much. I finally get it! Really appreciate your patience. It's great to finally understand what's going on with this.

Sign in to comment.

More Answers (1)

For each equation divide both sides by the highest negative coefficient,
k4(Xhat)/Xhat^(-2) = a8*Xhat^(-2)/Xhat^(-2) + etc
k4(Xhat)*Xhat^2 = a8 + a9*Xhat + a10*Xhat^2
subtract left from right and you have a standard polynomial that you can do fitting on

1 Comment

Sorry, Walter, I don't follow (very rusty at algebra!). If you have time, would you please explain how to get the "a" values from this? Are you saying that I should use each individual k equation to fit the data in the figure? I'm embarrassed to be so clueless on this!

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 25 May 2022

Edited:

on 26 May 2022

Community Treasure Hunt

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

Start Hunting!