Converting a symbolic matrix to numeric

22 次查看(过去 30 天)
Carl
Carl 2014-1-12
评论: Carl 2014-1-12
Hi,
I am trying to convert a symbolic matrix to a numeric array, but am having no luck. Please refer to the code below.
Z = sym(zeros(200,1));
for i = 1:200
syms omega_n;
lambda = i*1e-7;
k_zeta = 2*pi/lambda;
kappa_n = n*pi/W;
k_n = sqrt(((kappa_n)^2) + ((k_zeta)^2))
Y(i,1) = k_n;
P_nn = (k_zeta*W)^2/(i*pi)^2;
yellow = omega_M*P_nn*(1-P_nn)*((sin(phi))^2);
green = 1 - (P_nn*(1+((cos(phi))^2)));
blue = omega_H + J*omega_M*((k_n)^2);
F_n = P_nn + green*((sin(theta))^2) + yellow/blue;
A = (sqrt(blue*(blue + (omega_M*F_n))))/(2*pi) - omega_n;
R = feval(symengine,'numeric::fsolve', A, 'omega_n=1e8')
Z(i) = vpa(omega_n);
%printf('For k = %d, freq = %d\n', k_zeta,A);
end
Y = int8(Y);
class(Z)
Z = vpa(Z,100);
class(Z)
%Z = vpa(Z);
scatter(Y,Z)
All of the variables, except for omega_n, are of type double.
When I attempt to use the 'double' function on object Z ie double(Z), I receive an error message
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead.
Can someone point out where I am going wrong? This is the first time I am trying to use the symbolic toolbox, so apologies if it is something obvious! Note I am using Matlab R2011b.
Thanks!
Carl
  2 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2014-1-12
You can make your question clear, by just posting the matrix you want to convert
Carl
Carl 2014-1-12
Sorry it wasn't very clear.
Here is the code snippet:
syms omega_n;
Z = sym(zeros(200,1));
% A section about defining equation A, which is a mixture of doubles and the symbolic value omega_n, and then looping over 'i', which is part of one of the input parameters for the equation.
Z(i) = feval(symengine,'numeric::fsolve', A, 'omega_n=1e8')
I would like to write matrix Z as an array of numeric elements, so that I can write it to a file, plot it etc. Simply writing 'double(Z)' does not do the conversion.
Hope this makes the question more clear.

请先登录,再进行评论。

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-1-12
编辑:Azzi Abdelmalek 2014-1-12
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2014-1-12
If you post an example we can test, it will be useful
Carl
Carl 2014-1-12
Here is the script I am working with - most of it's length comes from setting parameters etc (I'm modelling a physical system), so please don't be put off! Thank you Azzi!
pi = 4*atan(1.0);
mu0 = 4*pi*1e-7; % Units of T.m/A
gamma = 1.7609e11; % Units of rad/(T.s)
B = 500; % External field, in units of Oe
M_sat = 1.005; % Magnetization of saturation, in T
J = (5.03e-9)^2; % Exchange stiffness, in m^2 (square of l_ex)
M_0 = M_sat*795774.72; % Converting T to A/m.
H_0 = B*79.577472; % Converting Oe to A/m.
omega_M = gamma*M_0*mu0; % Units of rad/s
omega_H = gamma*H_0*mu0; % Units of rad/s
% Now define the waveguide geometry.
W = 5e-6;
L = 100e-9;
theta = 0*pi/180; % We're only considering in-plane modes here
% We'll start off finding the dispersion relation for the BVMSW.
% Refer to [1] for reference.
n = 1; % Mode number
phi = 0*pi/180; % Orientation of external field, in rad
% The 'meat' of this script: the function used here.
Z = sym(zeros(200,1));
for i = 1:200
syms omega_n;
lambda = i*1e-7;
k_zeta = 2*pi/lambda;
kappa_n = n*pi/W;
k_n = sqrt(((kappa_n)^2) + ((k_zeta)^2))
Y(i,1)= k_n;
P_nn = (k_zeta*W)^2/(i*pi)^2;
yellow = omega_M*P_nn*(1-P_nn)*((sin(phi))^2);
green = 1 - (P_nn*(1+((cos(phi))^2)));
blue = omega_H + J*omega_M*((k_n)^2);
F_n = P_nn + green*((sin(theta))^2) + yellow/blue;
A = (sqrt(blue*(blue + (omega_M*F_n))))/(2*pi) - omega_n;
R = feval(symengine,'numeric::fsolve', A, 'omega_n=1e8')
Z(i) = subs(R);
%printf('For k = %d, freq = %d\n', k_zeta,A);
end
class(Z)
Z = double(Z,100);
class(Z)
%Z = vpa(Z);
scatter(Y,Z)

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by