Incorrect Coefficients from Symbolic Polynomial Equation Solving

36 views (last 30 days)
I'm working with MATLAB's symbolic toolbox to solve a polynomial equation for specific coefficients a1, a2, a3, and a4. The objective is to express a polynomial in terms of these coefficients and then solve for them such that the polynomial equation holds true.
**Here is my function:**
function FindEquations()
syms a1 a2 a3 a4 x; % Define symbolic variables
% Define the equation with all terms on one side (zero on the other side)
eqn = a1 + a4*(x^3 + x^2 + x + 1) + a3*(x^2 + x + 1) + a2*(x + 1) - x^2 == 0;
% Expand the equation to simplify it and correctly distribute all terms
eqn = expand(eqn);
% Collect coefficients for the system of linear equations
[A, B] = equationsToMatrix(eqn, [a1, a2, a3, a4]);
% Solve the linear system
sol = linsolve(A, B);
% Display the solution
disp('The solutions for a1, a2, a3, a4 are:');
disp(sol);
end
```
**Expected Output:**
The coefficients should logically be [0, -1, 1, 0], corresponding to a1, a2, a3, a4, respectively.
**Actual Output:**
However, the actual output I get is [x^2, 0, 0, 0]. This suggests that a1 is being incorrectly set to x^2 instead of 0, and the others are zero, which does not match my expectations based on the equation setup.
Question:
1. Why might the `equationsToMatrix` function not be translating the symbolic equation into the correct system matrix A and vector B?
2. How can I adjust the equation or the process of deriving A and B to ensure that the coefficients are determined accurately?
Additional Context:
- I am using MATLAB R2021a.
- The symbolic toolbox is installed and has been working fine for other types of calculations.
Any insights or suggestions on how to correct this issue or alternative methods to achieve the desired results would be greatly appreciated!

Accepted Answer

John D'Errico
John D'Errico on 13 Apr 2024 at 18:11
Edited: John D'Errico on 13 Apr 2024 at 19:43
NO. You think there is a unique solution. But that is wrong.
syms a1 a2 a3 a4 x; % Define symbolic variables
% Define the equation with all terms on one side (zero on the other side)
eqn = a1 + a4*(x^3 + x^2 + x + 1) + a3*(x^2 + x + 1) + a2*(x + 1) - x^2 == 0;
eqn = expand(eqn)
eqn = 
For that equation to hold true, for ALL values of x, this is what solve returns. What makes that equation valid for EVERY POSSIBLE value of x? That is, identically EQUAL TO ZERO.
[A, B] = equationsToMatrix(eqn, [a1, a2, a3, a4])
A = 
B = 
% Solve the linear system
sol = linsolve(A, B);
Warning: Solution is not unique because the system is rank-deficient.
% Display the solution
disp('The solutions for a1, a2, a3, a4 are:');
The solutions for a1, a2, a3, a4 are:
disp(sol);
It holds true for ALL x, only when a1 = x^2, and all of the other coefficients are zero.
In fact, linsolve tells us there are other solutions that are possible. That is, the solution was not a unique choice. There is in fact a 3 dimensional solution space of infinitely many solutions.
solgen = solve(eqn,{a1,a2,a3,a4},returnconditions = true)
solgen = struct with fields:
a1: x^2 - z1 - z2 - x*z - x*z1 - x*z2 - x^2*z1 - x^2*z2 - x^3*z2 - z a2: z a3: z1 a4: z2 parameters: [z z1 z2] conditions: symtrue
Solve tells us that, although it is not very clear what that space looks like. Effectively, it is controlled by the equation itself. So you could choose ANY value for z there, and z1, and z2.
syms z z1 z2
sol124 = subs(solgen,[z,z1,z2],[1 2 4])
sol124 = struct with fields:
a1: - 4*x^3 - 5*x^2 - 7*x - 7 a2: 1 a3: 2 a4: 4 parameters: [1 2 4] conditions: symtrue
Now we can plug that back into eqn...
subs(eqn,sol124)
ans = 
and we see it is happy.
Now, COULD the solution you think is the correct one also work? Yes. But why do you think it is the ONLY unique solution? That is flat out wrong.
subs(eqn,[a1,a2,a3,a4],[0 -1 1 0])
ans = 
Again, it is happy. BUT the solution linsolve returns is as equally valid.
Hmm, I think maybe what you want, is to see if a solution exists where all of the parameters a1,a2,a3,a4 are constant, and not a function of x. linsolve does not understand that as a requirement. Try this instead:
expr = a1 + a4*(x^3 + x^2 + x + 1) + a3*(x^2 + x + 1) + a2*(x + 1) - x^2
expr = 
So I wrote it as an expression, instead of an equation. That allows me to use coeffs.
coeffs(expr,x) == 0
ans = 
do you see that generates a set of equations for the coefficients of powers of x. Now we can use solve.
solve(coeffs(expr,x) == 0)
ans = struct with fields:
a1: 0 a2: -1 a3: 1 a4: 0
  2 Comments
Teoman
Teoman on 13 Apr 2024 at 18:26
Edited: Teoman on 13 Apr 2024 at 18:27
I am tryiong to find the system of linear equations using symbols : Isolating and explain how to determine the coefficients a, b, c, and d such that:
x^2 = a * 1 + b * (1+x) + c * (1+x+x^2) + d * (1+x+x^2+x^3)
To find a, b, c, and d we need to equate the polynomial x^2 to the linear combination of the given basis polynomials. This involves setting up the equation and solving for the coefficients. Below is a breakdown of the process:
Step-by-Step Solution:
1. Expand and match the equation to the form of x^2:
- The equation simplifies to: a * 1 + b * (1+x) + c * (1+x+x^2) + d * (1+x+x^2+x^3).
- Expanding this, we get:
(a + b + c + d) + (b + c + d)x + (c + d)x^2 + dx^3
2. Match coefficients for each power of x:
- For x^0 (constant term): a + b + c + d = 0
- For x^1: b + c + d = 0
- For x^2: c + d = 1
- For x^3: d = 0
3. Solve the system of equations:
- From d = 0, plug back into the other equations:
- c + 0 = 1 so c = 1.
- b + 1 + 0 = 0 so b = -1.
- a - 1 + 1 + 0 = 0 so a = 0.
Coefficients Solution:
Thus, the coefficients that satisfy the equation x^2 = a * 1 + b * (1+x) + c * (1+x+x^2) + d * (1+x+x^2+x^3) are:
- a = 0
- b = -1
- c = 1
- d = 0
This means the linear combination of the basis that equals x^2 is:
x^2 = 0 * 1 - 1 * (1+x) + 1 * (1+x+x^2) + 0 * (1+x+x^2+x^3)
Hopefully this clears things up. I dop not know how I can implement this in MATLAB however

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!