0

I'm new to this site and new to Python. I remember when I was at school, I wrote a program in BASIC (it was in the eighties) to solve systems of linear equations using matrices.

I would like to do the same in Python and I found it could be done with code like this:

import numpy as np

a = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]])
b = np.array([9, 15, 35])

x = np.linalg.solve(a, b)

print (x)

But I'd like the program to ask for the coefficients so that I don't have to edit it for each equation. I'm sure it's possible, but I've searched for days and found nothing. I wonder if you could give me hints about how to implement that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wilou
  • 1
  • 2
  • By "ask the coefficients", do you mean it should prompt the user to ask for what the numbers are, and then put them into an array? – Oden May 08 '19 at 13:46
  • 1
    Use the `input()` function – Boris Verkhovskiy May 08 '19 at 13:50
  • Thanks for your answers ! :) Yes odensc, that's what I want to do. At first, define the array (in my example, it would be 3), and then, the program would ask me every coefficient (in the previous example, it would be 8, 3, -2, -4, 7, 5...). I know I will have to use the input() function, but I don't know how. I suppose it's in a loop. Thanks ! :) – Wilou May 08 '19 at 15:27

2 Answers2

0

I've found how to do. Here's the code:

import numpy as np

print (" Résolution de systèmes de n équations à n inconnues :")

# Saisie du nombre d’inconnues
print ("\n")
m = int(input(" Nombre d’inconnues ? "))
n = m

mat_a = []
mat_b = []

# Saisie des coefficients - matrice A
print ("\n")
print (" Saisie des coefficients – Matrice [A] :")
for i in range (0,n):
    mat_a.append([])
for i in range (0,m):
    for j in range (0,n):
        mat_a[i].append(j)
        mat_a[i][j]=0
for i in range (0,m):
    for j in range (0,n):
        print (" Coefficient [", i+1,", ",j+1, "] ?", end = " ")
        mat_a[i][j] = eval(input())

# Saisie des coefficients - matrice B
m = 1
print ("\n")
print (" Saisie des coefficients – Matrice [B] :")
for i in range (0,n):
    mat_b.append([])
for i in range (0,n):
    for j in range (0,m):
        mat_b[i].append(j)
        mat_b[i][j]=0
for i in range (0,n):
    for j in range (0,m):
        print (" Coefficient [", i+1,", ",j+1, "] ?", end = " ")
        mat_b[i][j] = eval(input())

# Création des matrices
a = np.array ([mat_a])
b = np.array ([mat_b])

# Résolution du système d’équations       
x = np.linalg.solve(a, b)

# Affichage des solutions
print ("\n")
print (" L’équation admet", n, "solutions. S = {", x, "}.")

# Sortie du programme
print ("\n")
input (" Appuyer sur <Entrée> pour quitter…")

I was helped by a video on youtube: How to make a 2D list or matrix in Python and take a input from user

After a few changes, I managed to write the good code for my little program.

If you want to give it a try, with the following system, for example:

4x + 2y = -1
3x - y = 2

The two solutions are S = {0,3 ; -1,1}.

:)

W.

Wilou
  • 1
  • 2
0

Did a little modification:

    import numpy as np

    print("Resolving linear equations with unknown n:\n")
    m=int(input("Number of unknowns? \n"))
    n=m

    matrix_a=[]
    matrix_b=[]

    print("\n Enter the coefficients of Matrix[A]:\n")

    matrix_a=np.zeros((m,n),dtype=np.int)
    print(matrix_a)
    for i in range (0,m):
        for j in range (0,n):
            print (" Enter Coefficient [", i,",",j, "]?",end=" ")
            matrix_a[i][j] = eval(input())
    print(matrix_a)

    m=1
    print (" \n Enter the coefficients of Matrix [B] :\n")
    for i in range(0,n):
        matrix_b.append([])
    for i in range(0,n):
        for j in range(0,m):
            matrix_b[i].append(j)
            matrix_b[i][j]=0

    for i in range (0,n):
        for j in range (0,m):
            print (" Enter Coefficient [", i,",",j, "]?",end=" ")
            matrix_b[i][j] = eval(input())

    x = np.linalg.solve(matrix_a,matrix_b)
    print("Missing linear equation values:\n",x)
mastisa
  • 1,875
  • 3
  • 21
  • 39