# matlabFunction Equivalent?

R

#### rpi.baldum

Hey all,

I'm new at Python, so if you see any mistakes feel free to let me know.

I'm trying to take a symbolic expression and turn it into a variable equation or function. I think that just an expression of variables would be preferable.

I have a range equation which I form using symbols and then take various derivatives of it. I then want to have these derivatives on hand to use for various functions, but short of using sub every time or just copy pasting from the console output (I don't want to do that), I can't find an efficient way to do this. Matlab had matlabFunction which was really handy, but I don't think Python has an equivalent.

########
import numpy as np
import scipy as sp
import sympy as sy
import math as ma

x, y, z, x_s, y_s, z_s, theta, theta_dot, x_dot, y_dot, z_dot = sy.symbols('x y z x_s y_s z_s theta theta_dot x_dot y_dot z_dot')

rho = (x**2 + y**2 + z**2 + x_s**2 + y_s**2 + z_s**2 - 2*(x*x_s + y*y_s)*sy.cos(theta) + 2*(x*y_s - y*x_s)*sy.sin(theta) - 2*z*z_s)**(0.5)

rho_dot = (x*x_dot + y*y_dot + z*z_dot - (x_dot*x_s + y_dot*y_s)*sy.cos(theta) + theta_dot*(x*x_s + y*y_s)*sy.sin(theta) + (x_dot*y_s - y_dot*x_s)*sy.sin(theta) + theta_dot*(x*y_s - y*x_s)*sy.cos(theta) - z_dot*z_s)/rho

drho_dx = sy.diff(rho, x)

drho_dy = sy.diff(rho, y)

drho_dz = sy.diff(rho, z)

#I then want drho_dx, etc to be variable expressions with x, y, z, etc as variables instead of symbols or numbers. I could do:

x, y, z = 1200, 1300, 1400 #m

drho_dx = subs([x, x], [y, y], [z, z])

#but this seems inefficient to do multiple times. Thoughts?

M

#### Mark Lawrence

Hey all,

I'm new at Python, so if you see any mistakes feel free to let me know.

I'm trying to take a symbolic expression and turn it into a variable equation or function. I think that just an expression of variables would be preferable.

I have a range equation which I form using symbols and then take various derivatives of it. I then want to have these derivatives on hand to use for various functions, but short of using sub every time or just copy pasting from the console output (I don't want to do that), I can't find an efficient way to do this. Matlab had matlabFunction which was really handy, but I don't think Python has an equivalent.

########
import numpy as np
import scipy as sp
import sympy as sy
import math as ma

x, y, z, x_s, y_s, z_s, theta, theta_dot, x_dot, y_dot, z_dot = sy.symbols('x y z x_s y_s z_s theta theta_dot x_dot y_dot z_dot')

rho = (x**2 + y**2 + z**2 + x_s**2 + y_s**2 + z_s**2 - 2*(x*x_s + y*y_s)*sy.cos(theta) + 2*(x*y_s - y*x_s)*sy.sin(theta) - 2*z*z_s)**(0.5)

rho_dot = (x*x_dot + y*y_dot + z*z_dot - (x_dot*x_s + y_dot*y_s)*sy.cos(theta) + theta_dot*(x*x_s + y*y_s)*sy.sin(theta) + (x_dot*y_s - y_dot*x_s)*sy.sin(theta) + theta_dot*(x*y_s - y*x_s)*sy.cos(theta) - z_dot*z_s)/rho

drho_dx = sy.diff(rho, x)

drho_dy = sy.diff(rho, y)

drho_dz = sy.diff(rho, z)

#I then want drho_dx, etc to be variable expressions with x, y, z, etc as variables instead of symbols or numbers. I could do:

x, y, z = 1200, 1300, 1400 #m

drho_dx = subs([x, x], [y, y], [z, z])

#but this seems inefficient to do multiple times. Thoughts?

There are references to MatlabFunction in code here
https://github.com/scipy/scipy/tree/master/scipy/io/matlab but I haven't
the faintest idea as to whether or not it does what you want, sorry J

#### Johannes Schneider

Hey all,

I'm new at Python, so if you see any mistakes feel free to let me know.

I'm trying to take a symbolic expression and turn it into a variable equation or function. I think that just an expression of variables would be preferable.

I have a range equation which I form using symbols and then take various derivatives of it. I then want to have these derivatives on hand to use for various functions, but short of using sub every time or just copy pasting from the console output (I don't want to do that), I can't find an efficient way to do this. Matlab had matlabFunction which was really handy, but I don't think Python has an equivalent.

########
import numpy as np
import scipy as sp
import sympy as sy
import math as ma

x, y, z, x_s, y_s, z_s, theta, theta_dot, x_dot, y_dot, z_dot = sy.symbols('x y z x_s y_s z_s theta theta_dot x_dot y_dot z_dot')

rho = (x**2 + y**2 + z**2 + x_s**2 + y_s**2 + z_s**2 - 2*(x*x_s + y*y_s)*sy.cos(theta) + 2*(x*y_s - y*x_s)*sy.sin(theta) - 2*z*z_s)**(0.5)

rho_dot = (x*x_dot + y*y_dot + z*z_dot - (x_dot*x_s + y_dot*y_s)*sy.cos(theta) + theta_dot*(x*x_s + y*y_s)*sy.sin(theta) + (x_dot*y_s - y_dot*x_s)*sy.sin(theta) + theta_dot*(x*y_s - y*x_s)*sy.cos(theta) - z_dot*z_s)/rho

drho_dx = sy.diff(rho, x)

drho_dy = sy.diff(rho, y)

drho_dz = sy.diff(rho, z)

#I then want drho_dx, etc to be variable expressions with x, y, z, etc as variables instead of symbols or numbers. I could do:

x, y, z = 1200, 1300, 1400 #m

drho_dx = subs([x, x], [y, y], [z, z])

#but this seems inefficient to do multiple times. Thoughts?

If you don not mind installing other programs, maybe you can have a
look at sage (www.sagemath.org). That's a ComputerAlgebraSystem using
python as its base and supporting most (all?) of the python syntax and
moduls

bg,
Johannes

--
Johannes Schneider
Webentwicklung
Tel.: +49.228.42150.xxx

Galileo Press GmbH
Rheinwerkallee 4 - 53227 Bonn - Germany
Tel.: +49.228.42.150.0 (Zentrale) .77 (Fax)
http://www.galileo-press.de/

Geschäftsführer: Tomas Wehren, Ralf Kaulisch, Rainer Kaltenecker
HRB 8363 Amtsgericht Bonn