How To Write A Program To Solve Equations

Table of contents:

How To Write A Program To Solve Equations
How To Write A Program To Solve Equations

Video: How To Write A Program To Solve Equations

Video: How To Write A Program To Solve Equations
Video: Matlab Tutorial - 49 - Solving Algebraic Equations 2024, May
Anonim

The rapid development of computer technology has made it possible to facilitate the solution of many problems. If earlier complex equations had to be solved on paper, now you can easily write a program and do it in a few seconds. The most suitable language for this is Python.

Program
Program

Preparing to write a program

Learn the theoretical foundations of solving linear equations before developing your interactive program. This will help you implement your future application code more efficiently.

Build the foundation for the program. The first step is to define the classes. Working with large groups of numbers as classes is easier if your computer resources are limited. This will help increase the usability of your code.

Create rules for the application. A typical example is the value area of the input data. The less free RAM there is on the computer, the less must be entered numbers.

Generating application code

Open a terminal session and invoke the Python interpreter with the following command:

My-iMac: ~ me $ python –v

This will show a long list of all Python modules available in a given version of the program. At the end, the compiler will tell you which version of Python is being used on the computer.

Create a new function definition in Python by entering the following code in the compiler window. Many sources call this function "isolve":

>> def isolve (a, b, c):

The colon will prevent the compiler from immediately interpreting the code when you press enter, and will allow you to finish the job.

Create two variables, q and r, taking the quotient and remainder of the equation with variables a and b, and then call the divmod function, which finds and separates these two numbers. After that, the divisor and the remainder of the operation, if any, will appear on the screen. The code should look like this:

… Q, r = divmod divmod (a, b)

Create an if condition that will quickly output the solution to the equation when there is no remainder. Enter the following:

… If r == 0:

… return ([0, c / b])

Create another condition for the case when there is a remainder:

… else:

… Sol = isolve (b, r, c)

… U = sol [0]

… V = sol [1]

… return ([v, u - q * v])

This will put b and r inside a divmod statement, return them as u and v, and then return them as a set of solutions. The complete code for this program looks like this:

>> def isolve (a, b, c):

… Q, r = divmod (a, b)

… If r == 0:

… return ([0, c / b])

… else:

… Sol = isolve (b, r, c)

… U = sol [0]

… V = sol [1]

… return ([v, u - q * v])

Pay special attention to the clarification after the else and if clauses. Python will not execute this code without proper definition.

Press the return button again to return to the previous line. Enter the function "isolve" and three values for z, y and c and press Return. You should see the following:

>> isolve (5, 17, 103)

[721, -206]

This means that the program is working correctly and there are no errors in the code. Try entering different initial values to check if the calculations are correct.

Recommended: