Python Forum
To put a solution of an equation into a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To put a solution of an equation into a function
#1
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.
Reply
#2
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()
Reply
#3
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.
Reply
#4
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?
Reply
#5
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.
Reply
#6
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]
Reply
#7
Thank you! But I have the same problem!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  are these equation to PyOpenCV API function conversions correct? jasper13 0 2,269 Oct-06-2018, 03:03 PM
Last Post: jasper13
  need help with solution-checker function hereathome 4 3,519 Jan-10-2018, 01:40 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020