MATLAB: Solving a symbolic equation in a for loop and store them

for loopsymbolic equation

I have been trying to solve a symbolic equation in a for loop and then store the data in matrix form. I've attached my code and error txt below;
c = 2.99792458e+08;
mu_0=4*pi*1e-7;
eps_0 = 8.8541878176e-12;
n_1 = 1.5;
% n_2 = 1.5;
% n_3 = 1.5;
thetad=30;
phid = 0;
nu_bar_0_cm=1000;
% nu_bar_0_cm = 1000;
nu_bar_0 = nu_bar_0_cm * 1e2;
lambda_0 = 1/nu_bar_0;
%%%%%%% dielectric tensor epsilon using eq(1)%%%%%%%%%%%%
% eps_2 = ((n_2)^2)*eps_0*eye(3);
% eps_3 = ((n_3)^2)*eps_0*eye(3);
nu = c/lambda_0;
lambda_1 = lambda_0/n_1;
Al = 2*pi*sind(thetad)*cosd(phid)/lambda_1;%alpha
Bt = 2*pi* sind(thetad) * sind(phid)/lambda_1; % beta
w=2*pi*nu;
eps_1 = (n_1)^2*eps_0*eye(3);
e_delta=1:20;
eps_33=eps_1(3,3);
len_Edelta=length(e_delta);
gamma = sym('gamma');
x11 = (w^2)*mu_0*eps_1(1,1)-(Bt^2)-(gamma^2);
x12 = (w^2)*mu_0*eps_1(1,2)+Al*Bt;
x13 = (w^2)*mu_0*eps_1(1,3)+Al*gamma;
x21 = (w^2)*mu_0*eps_1(2,1)+Al*Bt;
x22 = (w^2)*mu_0*eps_1(2,2)-(Al^2)-(gamma^2);
x23 = (w^2)*mu_0*eps_1(2,3)+Bt*gamma;
x31 = (w^2)*mu_0*eps_1(3,1)+Al*gamma;
x32 = (w^2)*mu_0*eps_1(3,2)+Bt*gamma;
% epsilon1=zeros(3,60);
% lendel=3*len_Edelta;
g1_store=zeros(4,len_Edelta);
p_store = zeroes(3,len_Edelta);
for ii=1:len_Edelta
new_eps33 = eps_33+eps_0*10^(-e_delta(ii));
eps_1(3,3) = new_eps33;
x33= w^2*mu_0*eps_1(3,3)-Bt^2-Al^2;
M = [x11 x12 x13; x21 x22 x23; x31 x32 x33];
detM = det(M(:,:));
DetM(ii)=detM;
% eqn = DetM(ii)==0;
gg= solve(DetM(ii),gamma);
gamma=gg;
g1(:,:) = double(gamma);
g1_store(:,ii) = g1(:);
p_x = (w.^2.*mu_0.*(eps_1(2,2))-Al.^2-(g1(1,1)).^2).*(w.^2.*mu_0.*eps_1(3,3)-Al.^2-Bt.^2)-(w.^2.*mu_0.*(eps_1(2,3))+Bt.*g1(1,1)).^2 ;
p_y = (w.^2.*mu_0.*eps_1(2,3)+Bt.*g1(1,1)).*(w.^2.*mu_0.*eps_1(3,1)+Al.*g1(1,1))-(w.^2.*mu_0.*eps_1(1,2)+Al.*Bt).*(w.^2.*mu_0.*(eps_1(3,3)+eps_0*1e-1)-Al.^2-Bt.^2);
p_z = (w.^2.*mu_0.*eps_1(1,2)+Al.*Bt).*(w.^2.*mu_0.*eps_1(2,3)+Bt.*g1(1,1))-(w.^2.*mu_0.*eps_1(1,3)+Al.*g1(1,1)).*(w.^2.*mu_0.*eps_1(2,2)-Al.^2-(g1(1,1)).^2);
p_store(:,ii) = [p_x; p_y; p_z];
end
and the error,
>> delta
Error using sym.getEqnsVars>checkVariables (line 87)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 54)
checkVariables(vars);
Error in solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in delta (line 62)
gg= solve(DetM(ii),gamma);
I would really appreciate any help. have been working on this for days now. Thanks in advance.

Best Answer

gamma = sym('gamma');
So you initialize gamma as a symbolic scalar variable.
for ii=1:len_Edelta
So you enter into a loop
gg= solve(DetM(ii),gamma);
First time through the loop, gamma is the initial symbolic scalar variable and the solve() is able to proceed
gamma=gg;
You just overwrote gamma with the expression that resulted from the solve() operation. The result is unlikely to happen to be a symbolic variable or vector of symbolic variables: it is more likely to be a symbolic expression
g1(:,:) = double(gamma);
If it were a symbolic variable or vector of symbolic variables, then double() if it would produce an error, so we can deduce that it is a symbolic expression that represents one or more numeric values.
end
So we are at the bottom of the loop. At this point, gamma is a symbolic expression representing a numeric value.
Now execute the second iteration of the for loop:
gg= solve(DetM(ii),gamma);
gamma is a symbolic expression representing one or more numeric values, not a symbolic variable. solve() throws a fit.