20TD02U Programmer som løser lineære likningssett - itnett/FTD02H-N GitHub Wiki

https://ndla.no/subject:1:5a5cac3f-46ff-4f4d-ba95-b256a706ec48/topic:c7111b35-0621-4977-8a31-fe41e8e4a34d/topic:b4ba45c3-b084-45a1-994c-c85eb755ee54/resource:208cde83-f2bf-4061-83e9-675b143cd4d7

Here is a Python program that solves a system of linear equations using the substitution method. This program will prompt the user to input the coefficients of the equations and then compute the solution.

def solve_linear_equations(a, b, c, d, e, f):
    """
    Solve the system of linear equations:
    ax + by = c
    dx + ey = f
    """
    # Check if the equations have a unique solution
    determinant = a * e - b * d
    if determinant == 0:
        return "The system has no unique solution."

    # Solve for y
    y = (c * d - a * f) / determinant

    # Solve for x using the value of y
    x = (c - b * y) / a

    return x, y

def main():
    print("This program solves a system of linear equations of the form:")
    print("ax + by = c")
    print("dx + ey = f")
    
    # Get coefficients from the user
    a = float(input("Enter coefficient a: "))
    b = float(input("Enter coefficient b: "))
    c = float(input("Enter constant c: "))
    d = float(input("Enter coefficient d: "))
    e = float(input("Enter coefficient e: "))
    f = float(input("Enter constant f: "))
    
    solution = solve_linear_equations(a, b, c, d, e, f)
    
    if isinstance(solution, str):
        print(solution)
    else:
        x, y = solution
        print(f"The solution is x = {x}, y = {y}")

if __name__ == "__main__":
    main()

Explanation:

  1. Function solve_linear_equations:

    • Calculates the determinant to check if there is a unique solution.
    • If the determinant is zero, the system does not have a unique solution.
    • Otherwise, it solves for y and then uses y to solve for x.
  2. Main Function:

    • Introduces the program to the user.
    • Prompts the user to input the coefficients and constants of the equations.
    • Calls the solve_linear_equations function with these inputs.
    • Prints the solution if there is one; otherwise, it informs the user that there is no unique solution.

Usage:

  1. Run the program.
  2. Enter the coefficients and constants when prompted.
  3. The program will output the solution or inform you if there is no unique solution.

Feel free to test this program with different sets of coefficients to verify its functionality.

Below is the Python program based on your detailed step-by-step instructions for solving a system of linear equations using the substitution method:

def main():
    # Inform the user about the program
    print("Dette programmet løser likningssettet ax + by = c , dx + ey = f.")
    
    # Get coefficients from the user
    a = float(input("Skriv inn verdien til konstanten a: "))
    b = float(input("Skriv inn verdien til konstanten b: "))
    c = float(input("Skriv inn verdien til konstanten c: "))
    d = float(input("Skriv inn verdien til konstanten d: "))
    e = float(input("Skriv inn verdien til konstanten e: "))
    f = float(input("Skriv inn verdien til konstanten f: "))
    
    # Calculate y
    denominator = (a * e - b * d)
    if denominator == 0:
        print("Likningssettet har ingen unik løsning.")
        return
    
    y = (c * d - a * f) / denominator
    
    # Calculate x
    x = (c - b * y) / a
    
    # Display the solution
    print(f"Løsningen er x = {x}, y = {y}.")

if __name__ == "__main__":
    main()

Explanation:

  1. User Interface:

    • The program starts by informing the user what it does.
    • It then prompts the user to enter the values of the constants a, b, c, d, e, and f.
    • These inputs are converted to floating-point numbers and stored in corresponding variables.
  2. Calculation:

    • The program calculates the denominator (a * e - b * d) to check if the system of equations has a unique solution.
    • If the denominator is zero, it prints that there is no unique solution and exits.
    • Otherwise, it calculates y using the formula (c * d - a * f) / (a * e - b * d).
    • Then, it calculates x using the formula (c - b * y) / a.
  3. Output:

    • Finally, it prints the solution for x and y.

This program handles input, computation, and output as per your instructions, ensuring clarity and correctness in solving the system of linear equations.