Converting symbolic matrix to numeric matrix with while loop

I am trying to convert several 1x11 symbolic matrix to a 1x11 numeric matrix.
WhenI run the code it gives the error:
Reference to non-existent field 'Ni'.
Error in Carbon_IB_HW (line 50)
solN1(1,i) = double(sol.Ni);
Does anyone know how I can define the elements in the matricies so I don't get this error? Or if there is some other way I can set this up that will avoid this problem entirely? Thanks.
The relevent code is:
clc
clear all
%Defining know values
T = linspace(10^8,10^9,11);
l10T = log10(T);
a1 = [5.91e-15 4.42e-15 3.30e-15 2.46e-15 1.83e-15 1.36e-15 1.01e-15 7.53e-16 5.60e-16 4.17e-16 2.10e-16];
a2 = [4.22e-15 3.02e-15 2.16e-15 1.55e-15 1.12e-15 8.05e-16 5.83e-16 4.24e-16 3.10e-16 2.28e-16 1.69e-16];
a3 = [4.49e-15 3.27e-15 2.39e-15 1.74e-15 1.28e-15 9.36e-16 6.89e-16 5.09e-16 3.77e-16 2.81e-16 2.11e-16];
a4 = [3.14e-14 2.25e-14 1.61e-14 1.15e-14 8.17e-15 5.82e-15 4.15e-15 2.96e-15 2.11e-15 1.50e-15 1.07e-15];
a5 = [6.95e-14 4.98e-14 3.57e-14 2.56e-14 1.84e-14 1.32e-14 9.43e-15 6.76e-15 4.84e-15 3.48e-15 2.50e-15];
a6 = [1.33e-14 1.01e-14 7.56e-15 5.65e-15 4.21e-15 3.12e-15 2.30e-15 1.69e-15 1.24e-15 9.11e-16 6.63e-16];
k = 1.38e-23;
m = 1.9944235e-26;
V = sqrt((2*k.*(T.*11600))./(m));
I1 = 10.6; A11 = 1.829; A12 = -1.975; A13 = 1.149; A14 = -3.583; A15 = 2.451;
I2 = 24.4; A21 = 8.390e-1; A22 = -7.95e-1; A23 = 3.263; A24 = -5.382; A25 = 3.476;
I3 = 41.4; A31 = 4.009e-1; A32 = -3.518e-1; A33 = 2.375; A34 = -3.992; A35 = 2.794;
I4 = 64.5; A41 = 1.350; A42 = -8.748e-1; A43 = -1.444; A44 = 2.330; A45 = -2.730;
I4b = 285; A41b = -2.777; A42b = 5.376; A43b = -8.748; A44b = 1.766e1; A45b = -9.086;
I5 = 392; A51 = 9.205e-1; A52 = -6.297e-1; A53 = 1.316; A54 = -9.156e-2; A55 = 0;
I6 = 490; A61 = 2.489e-1; A62 = 1.847e-1; A63 = 4.475e-2; A64 = -9.432e-2; A65 = 5.122e-1;
E = 6.242e18.*(.5*m.*V.^2);
%Solving for collisional cross section
Si1 = (1e-13./(I1.*E)).*((A11.*log(E./I1))+((A12.*(1-(I1./E).^1))+(A13.*(1-(I1./E).^2))+(A14.*(1-(I1./E).^3))+(A15.*(1-(I1./E).^4))));
Si2 = (1e-13./(I2.*E)).*((A21.*log(E./I2))+((A22.*(1-(I2./E).^1))+(A23.*(1-(I2./E).^2))+(A24.*(1-(I2./E).^3))+(A25.*(1-(I2./E).^4))));
Si3 = (1e-13./(I3.*E)).*((A31.*log(E./I3))+((A32.*(1-(I3./E).^1))+(A33.*(1-(I3./E).^2))+(A34.*(1-(I3./E).^3))+(A35.*(1-(I3./E).^4))));
Si4 = (1e-13./(I4.*E)).*((A41.*log(E./I4))+((A42.*(1-(I4./E).^1))+(A43.*(1-(I4./E).^2))+(A44.*(1-(I4./E).^3))+(A45.*(1-(I4./E).^4))));
Si4b = (1e-13./(I4b.*E)).*((A41b.*log(E./I4))+((A42b.*(1-(I4b./E).^1))+(A43b.*(1-(I4b./E).^2))+(A44b.*(1-(I4b./E).^3))+(A45b.*(1-(I4b./E).^4))));
Si5 = (1e-13./(I5.*E)).*((A51.*log(E./I5))+((A52.*(1-(I5./E).^1))+(A53.*(1-(I5./E).^2))+(A25.*(1-(I5./E).^3))+(A55.*(1-(I5./E).^4))));
Si6 = (1e-13./(I6.*E)).*((A61.*log(E./I6))+((A62.*(1-(I6./E).^1))+(A63.*(1-(I6./E).^2))+(A64.*(1-(I6./E).^3))+(A65.*(1-(I6./E).^4))));
S1 = Si1.*V;
S2 = Si2.*V;
S3 = Si3.*V;
S4 = (Si4.*V)+(Si4b.*V);
S5 = Si5.*V;
S6 = Si6.*V;
%Solving system of equations to find populations at level N'x'
n = 11;
N1 = sym('N1',[1 n]);
N2 = sym('N2',[1 n]);
N3 = sym('N3',[1 n]);
N4 = sym('N4',[1 n]);
N5 = sym('N5',[1 n]);
N6 = sym('N6',[1 n]);
eqns = [N1 == N2.*(a2./S1), N2 == N3.*(a3./S2), N3 == N4.*(a4./S3),...
N4 == N5.*(a5./S4), N5 == N6.*(a6./S5), 1 == N1+N2+N3+N4+N5+N6];
sol = solve(eqns,[N1 N2 N3 N4 N5 N6]);
solN1 = zeros(1,n);
solN2 = zeros(1,n);
solN3 = zeros(1,n);
solN4 = zeros(1,n);
solN5 = zeros(1,n);
solN6 = zeros(1,n);
i=1;
while i<=11
solN1(1,i) = double(sol.N1i);
solN2(1,i) = double(sol.N2i);
solN3(1,i) = double(sol.N3i);
solN4(1,i) = double(sol.N4i);
solN5(1,i) = double(sol.N5i);
solN6(1,i) = double(sol.N6i);
i=i+1;
end

 採用された回答

Torsten
Torsten 2022 年 3 月 1 日
編集済み: Torsten 2022 年 3 月 2 日

0 投票

First solve the equations for all variables involved being scalars:
syms N1 N2 N3 N4 N5 N6 a2sym a3sym a4sym a5sym a6sym S1sym S2sym S3sym S4sym S5sym
eqns = [N1 == N2*(a2sym/S1sym), N2 == N3*(a3sym/S2sym), N3 == N4*(a4sym/S3sym),...
N4 == N5*(a5sym/S4sym), N5 == N6*(a6sym/S5sym), 1 == N1+N2+N3+N4+N5+N6];
sol = solve(eqns,[N1 N2 N3 N4 N5 N6]);
Then use "subs" to insert the values from the arrays for a2,a3,a4,a5,a6,...
N1num = double(subs(sol.N1,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
N2num = double(subs(sol.N2,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
N3num = double(subs(sol.N3,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
N4num = double(subs(sol.N4,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
N5num = double(subs(sol.N5,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
N6num = double(subs(sol.N6,{a2sym,a3sym,a4sym,a5sym,a6sym,S1sym,S2sym,S3sym,S4sym,S5sym},{a2,a3,a4,a5,a6,...
S1,S2,S3,S4,S5}));
But why do you set up your problem with symbolic variables ? You have a simple linear system in N1,...,N6 that can be solved by numerical backslash much more efficient than with symbolic calculus.

1 件のコメント

Hugh Currah
Hugh Currah 2022 年 3 月 2 日
Thanks, this worked. I am not sure why I used symbolic variables, it is just what I am used to in Matlab.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by