So, if the title is not clear, I am trying to take the function [sin(x)/(x3-2*x2+4x-8)] and find the integral. I know that the analytical solution is (-pi/(8e**2))+(pi/8)*cos(2) or ~ -0.2165...
The code that I can not seem create properly so far looks like
import numpy as np
from sympy import integrate
from sympy.abc import x
f = integrate(np.sin(x)/(x**3-2*x**2+4x-8), (x, -5, 2))
print(f)
I would like the code to eventually be drawn out which I plan on doing myself, however, this gives me the error
"TypeError: loop of ufunc does not support argument 0 of type Symbol which has no callable sin method"
How do I go about fixing this?
I used sympy to derive, via lagrange, the equations of motion of my 3 link robot. The resultant equation of motion in the form (theta_dot_dot = f(theta, theta_dot)) turned out very complicated with A LOT of cos and sin. I then lambdified the functions to use with drake, replacing all the sympy.sin and sympy.cos with drake.sin, drake.cos.
The final function can be evaluated numerically (i.e. given theta, theta_dot, find theta_dot_dot) somewhat efficiently in the milliseconds range.
I then tried to use direct transcription to do trajectory optimization. Note I did not use the DirectTranscription library, instead manually added the necessary constraints.
The constraints are added roughly as follows:
for i in range(NUM_TIME_STEPS-1):
print("Adding constraints for t = " + str(i))
tau = mp.NewContinuousVariables(3, "tau_%d" % i)
next_state = mp.NewContinuousVariables(8, "state_%d" % (i+1))
for j in range(8):
mp.AddConstraint(next_state[j] <= (state_over_time[i] + TIME_INTERVAL*derivs(state_over_time[i], tau))[j])
mp.AddConstraint(next_state[j] >= (state_over_time[i] + TIME_INTERVAL*derivs(state_over_time[i], tau))[j])
state_over_time[i+1] = next_state
tau_over_time[i] = tau
The problem I'm facing right now is that on each iteration of adding constraints, I observe that my memory usage increases by around 70-100MB. This means that my number of time steps cannot go more than around 50 before the program crashes due to out of memory.
I'm wondering what I can do to make trajectory optimization work for my robot. Obviously I can try to simplify (by hand or otherwise) the equations of motions... but is there anything else I can try? Is it even normal that the constraints are taking up so much memory? Am I doing something very wrong here?
You're pushing drake's symbolic through your complex equations. Making that better is a good goal, but probably you want to avoid it by using the other overload for AddConstraint:
AddConstraint(your_method, lb, ub, vars)
https://drake.mit.edu/pydrake/pydrake.solvers.mathematicalprogram.html?highlight=addconstraint#pydrake.solvers.mathematicalprogram.MathematicalProgram.AddConstraint
That will use your python code as a function pointer, and should use autodiff instead of symbolic.
I want to compute remainders in Python for multivariate polynomials and I found that div() from sympy should do the trick (I also need sympy for Gröbner computations). But the problem I keep finding is that it seems that div() only checks the leading term for division because
q, r = div(x**2 + y, 2*x)
gives r=y, while
q, r = div(x**2 + y, 2*y)
gives r=x**2+y.
I want to do things like Ideal Membership, hence finding the remainder of some polynomial f on division by G={g_1,...,g_s}, where by above I now cannot rely upon div().
Whilst working with Sage I don't get this problem (using (x^2+y)%y gives x^2), but I'm more familier with Python and prefer to do this via Python.
Can someone please tell me if I'm doing something wrong? Or does someone know a better function to use for remainders?
I found a good alternative: reduced(x**2+y, [2*y]) gives the desired ([1/2], x^2).
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.
For E=0.46732451 and t=1.07589765 I am trying to solve for the upper limit of the integral t= \int_{0}^{z} 1/sqrt(2*(0.46732451-z**2)), I plotted this function and it looks like this .
Around t=1 it kind of asymptotes.
I have the following code
import numpy as np
from scipy import integrate
from scipy.optimize import fsolve
def fg(z_up,t,E):
def h(z,E):
return 1/(np.sqrt(2*(E-z**2)))
b, err = integrate.quad(h, 0, z_up,args=(E))
return b-t
x0 = 0.1
print fsolve(fg, x0, args=(1.07589765, 0.46732451))[0]
But this code just outputs the guess value, no matter what I put, so I am guessing it has something to do with the fact that the curve asymptotes there. I should note that this code works for other values of t which are away from the asymptotic region.
Can anyone help me resolve this?
Thanks
EDIT After playing around for a while, I solved the problem, but it's kind of a patchwork, it only works for similar problems not in general (or does it?)
I made the following changes: the maximum value that z can attain is sqrt(0.46732451), so I set x0=0.5*np.sqrt(0.46732451) and set factor anywhere between 0.1 to 1, and out pops the correct answer. I don't have an explanation for this, perhaps someone who is an expert in this matter can help?
You should use bisect instead, as it handles nan without problems:
print bisect(fg, 0.4, 0.7, args=(1.07589765, 0.46732451))
Here 0.4 and 0.7 are taken as an example but you can generalize this for almost any diverging integral by using 0 and let's say 1e12 as the limits.
However, I'm not sure I understand what you really want to do... if you want to find the limit at which the integral diverges, cf. your
I am trying to solve for the upper limit of the integral
then it's simply for z_up -> \sqrt{E} \approx 0,683611374...
So to find the (approximate) numerical value of the integral you just have to decrease z_up from that value until quad stops giving a nan...