Ask Your Question
2

General solution for a matrix

asked 2019-04-16 20:20:09 +0100

scrName gravatar image

updated 2019-04-17 09:22:18 +0100

I have a matrix named p and with the values

[-10*x1 - 4*x2 + 14,   -4*x1 - 2*x2 + 6]

When I try to solve the matrix with this line:

print(p(p.arguments()[0] == 0, p.arguments()[1] == 0))

I get this error:

ValueError: use named arguments, like EXPR(x=..., y=...)

In general i want to use something like this, the arguments are x1 and x2 and the x is a list of numbers that it will change on every loop

print(p([i == j for i,j in zip(f.arguments(), x)]))

But it reproduce the same error

edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage. Thank you for your question.

slelievre gravatar imageslelievre ( 2019-04-17 08:29:26 +0100 )edit

Can you provide the actual code you use to define p?

Please edit your question to do that.

slelievre gravatar imageslelievre ( 2019-04-17 08:30:03 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-04-17 08:40:03 +0100

slelievre gravatar image

updated 2019-04-17 09:34:35 +0100

To solve this system of two equations in two unknowns, one can either use solve or, since the equations are linear, one could use linear algebra.

Here is how to use solve:

sage: x1, x2 = SR.var('x1 x2')
sage: p = [-10*x1 - 4*x2 + 14, -4*x1 - 2*x2 + 6]
sage: solve(p, x1, x2)
[[x1 == 1, x2 == 1]]

Using linear algebra, the question translates as solving $a u = b$ for $u$, where $a = \begin{pmatrix}10 & 4 \newline 4 & 2 \newline \end{pmatrix}$ and $b = \begin{pmatrix}14 \newline 6\end{pmatrix}$.

Here is how to solve that:

sage: a = matrix([[10, 4], [4, 2]])
sage: b = vector([14, 6])
sage: a \ b
(1, 1)

Now it's easy to change the vector b.

sage: a \ vector([3, 8])
(-13/2, 17)

To solve in the general case $b = \begin{pmatrix}b_x \newline b_y\end{pmatrix}$:

sage: a = matrix(SR, [[10, 4], [4, 2]])
sage: bx, by = SR.var('bx by')
sage: b = vector([bx, by])
sage: a \ b
(1/2*bx - by, -bx + 5/2*by)

Note that this amounts to computing the inverse of a:

sage: a.inverse()
[1/2  -1]
[ -1 5/2]

To read more about this and other topics, consider the following resources:

edit flag offensive delete link more

Comments

Thank you but i did make a wrong use of the word solution I want to replace the x1 and x2 with numbers and use that result on something else So on the example before i want to get the result of [14, 6]

scrName gravatar imagescrName ( 2019-04-17 09:03:35 +0100 )edit

Ok i also had this equation for p

p = Q*x - b

and i replace x in that equation and that it is how i got the answer that i wanted

I think that sagemath wants to use matrices with multiplication not as a place that you can throw a function

scrName gravatar imagescrName ( 2019-04-17 10:21:22 +0100 )edit

Thank you, you give a good idea on how to solve my problem

scrName gravatar imagescrName ( 2019-04-17 10:21:51 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-04-16 20:20:09 +0100

Seen: 787 times

Last updated: Apr 17 '19