0% found this document useful (0 votes)
37 views

Python#GRUPO4

The document contains several examples of solving systems of linear equations using NumPy in Python. Each example defines the coefficients matrix A and constant term vector b, then uses NumPy functions like np.linalg.solve() to calculate the solution vector x.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Python#GRUPO4

The document contains several examples of solving systems of linear equations using NumPy in Python. Each example defines the coefficients matrix A and constant term vector b, then uses NumPy functions like np.linalg.solve() to calculate the solution vector x.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

8/6/23, 20:17 practica 1.

- Colaboratory

Grupo 4:

Tema: Sistema de Ecuaciones

2x + 3y = 5

4x + 5y = 9

2 3
( )
4 5

5
( )
9

1 import numpy as np
2 a=np.array([[2,3],[4,5]])
3 b=np.array([5,9])
4 c=np.linalg.inv(a)
5 np.dot(c,b)
6

array([1., 1.])

6x + 2y − 4Z = 8

2x + 4y − 3z = 9

8x + 6y + 5z = 7

6 2 −4
⎛ ⎞

⎜2 4 −3 ⎟
⎝ ⎠
8 6 5

8
⎛ ⎞

⎜9 ⎟
⎝ ⎠
7

1 A=np.array([[3,2,-4],
2            [-2,4-3],
3             [4,6,5]])
4
5 b=np.array([8,9,7])
6
7

<ipython-input-22-45dcac8800de>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a lis
A=np.array([[3,2,-4],

1 Aum=np.column_stack((A,b))
2 Aum

array([[list([3, 2, -4]), 8],


[list([-2, 1]), 9],
[list([4, 6, 5]), 7]], dtype=object)

10x − y + 2z = 6

− x + 11y − z + 3w = 25

2x − y + 10z − w = −11

3y − z + 8w = 15

10 −1 2 0
⎛ ⎞

⎜ −1 11 −1 3 ⎟
⎜ ⎟
⎜ 2 −1 10 −1 ⎟
⎝ ⎠
3 −1 8 0

6
⎛ ⎞

⎜ 25 ⎟
⎜ ⎟
⎜ −11 ⎟

⎝ ⎠
15

1 import numpy as np
2 import numpy.linalg as lin
3 A=np.array([[10,-1,2,0],[-1,11,-1,3],[2,-1,10,-1],[3,-1,8,0]])
4 B=np.array([[6],[25],[-11],[15]])
5 x=lin.solve(A,B)
6 print("la solucion  del sistema es:")
7 print("x=",x[0])

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 1/7
8/6/23, 20:17 practica 1. - Colaboratory
8 print("y=",x[1])
9 print("z=",x[2])
10 print("w=",x[3])

la solucion del sistema es:


x= [-0.2]
y= [-5.46666667]
z= [1.26666667]
w= [28.73333333]

2x + 4y + 6z = 18

4x + 5y + 6z = 20

3x + y − 2z = 4

2 4 6
⎛ ⎞

⎜4 5 6 ⎟
⎝ ⎠
3 1 −2

18
⎛ ⎞

⎜ 20 ⎟
⎝ ⎠
4

1 import numpy as np
2 a=np.array ([[2,4,6],[4,5,6],[3,1,-2]])
3 b=np.array([18,24,4])
4 x=np.linalg.solve(a,b)
5 print(x)

[ 4. -2. 3.]

2x − 6y = −14

− 4x + 2y + z = 11/2

1 from sympy import*
2 x,y,z=symbols('x y z')
3 linsolve([2*x-6*-14,4*x+2*y+z-11/2],(x,y,z))

{(−42.0,  86.75 − 0.5z,  1.0z )}

Haz doble clic (o pulsa Intro) para editar

1 A = np.array([[1, -1], [2, 4]])
2 B = np.array([[-0.5, 1./3.], [0.5, -2./3.]])
3 L = np.diag([2., 3.])
4
5 print('A=\n', A)
6 print('B=\n', np.round(B, 1))
7 print('L=\n', L)

A=
[[ 1 -1]
[ 2 4]]
B=
[[-0.5 0.3]
[ 0.5 -0.7]]
L=
[[2. 0.]
[0. 3.]]

2x + 4y + 6z = 22

3x + 8y + 5z = 27

− x + y − 2z = 2

1 A=np.array([[2,4,6],
2            [3,8,5],
3            [-1,1,2]])
4
5 b=np.array([22,27,2])

1 Aum=np.column_stack((A,b))
2 Aum

array([[ 2, 4, 6, 22],
[ 3, 8, 5, 27],
[-1, 1, 2, 2]])

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 2/7
8/6/23, 20:17 practica 1. - Colaboratory
1 np.linalg.det(A)

44.000000000000014

3X + 5Y + 8Z = 20

1X + 2Y + 7Z = 14

− X + 1Y + 2Y = 4

1 import numpy as np
2 A=np.array([[3,5,8],
3             [1,2,7],
4             [-1,1,2]])
5
6 b=np.array([20,14,4])
7
8

1 Aum=np.column_stack((A,b))
2 Aum

array([[ 3, 5, 8, 20],
[ 1, 2, 7, 14],
[-1, 1, 2, 4]])

1 np.linalg.det(A)

-30.000000000000014

1 np.dot(np.linalg.inv(A),b)

array([0.4, 1.2, 1.6])

3x + 2y + z − 500 = 0

5x + 3y + 2z − 900 = 0

2x + 4y + z − 550 = 0

1 from sympy import*
2 x,y,z=symbols('x y z')
3 linsolve([3*x+2*y+z-500,5*x+3*y+2*-900,2*x+4*y+z-550],(x,y,z))

3450 2050 7950


{( ,  ,  − )}
13 13 13
​ ​ ​

3X + 2Y − 2Z = 0

2X − Y + 3Z = 9

X + 4Y + 2Z = −4

1 import numpy as np
2 A=np.array([[3,2,-2],[2,-1,3],[1,4,2]])
3 b=np.array([0,9,-4])
4 solucion=np.linalg.solve(A,b)
5 print(solucion)

[ 2. -2. 1.]

5x + 2y + 6z + 1w = 1

− 3x + 3 + 2z + 8wy = 5

2x + y + 6z + w = 6

4x + z + 3 = 14

1 import numpy as np
2 import numpy.linalg as lin
3 A=np.array([[5,2,6,1],[-3,3,2,8],[2,1,6,0],[4,0,1,3]])
4 b=np.array([1,5,6,14])
5 x=lin.solve (A,b)
6 print("la solucion es:")
7 print("w",x[0])
8 print("x",x[1])
9 print("z",x[2])
10 print("y",x[3])
11 s=np.allclose(np.dot(A,x),b)
12 print(s)

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 3/7
8/6/23, 20:17 practica 1. - Colaboratory

la solucion es:
w 0.22253521126760561
x -9.216901408450704
z 2.4619718309859158
y 3.549295774647888
True

−7x + 1y + 3z + w = 20

2x + y + 6z + 8w = 50

y + 5z + 3w = 9

2x + 5z + 4w = 46

Haz doble clic (o pulsa Intro) para editar

1 import numpy as np
2 import numpy.linalg as lin
3 A=np.array([[-7,1,3,1],[2,1,6,8],[0,1,5,3],[2,0,5,4]])
4 b=np.array([20,50,9,46])
5 x=lin.solve (A,b)
6 print("la solucion es:")
7 print("w",x[0])
8 print("x",x[1])
9 print("z",x[2])
10 print("y",x[3])
11 s=np.allclose(np.dot(A,x),b)
12 print(s)

la solucion es:
w -5.362204724409447
x -38.11023622047244
z 3.6535433070866183
y 9.614173228346452
True

1x + 4y + z = 1

4x − 3y + 2z = −5

− 8x − 1 − 6 = 3

1 import numpy as np
2 A=np.array([[1,4,8],[4,-3,2],[-8,-1,-6]])
3 b=np.array([1,-5,3])
4 print("multiplicacion A*b")
5 print(A*b)

multiplicacion A*b
[[ 1 -20 24]
[ 4 15 6]
[ -8 5 -18]]

9x − 17z + 4y = 90

− 4z + 7y = 30

− x + z − y = −1

1 import numpy as np
2 C=np.array([[9,-17,4],[0,-4,7],[-1,-1,-1]])
3 d=np.array([90,30,-1])
4 print("multiplicacion C*d")
5 print("inv d*C")
6 print((C*d),d*C)

multiplicacion C*d
inv d*C
[[ 810 -510 -4]
[ 0 -120 -7]
[ -90 -30 1]] [[ 810 -510 -4]
[ 0 -120 -7]
[ -90 -30 1]]

9x − 3y = 26

− 8x + 5y = 15

1 import numpy as np
2 a=np.array([[9,-3],[-8,5]])
3 b=np.array([26,15])
4 c=np.linalg.inv(a)

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 4/7
8/6/23, 20:17 practica 1. - Colaboratory
5 np.dot(c,b)
6

array([ 8.33333333, 16.33333333])

1 import numpy as np
2 import scipy as sp  
3 #se crea la matriz de coeficientes del sistema a resolver 
4 A=np.array([[6,0,-1,0,0],
5             [3,-3,0,0,0], 
6             [0,-1,9,0,0], 
7             [0,1,8,-11,2],
8             [3,1,0,0,-4]])
9 b=np.array([50,0,160,0,0])
10 A
11

array([[ 6, 0, -1, 0, 0],


[ 3, -3, 0, 0, 0],
[ 0, -1, 9, 0, 0],
[ 0, 1, 8, -11, 2],
[ 3, 1, 0, 0, -4]])

1 import numpy as np
2 c=np.linalg.inv(A)
3 np.dot(A,b)
4

array([ 140, 150, 1440, 1280, 150])

2x + 4y + 2z = 8

x + z − 3w = 7

4x + 2y − 1z = 1

7x + 4y − 12z − 4w = 3

1 import numpy as np
2 import numpy.linalg as lin 
3 A=np.array([[2,4,2,0],[1,0,1,-3],[4,2,-1,0],[7,4,-12,-4]])
4 b=np.array([[8,7,1,3]])
5 A
6 print("la solucion es:")
7 print("w",x[0])
8 print("x",x[1])
9 print("z",x[2])
10 print("y",x[3])
11 s=np.allclose(np.dot(A,x),b)
12 print(s)

la solucion es:
w -5.362204724409447
x -38.11023622047244
z 3.6535433070866183
y 9.614173228346452
False

1 from sympy import *
2
3
4 #Se define las variables simbolicas x,y,z
5
6 x = Symbol('x')
7
8 y = Symbol('y')
9
10 z= Symbol('z')
11
12
13 #Resolver el sistema de ecuaciones
14
15 #3x+9y-10z  =   24
16
17 #x-6y+4z      =   -4
18
19 #10x-2y+8z  =  20
20
21 resultado =solve([3*x+9*y-10*z-24,x-6*y+4*z+4,10*x-2*y+8*z-20],[x,y,z])
22 print(resultado)
23

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 5/7
8/6/23, 20:17 practica 1. - Colaboratory

{x: 308/103, y: 42/103, z: -117/103}

6x + 2y − 4z = 13

4x + 7y − z = 4

− 5x − y − 8z = 20

1 import numpy as np
2 A=np.array([[6,2,-4],[4,7,-1],[-5,-1,-8]])
3 b=np.array([13,4,20])
4 c=np.linalg.inv(A)
5 np.dot(c,b)

array([ 0.35969388, -0.02295918, -2.72193878])

11x + 4y − 5z = 7

6x + 3y + 2z = 20

9x − 4y + 7z = 23

1 import numpy as np
2 A=np.array([[11,4,-5],[6,3,2],[9,-4,7]])
3 b=np.array([7,20,23])
4 c=np.linalg.inv(A)
5 np.dot(c,b)

array([1.19665272, 2.25104603, 3.0334728 ])

Haz doble clic (o pulsa Intro) para editar

−43x + 2y − z = 4

11x + 5y + z = 9

− 5x − 2y + 5z = 9

1 import numpy as np
2 A=np.array([[-43,2,-1],[11,5,1],[-5,-2,5]])
3 b=np.array([4,9,9])
4 c=np.linalg.inv(A)
5 np.dot(c,b)

array([-0.0771028 , 1.5046729 , 2.32476636])

−x − y + 6z = 10

− 6x + y + 12z = 48

7x + 8y − 3 = 12

1 import numpy as np
2 A=np.array([[-1,-1,6],[-6,1,12],[7,8,-3]])
3 b=np.array([10,48,12])
4 c=np.linalg.inv(A)
5 np.dot(c,b)
6

array([-3.21212121, 5.05050505, 1.97306397])

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 6/7
8/6/23, 20:17 practica 1. - Colaboratory

https://colab.research.google.com/drive/1GHMjsSFY-AcGhdqXBEZJmynlNly9sPZh#scrollTo=FXYv9RRXfweB 7/7

You might also like