Error in Derivative using Sympy

369 views Asked by At

I'm having a problem using sympy to find partial derivative of a function

import sympy
import mpmath
import numpy as np
import math

Sa = 200
Sm = 100

mu1 = 310
sigma1 = 15

mu2 = 95
sigma2 = 5

f = 0.9

u1, u2 = sympy.symbols('u1 u2', real=True)
Snf = Sa/(1-Sm/(sigma1*u1 + mu1))
a = ((f*(sigma1*u1 + mu1))**2)/(sigma2*u2 + mu2)
b = (-1/3)*sympy.log(f*(sigma1*u1 + mu1)/(sigma2*u2 + mu2),10)

fu1 = sympy.diff((Snf/a)**(1/b),u1).subs({u1:8.11656443, u2:13.4526774})
print(fu1)

fu2 = sympy.diff((Snf/a)**(1/b),u2).subs({u1:8.11656443, u2:13.4526774})
print(fu2)

When I run it, I have the answer but the answer looks like?

enter image description here

Is there something wrong with my code?? Or do I need to calculate that number myself??

When I try a function without the log, the answer is just simple number though, just when I try function with log it turns like that

1

There are 1 answers

0
eyllanesc On BEST ANSWER

Use simplify function from sympy.

import sympy
import mpmath
import numpy as np
import math

Sa = 200
Sm = 100

mu1 = 310
sigma1 = 15

mu2 = 95
sigma2 = 5

f = 0.9

u1, u2 = sympy.symbols('u1 u2', real=True)
Snf = Sa/(1-Sm/(sigma1*u1 + mu1))
a = ((f*(sigma1*u1 + mu1))**2)/(sigma2*u2 + mu2)
b = (-1/3)*sympy.log(f*(sigma1*u1 + mu1)/(sigma2*u2 + mu2),10)

fu1 = sympy.diff((Snf/a)**(1/b),u1).subs({u1:8.11656443, u2:13.4526774})
print(sympy.simplify(fu1))

fu2 = sympy.diff((Snf/a)**(1/b),u2).subs({u1:8.11656443, u2:13.4526774})

print(sympy.simplify(fu2))

Output:

5510.92194972571
2661.80497262362