You go to the shops on Monday and buy 1 apple, 1 banana, and 1 carrot; the whole transaction totals €15. On Tuesday you buy 3 apples, 2 bananas, 1 carrot, all for €28. Then on Wednesday 2 apples, 1 banana, 2 carrots, for €23.
Construct a matrix and vector for this linear algebra system. Where a, b, c, are the prices of apples, bananas, and carrots. And each ss is the total for that day.
Fill in the components of A and s.
A11=1
A12=1
A13=1
A21=3
A22=2
A23=1
A31=2
A32=1
A33=2
sMon=15
sTue=28
sWed=23
A = [[A11, A12, A13],
[A21, A22, A23],
[A31, A32, A33]]
s = [sMon, sTue, sWed]
Let’s return to the apples and bananas from Question 1.
Take your answer to Question 1 and convert the system to echelon form. I.e. Replace A and s with the correct values below:
A12=1
A13=1
A23=2
s1=15
s2=17
s3=5
A = [[ 1 , A12, A13],
[ 0, 1 , A23],
[ 0, 0 , 1 ]]
s = [s1, s2, s3]
Following on from the previous question; now let’s solve the system using back substitution.
What is the price of apples, bananas, and carrots?
a=3
b=7
c=5
s = [a, b, c]
If every week, you go to the shops and buy the same amount of apples, bananas, and oranges on Monday, Tuesday, and Wednesday; and every week you get a new list of daily totals - then you should solve the system in general.
That is, find the inverse of the matrix you used in Question 1
A11=-1.5
A12=0.5
A13=0.5
A21=2
A22=0
A23=-1
A31=0.5
A32=-0.5
A33=0.5
Ainv = [[A11, A12, A13],
[A21, A22, A23],
[A31, A32, A33]]
In practice, for larger systems, one never solves a linear system by hand as there are software packages that can do this for you - such as numpy in Python.
Use this code block to see numpy invert a matrix.
You can try to invert any matrix you like. Try it out on your answers to the previous question.
import numpy as np
A = [[1, 1, 3],
[1, 2, 4],
[1, 1, 2]]
Ainv = np.linalg.inv(A)
In general, one shouldn’t calculate the inverse of a matrix unless absolutely necessary. It is more computationally efficient to solve the linear algebra system if that is all you need.
Use this code block to solve the following linear system with numpy. Ar=s,
import numpy as np
A = [[4, 6, 2],
[3, 4, 1],
[2, 8, 13]]
s = [9, 7, 2]
r = np.linalg.solve(A, s)