Ask Your Question
0

How to convert linear system to matrix form

asked 2021-04-05 06:09:26 +0100

fahadktk gravatar image

updated 2021-04-05 10:44:35 +0100

slelievre gravatar image

Hi, I am new to SageMath, need help on convert a linear system into matrix form using SageMath, e.g.

3 x + 2 y = 16

7 x + y = 19

edit retag flag offensive close merge delete

Comments

This looks like homework. Could you please tell us, which mathematical equation you want to solve, and what did you try to convert it in Sage ?

tmonteil gravatar imagetmonteil ( 2021-04-05 10:52:01 +0100 )edit

sorry @slelievre I might not have answered because it is probably a homework assignment, but I couldn't resist when I finally saw a question at my math level ! ;-)

ortollj gravatar imageortollj ( 2021-04-05 11:04:58 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-04-05 23:11:28 +0100

slelievre gravatar image

To complement @ortollj's answer which explains how to input the matrix by hand and solve it, here we explain how to enter the system as equations and get matrix form automatically.

Define the symbolic variables and equations:

sage: x, y = SR.var('x, y')
sage: eqns = [3*x + 2*y == 16,
....:         7*x +   y == 19]

Define list of unknowns in desired order, then extract matrix and vector:

sage: unks = [x, y]
sage: m = matrix([[lhs.coefficient(u) for u in unks] for eq in eqns for lhs in [eq.lhs()]])
sage: v = vector([eq.rhs() for eq in eqns])

sage: m
[3 2]
[7 1]
sage: v
(16, 19)

Solve:

sage: sol = m.solve_right(v)
sage: sol
(2, 5)

Check whether there are other solutions:

sage: m.right_kernel()
Vector space of degree 2 and dimension 0 over Symbolic Ring
Basis matrix:
[]

Since the kernel of the matrix has dimension zero, there are no other solutions.


Related:

edit flag offensive delete link more

Comments

To complement @ortollj's answer which explains how to input the matrix by hand and solve it, here we explain how to enter the system as equations and get matrix form automatically.

Yes @slelievre after thinking it over, I said to myself that this is probably what the question was asking (it was probably not a simple question of homework). But unfortunately the author of the question does not seem very talkative (no answer for @tmonteil request for precision ). I plus your answer.

ortollj gravatar imageortollj ( 2021-04-06 07:29:31 +0100 )edit

what's the difference between SR.var and just using var ? @slelievre

aurelius_nero gravatar imageaurelius_nero ( 2021-11-09 08:58:22 +0100 )edit
0

answered 2021-04-05 10:53:15 +0100

ortollj gravatar image

updated 2021-04-05 10:54:23 +0100

like that:

eqL=[3*x+2*y==16 ,7*x+y==19]
M=matrix([[3,2],[7,1]])

P=M*matrix([x,y]).transpose()
show(M\matrix([16,19]).transpose())

check :

solve(eqL,x,y)

not sure it is what you asked

edit flag offensive delete link more

Comments

I forgot to put in the header :var('x','y')

ortollj gravatar imageortollj ( 2021-04-05 11:19:59 +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

Stats

Asked: 2021-04-05 06:09:26 +0100

Seen: 841 times

Last updated: Apr 05 '21