I'm faced with an Hessenberg index-2 DAE, I'm trying to solve it using the python module gekko.
After a few days of trial and error, I think I'm not too far from a code that works. But I've just realized that maybe gekko is not able to handle complex numbers?
Here is a minimal working example:
import numpy as np
from gekko import GEKKO
# Define the simulation and its parameters
g = GEKKO()
g.options.IMODE = 7
g.options.NODES = 1
# define the time array
n_steps = 100
Time = np.linspace(0, 2 * np.pi, n_steps)
g.time = Time
# Initialise the variables
x = g.Var(0.0)
# Write the model's equations
g.Equation(x.dt() == 1.0j * x)
# solve the equations
g.solve(disp = False)
print(x.value)
If I try to run this code, I expect to find the standard complex exponential.
But instead, I get the following error:
File "gekko.py", line 2185, in solve
raise Exception(response)
Exception: #error: Model Expression
*** Error in syntax of function string: Missing operator
Position: 9
$v1-(((1j)*(v1)))
Could you confirm that gekko cannot handle complex numbers? And maybe suggest another python DAE solver that does?
Thank you so much!
Gekko does not natively handle complex numbers. The automatic differentiation and gradient-based solvers haven't been programmed with that in mind. As you discussed in the comments, there are workarounds to solve complex number problems by splitting the variable. There are additional suggestions at Application of complex numbers in Linear Programming?
I am using Python v.3.6 running on the Jupyter QtConsole. I am attempting to do some linear algebra on a dataset using Sympy for a personal project linking predictions with survey scores.
In essence, I set up an augmented matrix, with N = 14 linear equations and M = 5 unknowns, and am trying to solve the system. My problem is that when I use the solve_linear_system command on my augmented matrix, I don't get any output for my code:
import sympy
from sympy import *
from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z, u, v
system = Matrix(((1,1,-1,0,0,1),(1,1,-1,0,0,2),(0,0,-1,0,-1,3),
(0,0,-1,0,-1,2),(0,0,0,1,0,1),(1,0,1,1,-1,2),(0,0,-1,0,-1,2),(1,0,1,0,0,1),
(1,1,1,0,1,3),(1,1,1,0,0,2),(-1,1,0,0,-1,3),(1,-1,-1,-1,0,2),(-1,1,1,1,-1,3),
(0,-1,0,0,0,2)))
solve_linear_system(system, x, y, z, u, v)
>>
Can someone explain what might be the issue and how to remedy the situation? I have tried other matrices and it seems to work with them, so is there something fundamentally wrong with what I am asking Sympy todo or is it the method?
Thank you.
The reason is there are no solutions to the augmented system in reference.
(probably too many constraints, you could try to relax it by eliminating some of the superfluous equations)
If you stare at your matrix for a little while, you will find that there are incompatible equations, for instance, rows 2 & 3: (0,0,-1,0,-1,3), (0,0,-1,0,-1,2), or rows 0 and 1: (1,1,-1,0,0,1),(1,1,-1,0,0,2). There may also be redundant ones.
I would like to solve the equations of the following type in Python. Please let me know if there are any relevant libraries or methods to solve it. ceil(x) denotes the ceiling function.
ceil(x/7)+3*ceil(x/12) = x
This is just a random example, not sure if there is a solution to this.
I tried looking into Sympy library of Python but I can't find a way to use it for my case.
Any help is appreciated. Thanks!
I tried
from sympy import ceiling
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
print(solve(ceiling(x/7) + 3*ceiling(x/12) - x, x))
which ended up in
No algorithms are implemented to solve equation -x + 3*ceiling(x/12) + ceiling(x/7)
The only thing I can think of right now is a graphical solution.
I would like to add a functionality to solve systems of linear equations to my python based editor. I am looking for a module that would be able to parse a string such as the one below:
sample_volume=20
final_concentration=0.55
ethanol_concentration=0.96
final_volume = ethanol_vol+sample_volume
final_concentration*final_volume=ethanol_vol*ethanol_concentration
And solve for the values of all variables.
I have implemented a quick and dirty script using sympy that does this, see here for a Jupyter notebook.
I think that this must have been implemented by someone in a more robust way, I would like to avoid reinventing the wheel here.
Does anyone know of any alternative implementation that are more robust (have tests etc)?
Additionally, according to the sympy docs, sympy.solveset should be used instead of sympy.solve.
I cannot make this work with a list of equations as in my example. Can someone proficient in sympy guide me how to use solveset with an equation system such as this.
In [2]: sample_volume=20
...: final_concentration=0.55
...: ethanol_concentration=0.96
...:
In [4]: fv,ev = symbols('fv,ev')
In [5]: expr1 = fv - ev+sample_volume
In [6]: expr2 = final_concentration*fv - ev*ethanol_concentration
In [7]: solve([expr1,expr2], [fv,ev])
Out[7]: {ev: -26.8292682926829, fv: -46.8292682926829}
# I will recommend to use solveset's linear system solver (linsolve)
In [8]: linsolve([expr1,expr2], [fv,ev])
Out[8]: {(-46.8292682926829, -26.8292682926829)}
how can I minimize a function (uncostrained), respect a[0] and a[1]?
example (this is a simple example for I uderstand scipy, numpy and py):
import numpy as np
from scipy.integrate import *
from scipy.optimize import *
def function(a):
return(quad(lambda t: ((np.cos(a[0]))*(np.sin(a[1]))*t),0,3))
i tried:
l=np.array([0.1,0.2])
res=minimize(function,l, method='nelder-mead',options={'xtol': 1e-8, 'disp': True})
but I get errors.
I get the results in matlab.
any idea ?
thanks in advance
This is just a guess, because you haven't included enough information in the question for anyone to really know what the problem is. Whenever you ask a question about code that generates an error, always include the complete error message in the question. Ideally, you should include a minimal, complete and verifiable example that we can run to reproduce the problem. Currently, you define function, but later you use the undefined function chirplet. That makes it a little bit harder for anyone to understand your problem.
Having said that...
scipy.integrate.quad returns two values: the estimate of the integral, and an estimate of the absolute error of the integral. It looks like you haven't taken this into account in function. Try something like this:
def function(a):
intgrl, abserr = quad(lambda t: np.cos(a[0])*np.sin(a[1])*t, 0, 3)
return intgrl