0
    from sympy import *
    import numpy as np  

    x= symbols('x') 
    gfg_exp = np.tan(x)-1/x
    intr = solve(gfg_exp, x) 
    print("After Integration : {}".format(intr)) 

I've tried to solve equation with this code but it did not work.

    ----> 5 gfg_exp = np.tan(x)-1/x
    TypeError: loop of ufunc does not support argument 0 of type Symbol which 
    has no callable tan method

I'm sure this is due to tan(x). If this is a bad way, is there a good code to work on the equations in the specific interval by Python? It would be nice to use numerical algorithms, but it would take too much time to get one by one, and it would be inconvenient to set the initial value separately.

Mefitico
  • 816
  • 1
  • 12
  • 41
김세윤
  • 19
  • 2
  • 1
    `numpy` only implements calculations on actual numbers. You need to use `sympy`'s definitions for trigonometric functions, etc. instead, in order to work with `sympy`'s symbolic variables. – jasonharper Sep 14 '20 at 01:33
  • The problem can be reduced to `import sympy; x = sympy.symbols("x"); sympy.solve(sympy.tan(x) - 1 / x, x)` – jkr Sep 14 '20 at 01:41
  • I try to get rid of the tan(x) and replace the equation by x-2 then it gives a root. so problem is np,tan(x) – 김세윤 Sep 14 '20 at 01:42
  • jakub , thank but it didn't be in operation – 김세윤 Sep 14 '20 at 01:46
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). We expect you to do basic research before posting here. "Solve equations in Python" brought up many useful hits; it appears that you did not do even this much. – Prune Sep 14 '20 at 02:15

2 Answers2

1

You did not specify that you need an algebraic/analytical solution... Good, because I don't think there is one. Regarding @jakub's comment to implement as follows:

import sympy
x = sympy.symbols("x")
out = sympy.solve(sympy.tan(x) - 1 / x, x)

That could work, but actually yields the error No algorithms are implemented to solve equation tan(x) - 1/x.

However, note that:

tan(x)=1/x -> x = arctan(1/x)

And notice that the derivative of this expression with regards to x is -1/(1+x²) which in absolute value is always less than one, hence you can make fixed point iterations to get a numerical solution, such as:

import numpy as np

x = 1
tol = 1e-17
while np.abs(np.tan(x) - 1/x) > tol:
    x = np.arctan(1/x)
    print('x =', x)

print('Final error:', np.abs(np.tan(x) - 1/x))

This yields to me:

x = 0.7853981633974483
x = 0.9050225767665427

...
x = 0.8603335890193796
x = 0.8603335890193798
x = 0.8603335890193797
Final error: 0.0

Process finished with exit code 0

Note that I've used x = 1 as my initial guess, but you can always pick the middle of any of your intervals as an initial guess, as long as you don't start with 0, you may need to add an offset in the equation to handle the fact that the arctangent function yields only results in the [-pi, pi] interval.

Mefitico
  • 816
  • 1
  • 12
  • 41
0

If you are in the state of finding value of x , i can help you. you need to give a user input or any variable to x. And there is a library called as math(python inbuild library you need to use it). The equation need to have the variable x in one side . So the equation would be x = tan(x) - 1 . It may work and to calculate tan you need to use math.tan(x) . But you need to assign the value of x the the code would work fine . But practically thinking tan(x) - 1 / x = 0 simply says the the answer for equation is 0. If tan(x) - 1 goes to other side x would be zero. I dont think you expect answer as 0 . So use, "x = math.tan(x) - 1" . Hope this works.