Python Forum
Help with code to solve polynomials equations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with code to solve polynomials equations
#1
Question 
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)
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code challenge I need to solve for a Bootcamp ramonrocha 1 463 Sep-05-2023, 03:41 PM
Last Post: deanhystad
Heart how to solve complex equations in python HoangF 3 2,733 Dec-26-2021, 07:04 PM
Last Post: HoangF
  Solve system of equations Sancho_Pansa 19 8,706 Oct-27-2020, 08:15 AM
Last Post: Sancho_Pansa
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,645 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Solve a system of linear equations with binary variables lopeslimagabriel 3 2,443 Sep-24-2020, 07:09 AM
Last Post: scidam
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,796 May-29-2020, 04:06 PM
Last Post: Euler2
  How to solve difficult non-linear equations? shreeniket 3 2,348 Apr-23-2020, 01:36 AM
Last Post: shreeniket
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,685 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  System of 3 non-linear equations in 3 unknowns (how-to-solve?) samsonite 2 3,539 Mar-23-2019, 10:14 AM
Last Post: samsonite

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020