Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aedb276

Browse filesBrowse files
committed
add linalg solution
1 parent c7f3b7f commit aedb276
Copy full SHA for aedb276

File tree

Expand file treeCollapse file tree

1 file changed

+261
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+261
-0
lines changed

‎day_04/03_linalg.ipynb

Copy file name to clipboard
+261Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"kernelspec": {
6+
"display_name": "Python 3",
7+
"language": "python",
8+
"name": "python3"
9+
},
10+
"language_info": {
11+
"codemirror_mode": {
12+
"name": "ipython",
13+
"version": 3
14+
},
15+
"file_extension": ".py",
16+
"mimetype": "text/x-python",
17+
"name": "python",
18+
"nbconvert_exporter": "python",
19+
"pygments_lexer": "ipython3",
20+
"version": "3.7.4"
21+
},
22+
"colab": {
23+
"name": "03_linalg.ipynb",
24+
"provenance": []
25+
}
26+
},
27+
"cells": [
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {
31+
"id": "zz2gPozLCcpL"
32+
},
33+
"source": [
34+
"### Solve using Numpy\n",
35+
"\n",
36+
"3x + 5y = 6\n",
37+
"\n",
38+
"7x - 5y = 9\n",
39+
"\n",
40+
"---\n",
41+
"\n",
42+
"x + y + z = 6\n",
43+
"\n",
44+
"y + z = -4\n",
45+
" \n",
46+
"2x + 5y - z = 27\n",
47+
"\n",
48+
"---\n",
49+
"\n",
50+
"x + y = 4\n",
51+
"\n",
52+
"2x + 2y = 8"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"metadata": {
58+
"id": "QyJ3X01uCcpM"
59+
},
60+
"source": [
61+
"import numpy as np"
62+
],
63+
"execution_count": null,
64+
"outputs": []
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {
69+
"id": "wKhsBrp-CcpM"
70+
},
71+
"source": [
72+
"### Question 1\n",
73+
"\n",
74+
"3x + 5y = 6\n",
75+
"\n",
76+
"7x - 5y = 9"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"metadata": {
82+
"colab": {
83+
"base_uri": "https://localhost:8080/"
84+
},
85+
"id": "7Oq8hIBPCcpM",
86+
"outputId": "07c80148-14b4-48c8-b3d6-7f7304b25683"
87+
},
88+
"source": [
89+
"# Create a coefficients matrix\n",
90+
"coeff_matrix = np.array(\n",
91+
" [[3, 5],\n",
92+
" [7, -5]]\n",
93+
")\n",
94+
"\n",
95+
"# Results Matrix\n",
96+
"result_matrix = np.array(\n",
97+
" [[6],\n",
98+
" [9]]\n",
99+
")\n",
100+
"\n",
101+
"# Calculate the results\n",
102+
"results = np.linalg.inv(coeff_matrix).dot(result_matrix)\n",
103+
"\n",
104+
"# Check Answer\n",
105+
"x = results[0]\n",
106+
"y = results[1]\n",
107+
"\n",
108+
"check_answer = (3*x) + (5*y)\n",
109+
"print(f\"Answer to 3x+5y = 6 where x={x[0]} and y={y[0]}: {check_answer[0]}\\n\")\n",
110+
"\n",
111+
"check_answer2 = (7*x)-(5*y)\n",
112+
"print(f\"Answer to 7x-5y = 9 where x={x[0]} and y={y[0]}: {check_answer2[0]}\")"
113+
],
114+
"execution_count": null,
115+
"outputs": [
116+
{
117+
"output_type": "stream",
118+
"text": [
119+
"Answer to 3x+5y = 6 where x=1.5 and y=0.30000000000000004: 6.0\n",
120+
"\n",
121+
"Answer to 7x-5y = 9 where x=1.5 and y=0.30000000000000004: 9.0\n"
122+
],
123+
"name": "stdout"
124+
}
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"metadata": {
130+
"id": "5TCyikB9CcpN"
131+
},
132+
"source": [
133+
"### Question 2\n",
134+
"\n",
135+
"x + y + z = 6\n",
136+
"\n",
137+
" y + z = -4\n",
138+
" \n",
139+
"2x + 5y - z = 27"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"metadata": {
145+
"colab": {
146+
"base_uri": "https://localhost:8080/"
147+
},
148+
"id": "cPpkBVtyCcpN",
149+
"outputId": "41526cf5-dc53-4b26-93fc-a81d7b1d974b"
150+
},
151+
"source": [
152+
"# Create a coefficients matrix\n",
153+
"coeff_matrix = np.array(\n",
154+
" [[1, 1, 1],\n",
155+
" [0, 1, 1],\n",
156+
" [2, 5, -1]]\n",
157+
")\n",
158+
"\n",
159+
"# Results Matrix\n",
160+
"result_matrix = np.array(\n",
161+
" [[6],\n",
162+
" [-4],\n",
163+
" [27]]\n",
164+
")\n",
165+
"\n",
166+
"# Calculate the results\n",
167+
"results = np.linalg.inv(coeff_matrix).dot(result_matrix)\n",
168+
"\n",
169+
"# Check Answer\n",
170+
"x = results[0]\n",
171+
"y = results[1]\n",
172+
"z = results[2]\n",
173+
"\n",
174+
"check_answer3 = x+y+z\n",
175+
"print(f\"Answer to x+y+z = 6 where x={x[0]}, y={y[0]}, z={z[0]}: {check_answer3[0]}\\n\")\n",
176+
"check_answer4 = y+z\n",
177+
"print(f\"Answer to y+z = -4 where y={y[0]} and z={z[0]}: {check_answer4[0]}\\n\")\n",
178+
"check_answer5 = (2*x) + (5*y) - z\n",
179+
"print(f\"Answer to 2x+5y-z = 27 where x={x[0]}, y={y[0]}, z={z[0]}: {check_answer5[0]}\\n\")"
180+
],
181+
"execution_count": null,
182+
"outputs": [
183+
{
184+
"output_type": "stream",
185+
"text": [
186+
"Answer to x+y+z = 6 where x=10.000000000000002, y=0.5, z=-4.5: 6.000000000000002\n",
187+
"\n",
188+
"Answer to y+z = -4 where y=0.5 and z=-4.5: -4.0\n",
189+
"\n",
190+
"Answer to 2x+5y-z = 27 where x=10.000000000000002, y=0.5, z=-4.5: 27.000000000000004\n",
191+
"\n"
192+
],
193+
"name": "stdout"
194+
}
195+
]
196+
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {
200+
"id": "kDn2tHPsCcpO"
201+
},
202+
"source": [
203+
"### Question 3\n",
204+
"\n",
205+
"x + y = 4\n",
206+
"\n",
207+
"2x + 2y = 8"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"metadata": {
213+
"colab": {
214+
"base_uri": "https://localhost:8080/"
215+
},
216+
"id": "9Ge8JwKMCcpO",
217+
"outputId": "a83f2ff7-65f7-449a-88a5-27ab1c92bccc"
218+
},
219+
"source": [
220+
"# Create a coefficients matrix\n",
221+
"# We add a small value to the diagonal values so we dont end up with a determinant of 0\n",
222+
"coeff_matrix = np.array(\n",
223+
" [[1, 1.00001],\n",
224+
" [2.00001, 2]\n",
225+
" ]\n",
226+
")\n",
227+
"\n",
228+
"# Results Matrix\n",
229+
"result_matrix = np.array(\n",
230+
" [[4],\n",
231+
" [8]]\n",
232+
")\n",
233+
"\n",
234+
"# Calculate the results\n",
235+
"results = np.linalg.inv(coeff_matrix).dot(result_matrix)\n",
236+
"\n",
237+
"# Check Answer\n",
238+
"x = results[0]\n",
239+
"y = results[1]\n",
240+
"\n",
241+
"check_answer6 = x+y\n",
242+
"print(f\"Answer to x+y = 4 where x={x[0]} and y={y[0]}: {check_answer6[0]}\\n\")\n",
243+
"check_answer7 = (2*x) + (2*y)\n",
244+
"print(f\"Answer to 2x+2y = 4 where x={x[0]} and y={y[0]}: {check_answer7[0]}\\n\")"
245+
],
246+
"execution_count": null,
247+
"outputs": [
248+
{
249+
"output_type": "stream",
250+
"text": [
251+
"Answer to x+y = 4 where x=2.666657777794171 and y=1.3333288889261894: 3.9999866667203605\n",
252+
"\n",
253+
"Answer to 2x+2y = 4 where x=2.666657777794171 and y=1.3333288889261894: 7.999973333440721\n",
254+
"\n"
255+
],
256+
"name": "stdout"
257+
}
258+
]
259+
}
260+
]
261+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.