ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 30 May 2012 09:47:24 +0200Issues with: Solving a polynomial equation with multiple variableshttps://ask.sagemath.org/question/9015/issues-with-solving-a-polynomial-equation-with-multiple-variables/It's probably very simple but I'm completely new to Sage and could not find a definitive answer. So here's the problem. I have a polynomial equation like this:
v**6 + a(k)*v**4 + b(k)*v**2 + c(k) == 0
For simple forms of a(k), b(k), c(k) etc. solve works like a charm and gives me a solution v(k) very quickly. When a(k), b(k), c(k) become very complicated e.g.
b(k) = e + f/(1+g*k**2) + (1+(h*k**2)/(1+g*k**2))/(1+g*k**2)
solve function chokes for long time. In such situations I would like to revert to some numerical solution because my final goal is to get numerical values for v(k).
I'm in the very early stages of learning sage and python so have no idea about how to proceed. I have copied my existing code below for your reference. For the parameters Ca, Cak etc. given right now, solve works fine. When I start giving non-zero values of Cak: a(k), b(k), c(k) become complicated functions as described above and I start getting into trouble.
from sage.all import *
import numpy as np
v,k=var('v, k')
Ca=4.; Cs=sqrt(6.0); di=1; de=0.04; Cak=0.;
dx=0.05; L=12.8;
kmin=2*pi/L;
kmax=pi/dx;
def DDe(k):
return 1+k**2*de**2
def Cmk(k):
return sqrt((Ca**2/DDe(k))+Cs**2)
def f(v,k):
return v**6-((Cmk(k)**2)+(Cak**2/DDe(k))*(1+(k**2*di**2)/DDe(k)))*v**4\
+ (1/DDe(k))((Cmk(k)**2*Cak**2)+(Cs**2*Cak**2)*(1+(k**2*di**2)/DDe(k)))*v**2\
- Cs**2*Cak**4/(DDe(k)**2)
sol=solve(f(v,k) == 0, v)
for i in range(0,len(sol)):
print 'Solution ', i, ': '; show(sol[i])
# plot(sol[i].rhs(),kmin,kmax)
show(plot(sol[0].rhs(),kmin,kmax)+plot(sol[1].rhs(),kmin,kmax)+\
plot(sol[2].rhs(),kmin,kmax)+plot(sol[3].rhs(),kmin,kmax))
Any help, pointers on how to proceed would be of great help.
**EDIT**
I just compared the output of the code with a similar code in Mathematica from which I'm trying to translate the code and found that the numbers in the simpler cases are wrong too. So it seems I'm doing more than a few things wrong. :(
Thanks!Tue, 29 May 2012 19:20:13 +0200https://ask.sagemath.org/question/9015/issues-with-solving-a-polynomial-equation-with-multiple-variables/Answer by niles for <p>It's probably very simple but I'm completely new to Sage and could not find a definitive answer. So here's the problem. I have a polynomial equation like this:</p>
<pre><code>v**6 + a(k)*v**4 + b(k)*v**2 + c(k) == 0
</code></pre>
<p>For simple forms of a(k), b(k), c(k) etc. solve works like a charm and gives me a solution v(k) very quickly. When a(k), b(k), c(k) become very complicated e.g.</p>
<pre><code>b(k) = e + f/(1+g*k**2) + (1+(h*k**2)/(1+g*k**2))/(1+g*k**2)
</code></pre>
<p>solve function chokes for long time. In such situations I would like to revert to some numerical solution because my final goal is to get numerical values for v(k).</p>
<p>I'm in the very early stages of learning sage and python so have no idea about how to proceed. I have copied my existing code below for your reference. For the parameters Ca, Cak etc. given right now, solve works fine. When I start giving non-zero values of Cak: a(k), b(k), c(k) become complicated functions as described above and I start getting into trouble.</p>
<pre><code>from sage.all import *
import numpy as np
v,k=var('v, k')
Ca=4.; Cs=sqrt(6.0); di=1; de=0.04; Cak=0.;
dx=0.05; L=12.8;
kmin=2*pi/L;
kmax=pi/dx;
def DDe(k):
return 1+k**2*de**2
def Cmk(k):
return sqrt((Ca**2/DDe(k))+Cs**2)
def f(v,k):
return v**6-((Cmk(k)**2)+(Cak**2/DDe(k))*(1+(k**2*di**2)/DDe(k)))*v**4\
+ (1/DDe(k))((Cmk(k)**2*Cak**2)+(Cs**2*Cak**2)*(1+(k**2*di**2)/DDe(k)))*v**2\
- Cs**2*Cak**4/(DDe(k)**2)
sol=solve(f(v,k) == 0, v)
for i in range(0,len(sol)):
print 'Solution ', i, ': '; show(sol[i])
# plot(sol[i].rhs(),kmin,kmax)
show(plot(sol[0].rhs(),kmin,kmax)+plot(sol[1].rhs(),kmin,kmax)+\
plot(sol[2].rhs(),kmin,kmax)+plot(sol[3].rhs(),kmin,kmax))
</code></pre>
<p>Any help, pointers on how to proceed would be of great help.</p>
<p><strong>EDIT</strong></p>
<p>I just compared the output of the code with a similar code in Mathematica from which I'm trying to translate the code and found that the numbers in the simpler cases are wrong too. So it seems I'm doing more than a few things wrong. :(</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/9015/issues-with-solving-a-polynomial-equation-with-multiple-variables/?answer=13633#post-id-13633For numerical solutions, I would suggest [`find_root`](http://www.sagemath.org/doc/reference/sage/numerical/optimize.html#sage.numerical.optimize.find_root) or [`minimize`](http://www.sagemath.org/doc/reference/sage/numerical/optimize.html#sage.numerical.optimize.minimize). See the links for examples and usage.Wed, 30 May 2012 09:47:24 +0200https://ask.sagemath.org/question/9015/issues-with-solving-a-polynomial-equation-with-multiple-variables/?answer=13633#post-id-13633