LuckyLuke
LuckyLuke

Reputation: 21

solve an equation (with max-statement) in python

i am seeking a solution for the following equation in Python.

    345-0.25*t = 37.5 * x_a    
    'with'
    t = max(0, 10-x_a)*(20-10) + max(0,25-5*x_a)*(3-4) + max(0,4-0.25*x_a)*(30-12.5)
    'x_a = ??'

If there is more than one solution to the problem (I am not even sure, whether this can happen from a mathematical point of view?), I want my code to return a positive(!) value for x_a, that minimizes t.

With my previous knowledge in the Basics of Python, Pandas and NumPy I actually have no clue, how to tackle this problem. Can someone give me a hint?

For Clarification: I inserted some exemplary numbers in the equation to make it easier to gasp the problem. In my final code, there might of course be different numbers for different scenarios. However, in every scenario x_a is the only unknown variable.

Update
I thought about the problem again and came up with the following solution, which yields the same result as the calculations done by Michał Mazur:

import itertools
from sympy import Eq, Symbol, solve
import numpy as np

x_a = Symbol('x_a')
possible_elements = np.array([10-x_a, 25-5*x_a, 4-0.25*x_a])
assumptions = np.array(list(itertools.product([True, False], repeat=3)))

for assumption in assumptions:
    x_a = Symbol('x_a')
    elements = assumption.astype(int) * possible_elements
    
    t = elements[0]*(20-10) + elements[1]*(3-4) + elements[2]*(30-12.5)
    eqn = Eq(300-0.25*t, 40*x_a)
    
    solution = solve(eqn)
    if len(solution)>2:
        print('Warning! the code may suppress possible solutions')
    if len(solution)==1:
        solution = solution[0]
        if (((float(possible_elements[0].subs(x_a,solution))) > 0) == assumption[0]) &\
        (((float(possible_elements[1].subs(x_a,solution))) > 0) == assumption[1]) &\
        (((float(possible_elements[2].subs(x_a,solution)))> 0) == assumption[2]):
            print('solution:', solution)

Compared to the already suggested approach this may have an little advantage as it does not rely on testing all possible values and therefore can be used for very small as well as very big solutions without taking a lot of time (?). However, it probably only is useful as long as you don't have more complex functions for t (even having for example 5 max(...) statements and therefore (2^5=)32 scenarios to test seems quite cumbersome).

As far as I'm concerned, I just realized that my problem is even more complex as i thought. For my project the calculations needed to derive the value of "t" are pretty entangled and can not be written in just one equation. However it still is a function, that only relies on x_a. So I am still hoping for a Python-Solution similar to the proposed Solver in Excel... or I will stick to the approach of simply testing out all possible numbers.

Upvotes: 0

Views: 471

Answers (1)

Michał Mazur
Michał Mazur

Reputation: 127

If you are interested in a solution a bit different than the Python one, then I will give you a hand. Open up your Excel, with Solver extention and plug in the data you are interested in cheking, as the following: enter image description here

Into the E2 you plug the command I just have writen, into E4 you plug in

=300-0,25*E2

Into the F4 you plug:

=40*F2

Then you open up your Solver menu

enter image description here

Into the Set Objective you put the variable t, which you want to minimize. Into Changing Variables you put the a. Into Constraint Menu you put the equality of E4 and F4 cells. You check the "Make Unconstarained Variables be non-negative" which will prevent your a variable to go below 0. Your method of computing is strictly non-linear, so you leave this option there.

You click solve. The computed value is presented in the screen.

The python approach I can think of:

minimumval=10100
minxa=10000
eps=0.01
for i in range(100000):
    k=i/10000
    x_a=k
    t = max(0, 10-x_a)*(20-10) + max(0,25-5*x_a)*(3-4) + max(0,4-0.25*x_a)*(30-12.5)
    val=abs(300-0.25*t-40*x_a)
    if (val<eps):
        if t<minimumval:
            minimumval=t
            minxa=x_a
    

It isn't direct solution, as in it only controls the error that you make in the equality by eps value. However, it gives solution.

Upvotes: 0

Related Questions