To put a solution of an equation into a function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: To put a solution of an equation into a function (/thread-14016.html) |
To put a solution of an equation into a function - pianistseb - Nov-11-2018 Hello to all! I am writing a python code for my thesis and I have a simple problem that a cannot solve it. I have a dispersion polynomial equation and I want to find it's solutions and draw them. I wrote this code: from sympy.solvers import solve from sympy import Symbol from sympy import solve, Poly, Eq, Function, exp from sympy.abc import x, y, z, a, b import matplotlib import matplotlib.pyplot as plt import numpy as np from pylab import figure, axes, pie, title, show # Dispersion equation solve omega2=Symbol('omega2') q=Symbol('q') a=Symbol('a') c=Symbol('c') N=Symbol('N') k0=Symbol('k0') s=solve(Poly(omega2**2-(k0**2+q**2+0.25*a**2)*c**2*omega2+k0**2*c**2*N**2), omega2) print("solution 1:") print(s[0]) print() print("solution 2") print(s[1]) # Define functions # here is the issue def sol1(k0,q,a,c,N): return s[0] def sol2(k0,q,a,c,N): return s[1] # Drawing of solutions N=10 a=20.42 k0=np.arange(1,500,1) n=np.arange(1,11,1) x=k0 c=1 plt.figure(1) plt.rc('text', usetex=True) plt.rc('font', family='serif') for i in n: q=i*np.pi y2=sol1(k0,q,a,c,N) # Here functions doesn't work y1=sol2(k0,q,a,c,N) plt.plot(x,y1,'b',x,y2,'r') plt.xlabel(r'$\bar{k}^{2}_{0}$',fontsize=16) plt.ylabel(r'$\bar{\omega}^{2}$',fontsize=16) plt.title(r"Arithmetic Dispersion Diagram", fontsize=16, color='purple') plt.legend([r'$\bar{\omega}_{p}$',r'$\bar{\omega}_{g}$']) plt.ylim(1,500) plt.xlim(1,30) plt.show()The problem is that when I call the functions, if I put some arguments, always the function returns to me the same thing. But the functions depends on k0,q,a,c,N. Of course I can just copy-paste the solutions of the output and fix the bad syntax. But I have to solve 5 problems like that and the other 4 have some monster equations, that are very difficult to copy-paste and fix the syntax. So I need something smarter. RE: To put a solution of an equation into a function - stullis - Nov-11-2018 Your functions aren't doing anything with their arguments. They're just returning the values of "s" calculated on line 18. To correct this, I imagine that you'll need to put line 18 inside your function. Try this: from sympy.solvers import solve from sympy import Symbol from sympy import solve, Poly, Eq, Function, exp from sympy.abc import x, y, z, a, b import matplotlib import matplotlib.pyplot as plt import numpy as np from pylab import figure, axes, pie, title, show # Dispersion equation solve omega2=Symbol('omega2') q=Symbol('q') a=Symbol('a') c=Symbol('c') N=Symbol('N') k0=Symbol('k0') s=solve( Poly( omega2**2-(k0**2+q**2+0.25*a**2)*c**2*omega2+k0**2*c**2*N**2 ), omega2 ) print("solution 1:") print(s[0]) print() print("solution 2") print(s[1]) # Define functions # here is the issue def sol1(k0,q,a,c,N): return solve( Poly( omega2**2-(k0**2+q**2+0.25*a**2)*c**2*omega2+k0**2*c**2*N**2 ), omega2 ) # Drawing of solutions N=10 a=20.42 k0=np.arange(1,500,1) n=np.arange(1,11,1) x=k0 c=1 plt.figure(1) plt.rc('text', usetex=True) plt.rc('font', family='serif') for i in n: q=i*np.pi y2, y1 = sol1(k0,q,a,c,N) # Here functions doesn't work plt.plot(x,y1,'b',x,y2,'r') plt.xlabel(r'$\bar{k}^{2}_{0}$',fontsize=16) plt.ylabel(r'$\bar{\omega}^{2}$',fontsize=16) plt.title(r"Arithmetic Dispersion Diagram", fontsize=16, color='purple') plt.legend([r'$\bar{\omega}_{p}$',r'$\bar{\omega}_{g}$']) plt.ylim(1,500) plt.xlim(1,30) plt.show() RE: To put a solution of an equation into a function - pianistseb - Nov-11-2018 Stulis I tried something like that. And now I tried your code and it says "can't initialize from 'list' without generators". It has problem in the line 56. It says always the same thing when I do that. RE: To put a solution of an equation into a function - stullis - Nov-11-2018 That's bizarre. I found the error in the source code but I don't see why it suddenly starting occurring now and not before. It's being raised by Poly(). There is an error in my version, sol1() needs omega2 as an argument. Perhaps that's the culprit? RE: To put a solution of an equation into a function - pianistseb - Nov-11-2018 Look at this code! def f(x,y): return solve(Poly(x**2*y+(23+y)*x+y**2),y) x=np.arange(-20,20,0.1) y=2 print(f(x,y))I think that the problem is in the dimensions but it has blocked my mind, and I cannot understand. RE: To put a solution of an equation into a function - Gribouillis - Nov-11-2018 You could try def sol1(k0,q,a,c,N): s = solve( Poly( omega2**2-(k0**2+q**2+0.25*a**2)*c**2*omega2+k0**2*c**2*N**2 ), omega2 ) return s[0], s[1] RE: To put a solution of an equation into a function - pianistseb - Nov-11-2018 Thank you! But I have the same problem! |