Create Matrix of coefficients from nonlinear function

This is the code im using. Based on question 3, I believe it is x^1, x^2, x^3, x^4 NOT x1 x2 x3 x4.
I attempted to use "[A]=equationsToMatrix(eqns)" but it states that it can not compute non linear equations.
syms x [4 1]
eqn1=16*x^1+32*x^2+33*x^3+13*x^4==91;
eqn2=5*x^1+11*x^2+10*x^3+x^4==16;
eqn3=9*x^1+7*x^2+6*x^3+12*x^4==5;
eqn4=34*x^1+14*x^2+15*x^3+x^4==43;
eqns=[eqn1;eqn2;eqn3;eqn4]

8 Comments

I can assure you that it is x = [x(1),x(2),x(3),x(4)], not x^1, x^2, x^3 and x^4.
if so then how would I solve for x in the 3 question
You solve it by correctly writing the equations. The question did not tell you to raise x to those powers, but to solve for a vector of unknowns. Or you can write it to solve for the 4 unknowns x1, x2, x3, x4. Your choice. Then use solve. But your decision, to not believe the question was written correctly is simply wrong. It WAS written correctly. There are 4 unknown variables.
if so then how would I solve for x in the 3 question
This question has already been answered here:
The only thing missing in the answer is
x = A\b
Another way to solve for x would be the usage of linsolve. But A\b is what I would use too.
This is what I did to solve for x1 x2 x3 x4
>> syms x1 x2 x3 x4
eqn1=16*x1+32*x2+33*x3+13*x4==91;
eqn2=5*x1+11*x2+10*x3+x4==16;
eqn3=9*x1+7*x2+6*x3+12*x4==5;
eqn4=34*x1+14*x2+15*x3+x4==43;
eqns=[eqn1;eqn2;eqn3;eqn4]
[A,b]=equationsToMatrix(eqns)
eqns =
16*x1 + 32*x2 + 33*x3 + 13*x4 == 91
5*x1 + 11*x2 + 10*x3 + x4 == 16
9*x1 + 7*x2 + 6*x3 + 12*x4 == 5
34*x1 + 14*x2 + 15*x3 + x4 == 43
A =
[16, 32, 33, 13]
[ 5, 11, 10, 1]
[ 9, 7, 6, 12]
[34, 14, 15, 1]
b =
91
16
5
43
>> solve(eqns)
ans =
struct with fields:
x1: -329/2624
x2: -23417/2624
x3: 1883/164
x4: -1/41
It just seems odd to me that the question would ask for 1 x value rather than asking for x1 x2 x3 x4.
Even in your solution, using x=A\b it still produced 4 values. Just thought it was weird
The x in your assignment question is the vector x consisting of the four components x(1), x(2), x(3) and x(4).
I already wrote so, but you don't seem to read carefully.
And if you use the command
solve(eqns)
you can skip the line
[A,b]=equationsToMatrix(eqns)
because A and b are not used.
The question also is asking for the vector b which is 4x1 too. Why do you accept b as a vector but not x? Note that linear systems have the form A*x=b with A being a n*n matrix and b & x being vectors of size n*1.

Sign in to comment.

Answers (1)

I'm sorry, but you are most certainly incorrect.
A system of 4 equations like that would have 4 unknowns. In what you were shown, there are 4 unknowns listed. They are called x1,x2,x3,x4.
But it makes absolutely NO sense to have a system of 4 equations with ONE unknown, where the unknown is the same variable in all four equations. That is, suppose you wrote this as
syms x
eq1 = 16*x^1 + 32*x^2 + 33*x^3 + 13*x^4 == 91
eq1 = 
As one polynomial equation, in one unknown, it has 4 roots. We can compute those roots easily enough, using solve. or vpasolve, for example.
vpasolve(eq1)
ans = 
However, it makes no sense to pose a system of 4 polynomial equations like that, all with the SAME variable x. Each equation will have totally different roots, but no root in common. And there would not be any reason to call it a "system of equations" if your thinking about the equations was correct.
So I am sorry, but you are completely incorrect. There are 4 unknowns. Call them x1,x2,x3,x4. Call them x,y,z,w. Call them fred, barney, wilma, pebbles as you wish. But they ARE NOT powers. Yes, I know, you think it was a typo, and that you expected to see powers there. They are just 4 distinct variables.
And when you do write the problem in terms of 4 unknowns as I have suggested, now equationsToMatrix will work perfecly well.

Products

Release

R2022b

Asked:

on 3 Dec 2022

Edited:

on 11 Jan 2023

Community Treasure Hunt

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

Start Hunting!