How to convert symbolic expression to numeric?

3 次查看(过去 30 天)
I am creating a distance function that I will need for convex optimization later. This distance function requires integrating a function of x, x', z, and z' so I am initially dealing with symbolic expressions. The code below produces the desired integrand as its ouput. However, I cannot seem to convert this 1x1 sym output into anything that I use numerically. double, matlabFunction, and sym2poly all just give errors. As a workaround, I've just manually transcriped the resulting formula but that is obviously not an ideal solution. What am I doing wrong here?
syms x y z k_1 k_2 k_3 k_4 k_5 k_6 k_7 k_8 y_0
x = k_1*(y^3) + k_2*(y^2) + k_3*y+k_4;
xprime = 3*k_1*(y^2) + 2*k_2*y + k_3;
% construction of approximate trajectory for z
z = k_5*(x^3) + k_6*(x^2) + k_7*x+k_8;
zprime = 3*k_5*(x^2) + 2*k_6*x + k_7;
integrand = sqrt(1+(zprime*xprime)^2+(xprime)^2);
% performance parameters
Isp = 300; % s
S = 12.56; % m^2
P_min = 540; % kN
P_max = 800; % kN
alpha_min = -10; % degrees
alpha_max = 10; % degrees
beta_min = -10; % degrees
beta_max = 10; % degrees
T_c = 0.7; % s
delta_T = 3; % s
maxdelta_P = 80; % kN
N = 200;
kappa = 0.5;
deltaT_L = 1.5; % s
V_0 = 335; % m/s
theta_0 = -65; % degrees
phi_0 = -6; % degrees
x_0 = 2500; % m
y_0 = 3698; % m
z_0 = 2500; % m
m_0 = 31200; % kg
% target terminal states
V_f = 0; % m/s
theta_f = -90; % degrees
phi_f = 0; % degrees
x_f = 3232; % m
y_f = 0; % m
z_f = 2646; % m
Ax = [(y_0)^3 (y_0)^2 y_0 1;...
(y_f)^3 (y_f)^2 y_f 1;...
3*(y_0)^2 2*(y_0) 1 0;...
3*(y_f)^2 2*(y_f) 1 0];
bx = [x_0; x_f; cos(phi_0)/tan(theta_0); cos(phi_f)/tan(theta_f)];
kx = Ax\bx;
k_1 = kx(1); k_2 = kx(2); k_3 = kx(3); k_4 = kx(4);
% construction of approximate trajectory for z
Az = [(x_0)^3 (x_0)^2 y_0 1;...
(x_f)^3 (x_f)^2 x_f 1;...
3*(x_0)^2 2*x_0 1 0;...
3*(x_f)^2 2*x_f 1 0];
bz = [z_0; z_f; -tan(phi_0); -tan(phi_f)];
kz = Az\bz;
k_5 = kz(1); k_6 = kz(2); k_7 = kz(3); k_8 = kz(4);
integrand_num = matlabFunction(subs(simplify(integrand)))
integrand_num = function_handle with value:
@(y)sqrt((y.*(-1.216549561249475e-3)+y.^2.*3.400868872575844e-7+5.012027833801532e-1).^2+(y.*(-1.216549561249475e-3)+y.^2.*3.400868872575844e-7+5.012027833801532e-1).^2.*(y.*1.800900185003124e-4-y.^2.*2.185626659078132e-7+y.^3.*4.073285028791622e-11-(y.*5.012027833801532e-1-y.^2.*6.082747806247377e-4+y.^3.*1.133622957525281e-7+3.232e+3).^2.*1.320420657586983e-7+1.379288179511788).^2+1.0)
%simplify(subs(integrand))

回答(1 个)

Torsten
Torsten 2024-11-26
编辑:Torsten 2024-11-27
integrand_num = matlabFunction(subs(simplify(integrand)))
gives you a function handle with y as input argument (see above).
And this doesn't work in your MATLAB release ? Or do you use Octave ?

类别

Help CenterFile Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by