Generating Multivariate Polynomial Coeffients with known Constants
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I am extremely new to MatLab and am having a hard time trying to generate Coefficients from this equation:
t= (A * a) + (B * s) + (C * s * a)+ (D * s^2) + (E * s^2 * a) + (F)
I have the data for t,s,a.
Trying to find (A,B,C,D,E,F)
I can remove the Coefficient (F) if need be, being it is just an offset, and I have (t).
I could post what I have tried, but I usually dont save it because I am not getting close.
I do have Curve Fitting, Statistics, and Optimization Toolboxes thinking it would help.
I have also tried polyfit and polyfitn.
Also, if this has been answered a hundred times, what would best describe that equation for searching?
Accepted Answer
Steven Lord
on 2 Jul 2024
See the Multiple Regression section on this documentation page for an example you can adapt to your equation. Make sure you use array operators when constructing the columns of the design matrix.
7 Comments
Eric
on 3 Jul 2024
x1 = [20]';
x2 = [250]';
x3 = [20.*20]';
x4 = [20.*250]';
x5 = [20.*20.*250]';
y = [62.5306]';
X = [ones(size(x1)) x1 x2 x3 x4 x5];
a = X\y
x1 = [20]';
x2 = [250]';
y = [62.5306]';
X = [ones(size(x1)) x1 x2 x1.*x1 x1.*x2 x1.*x1.*x2];
a = X\y
I must be missing something.
I tried both ways and it gives me 0 for the first 4 coefficients.
x1 = [20]';
x2 = [250]';
x3 = [20.*20]';
x4 = [20.*250]';
x5 = [20.*20.*250]';
y = [62.5306]';
X = [ones(size(x1)) x1 x2 x3 x4 x5];
v = X\y
v = 6x1
1.0e-03 *
0
0
0
0
0
0.6253
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This means that y = s.^2*a*v(6). With one data point, how do you expect to compute more than one non-zero coefficient? It is a valid solution; there are others.
[v(6)*x5; y]
ans = 2x1
62.5306
62.5306
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
svalue = 20;
avalue = 250;
v(6)*svalue.^2.*avalue - y
ans = -7.1054e-15
That looks like when I evaluate the polynomial at the specified value of s and a I get the correct value of y.
Try fitting with more sets of (s, a, y) values at once and the coefficients may not be 0 values.
I will do some more testing/learning. I was trying to learn on a given data point that I had all the answers for before I try my 10400x4 matrix. I have what the coefficients "should be" for that example I am working with, kind of a "reverse engineering" for me.
I have:
if
x1 = [20]
x2 = [250]
x3 = [20.*20]
x4 = [20.*250]
x5 = [20.*20.*250]
62.53059= (-240.3125) + (x1 * 7.119140625) + (x2 * 0.884765625) + (x3 * -0.2564697265625) + (x4 * -0.0068206787109375) + (x5 * 0.000759601593017578)
You seem to be assuming that the solution is unique. It need not be, as explained with this simpler example Cleve Moler wrote about years ago. Cleve's problem has 1 equation in 2 unknowns; yours (with that one data point) has 1 equation in 6 unknowns.
s = 20;
a = 250;
x1 = s;
x2 = a;
x3 = s*s;
x4 = s*a;
x5 = s*s*a;
This is one solution.
coeffs = [-240.3125;
7.119140625;
0.884765625;
-0.2564697265625;
-0.0068206787109375;
0.000759601593017578];
rhs = @(coeffs) coeffs(1) + (x1 * coeffs(2)) + (x2 * coeffs(3)) + (x3 * coeffs(4)) + (x4 * coeffs(5)) + (x5 * coeffs(6));
lhs = 62.53059;
check = [rhs(coeffs); lhs]
check = 2x1
62.5306
62.5306
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This is another solution.
coeffs = [lhs; 0; 0; 0; 0; 0];
check = [rhs(coeffs); lhs]
check = 2x1
62.5306
62.5306
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Here is a third.
coeffs = [0; 0; 0; 0; 0; lhs./(s.^2*a)]
coeffs = 6x1
1.0e-03 *
0
0
0
0
0
0.6253
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
check = [rhs(coeffs); lhs]
check = 2x1
62.5306
62.5306
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So which one is "the" right one for this single point? Any of the above.
Eric
on 3 Jul 2024
I read Cleve Molers article. That helps put it in perspective. Thank you!
I think my next project is to add lower and upper constraints to the coefficients.
Thank you again.
If you use more information (and so more equations) that will also affect the solution. To use Cleve's example, if I told you that the two numbers have an average of 3 there are multiple solutions (3 and 3, 4 and 2, 6 and 0, etc.)
A = [1/2 1/2; 1 -1];
b = [3; 2];
solutionsToFirstEquation = [3 4 6 16-pi; 3 2 0 -10+pi]
solutionsToFirstEquation = 2x4
3.0000 4.0000 6.0000 12.8584
3.0000 2.0000 0 -6.8584
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
checkFirstEquation = A(1, :)*solutionsToFirstEquation
checkFirstEquation = 1x4
3 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If I then add the additional information that the difference of the two numbers is 2 the solution is unique. The solution is the second column of solutionsToFirstEquation.
x = A\b
x = 2x1
4
2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
satisfiesBothEquations = A*solutionsToFirstEquation % Only 2nd is right
satisfiesBothEquations = 2x4
3.0000 3.0000 3.0000 3.0000
0 2.0000 6.0000 19.7168
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
satisfiesBothEquations2 = [A*x, b]
satisfiesBothEquations2 = 2x2
3 3
2 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Eric
on 4 Jul 2024
I can't tell if constraints on the coefficients would help me or not.
I understand (now) that if I am looking for a specific outcome, then I need to apply specific cirtieria for the output to meet.
I can't seem to find how to apply constraints to the coefficient data as it is calculated. Also, I would assume this would be linear constraints:
A= >= -16 & <= 16
B= >= -64 & <= 64
C= >= -.25 & <= .25
D= >= -2.000 & <=1 .999
E= >= -.0078 & <= .0078
F= >= -2048 & <= 2048
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)