Search code examples
pythonmathdifferential-equationsderivative

How to calculate output of a multivariable equation


I have a function,

f(x,y)=4x^2*y+3x+y 

displayed as

four_x_squared_y_plus_three_x_plus_y = [(4, 2, 1), (3, 1, 0), (1, 0, 1)]

where the first item in the tuple is the coefficient, the second item is the exponent of x, and the third item is the exponent of y. I am trying to calculate the output at a certain value of x and y

I have tried to split the list of terms up, into what they represent and then feed in the values of x and y when I input them however I am getting unsupported operand type regarding ** tuples - even though I tried to split them up into separate values within the terms

Is this an effective method of splitting up tuples like this of have I missed a trick?

def multivariable_output_at(list_of_terms, x_value, y_value):
    coefficient, exponent, intersect = list_of_terms
    calculation =int(coefficient*x_value^exponent*y_value)+int(coefficient*x_value)+int(y_value)
    return calculation


multivariable_output_at(four_x_squared_y_plus_three_x_plus_y, 1, 1) # 8 should be the output

Solution

  • please try this:

    four_x_squared_y_plus_three_x_plus_y = [(4, 2, 1), (3, 1, 0), (1, 0, 1)]
    
    
    def multivariable_output_at(list_of_terms, x_value, y_value):
        return sum(coeff*(x_value**x_exp)*(y_value**y_exp) for coeff,x_exp,y_exp in list_of_terms)
    
    
    print(multivariable_output_at(four_x_squared_y_plus_three_x_plus_y, 1, 1))
    

    NOTICE:

    this is different from how your code originally treated variables, and is based on my intuition of what the list of term means, given your example.

    If you have more examples of input -> output, you should check my answer with all of them to make sure what I did is correct.