Hyunseo Lee
Hyunseo Lee

Reputation: 163

How to solve Equation without any Modules in Python?

I want to solve this equation without any Modules(NumPy, Sympy... etc.)

Px + Qy = W

(ex. 5x + 6y = 55)

Thanks.

Upvotes: 0

Views: 3671

Answers (2)

Sqoshu
Sqoshu

Reputation: 1014

It is a very crude way to do this, but you can use brute-force technique, as I said in comment under your question. It can probably be optimized a lot, gives only int outputs, but overall shows the method:

import numpy as np

# Provide the equation:
print("Provide a, b and c to evaluate in equation of form {ax + by - c = 0}")
a = float(input("a: "))
b = float(input("b: "))
c = float(input("c: "))

x_range = int(input("x-searching range (-a, a): "))
y_range = int(input("y-searching range (-b, b): "))
error = float(input("maximum accepted error from the exact solution: "))

x_range = np.arange(-x_range, x_range, 1)
y_range = np.arange(-y_range, y_range, 1)

for x in x_range:
    for y in y_range:
        if -error <= a * x + b * y - c <= error:
            print("Got an absolute error of {} or less with numbers x = {} and y = {}.".format(error, x, y))

Example output for a = 1, b = 2, c = 3, x_range = 10, y_range = 10, error = 0.001:

Got an error of 0.001 or less with numbers x = -9 and y = 6.
Got an error of 0.001 or less with numbers x = -7 and y = 5.
Got an error of 0.001 or less with numbers x = -5 and y = 4.
Got an error of 0.001 or less with numbers x = -3 and y = 3.
Got an error of 0.001 or less with numbers x = -1 and y = 2.
Got an error of 0.001 or less with numbers x = 1 and y = 1.
Got an error of 0.001 or less with numbers x = 3 and y = 0.
Got an error of 0.001 or less with numbers x = 5 and y = -1.
Got an error of 0.001 or less with numbers x = 7 and y = -2.
Got an error of 0.001 or less with numbers x = 9 and y = -3.

I am using numpy, but not a built-in function to solve the equation itself, just to create an array. This can be done without it, of course.

Upvotes: 1

Gsk
Gsk

Reputation: 2955

There are thousands of ways to solve an equation with python.

One of those is:

def myfunc (x=None, y=None):
    return ((55-6*y)/5.0) if y else ((55-5*x)/6.0)


print(myfunc(x=10)) # OUTPUT: 0.833333333333, y value for x == 10
print(myfunc(y=42)) # OUTPUT: -39.4, x value for y == 42

You simply define inside a function the steps required to solve the equation.
In our example, if we have y value we subtract 6*y to 55 then we divide by 5.0 (we add .0 to have a float as result), otherwise (means we have x) we subtract 5*x from 55 and then we divide by 6.0

with the same principle, you can generalize:

def myfunc (x=None, y=None, P=None, Q=None, W=None):
    if not W:
        return P*x + Q*y
    elif not x:
        return (W-Q*y)/float(P)
    elif not y:
        return (W-P*x)/float(Q)
    elif not P:
        return (W-Q*y)/float(x)
    elif not Q:
        return (W-P*x)/float(y)


print(myfunc(x=10, P=5, Q=6, W=55)) # OUTPUT: 0.833333333333, y value for x == 10
print(myfunc(y=42, P=5, Q=6, W=55)) # OUTPUT: -39.4, x value for y == 42

check this QA for some other interesting ways to approach this problem

Upvotes: 1

Related Questions