Python Forum

Full Version: Help with code to solve polynomials equations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, i've been investigating this article..
Article 1: Unidimensional Search Scheme Using Identric Mean for Optimization Problems
Article 2: A NEW UNIDIMENSIONAL SEARCH METHOD FOR OPTIMIZATION
I developed the code based on article 1, but I can't develop the code based on article 2. I don't understand how to solve the RMS method to solve the equations

What I need is to solve the example equations with the methods of the articles and compare their results.

Can you help me with the code for the second article?

CODE Article 1:
from math import exp
from sympy import *

def identricmean(function, A, B, tol=1e-4):
    def fun(x):
        if (len(function) > 1):
            for i in range(len(function)):
                if (function[i] == "<" and x < float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == ">" and x > float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == ">=" and x >= float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == "<=" and x <= float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == "=" and x == float(function[i + 1])):
                    value = eval(function[i - 1])
        else:
            value = eval(function[0])
        return value

    k = 0
    A = A
    C = B
    B = (A + C) / 2
    fA = fun(A)
    fC = fun(C)
    fB = fun(B)

    S = 1 / exp(1.0) * ((A ** A / C ** C)) ** (1 / (A - C))
    fS = fun(S)

    while True:
        k += 1
        fB = min(fA, fB, fC, fS)

        list = sorted([A, B, C, S])

        for i in range(len(list)):
            if fB == fun(list[i]):
                B = list[i]
                A = list[i - 1]
                if (len(list) > i + 1):
                    C = list[i + 1]

                break

        D = 1 / exp(1.0) * ((A ** A / C ** C)) ** (1 / (A - C))
        fD = fun(D)

        if abs((fS - fD) / fS) <= tol:
            f_min = min(fB, fS, fD)
            list_2 = sorted([S, B, D])

            for i in range(len(list_2)):
                if f_min == fun(list_2[i]):
                    x_min = list_2[i]

            break
        else:
            S = D
            fS = fD

    return x_min, f_min


# Example 1
A = 3
B = 2
tol = 1e-3
ans = identricmean(["x**5-5*x**3-20*x+5"], A, B, tol)
print("Example 1: ", ans)

# Example 2
A = 0
B = 15
ans = identricmean(["x**4-8.5*x**3-31.0625*x**2-7.5*x+45"], A, B, tol)
print("Example 2: ", ans)

# Example 3
A = 2
B = 1
ans = identricmean(["5*(x-0.8)**2 + 1", "<", "0.8", "100*(x-0.8)**2 + 1.0", ">=", "0.8"], A, B, tol)
print("Example 3: ", ans)
Hi guys, i solved the problem..

import math
def rms_method(function, A, B, tol=1e-7):
    def func(x):
        if (len(function) > 1):
            for i in range(len(function)):
                if (function[i] == "<" and x < float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == ">" and x > float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == ">=" and x >= float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == "<=" and x <= float(function[i + 1])):
                    value = eval(function[i - 1])
                elif (function[i] == "=" and x == float(function[i + 1])):
                    value = eval(function[i - 1])
        else:
            value = eval(function[0])
        return value
    y = 0
    A = A
    C = B
    B = (A + C) / 2
    fA = func(A)
    fC = func(C)
    fB = func(B)
    x0 = (((A ** 2) + (B ** 2) + (C ** 2)) / 3) ** (1 / 2)
    fx0 = func(x0)
    while True:
        y = y + 1
        fB = min(fA, fB, fC, fx0) # Minimos
        roster = sorted([A, B, C, x0])
        for i in range(len(roster)):
            if fB == func(roster[i]):
                B = roster[i]
                A = roster[i - 1]
                if (len(roster) > i + 1):
                    C = roster[i + 1]
                break
        D = (((A ** 2) + (B ** 2) + (C ** 2)) / 3) ** (1 / 2)
        fD = func(D)
        if abs((fx0 - fD) / fx0) <= tol:
            f_min = min(fB, fx0, fD)
            rol = sorted([x0, B, D])
            for i in range(len(rol)):
                if f_min == func(rol[i]):
                    x_min = rol[i]
            break
        else:
            x0 = D
            fx0 = fD
    return x_min, f_min
tol=1e-7
A = 1
B = 3
ans = rms_method(["x**5-5*x**3-20*x+5"], A, B, tol)
print("example 2.1: ", ans)
A = 3
B = 14
ans = rms_method(["x**4-8.5*x**3-31.0625*x**2-7.5*x+45"], A, B, tol)
print("example 2.2: ", ans)
A = 0
B = 1
ans = rms_method(["5*(x-0.8)**2 + 1", "<", "0.8", "100*(x-0.8)**2 + 1.0", ">=", "0.8"], A, B, tol)
print("example 2.3: ", ans)
A = 1
B = 0
ans = rms_method(["5000*(x-0.1)**2+1", "<", "0.1", "5*(x-0.1)+1", ">", "0.1", "1", "=", "0.1"], A, B, tol)
print("example 2.4: ", ans)
A = 1
B = 14
ans = rms_method(["((x+2)**2)*(x+4)*(x+5)*(x+8)*(x-16)"], A, B, tol)
print("example 2.5: ", ans)