How to sum of loop elements?

3 views (last 30 days)
Egor Popov
Egor Popov on 21 May 2022
Answered: Mathieu NOE on 23 May 2022
I have a code in which, in addition to displaying graphs, you need to get the sum of the elements and display them on a common graph. How to correctly sum ALL elements of the cycle (loop) among themselves and then display the graph? (GA, GH, GUV, GGAS, GGRAINS)? ty
clear all
n=1;
z=1;
G=1.4;
Tgr = 15;
N_t = 100000;
T_ini = 10;
T_fin = 1e4;
dT=(1e4-1e1)/N_t;
ne=1e-4;
Rdiss = 5.6e-11;
nh2 = 2.6e-9;
k4 = 6.4e-10;
for k=1:N_t
T(k) = T_ini + dT*(k-1);
k60 = 3.0d-18*T(k)^0.5/(1+4.0d-2*(T(k)+Tgr)^0.5+2d-3*T(k)+8d-6*T(k)^2)/(1+1.0d4*exp(-600/Tgr));
ncrh = 10.^(3-0.416*log10(T(k)/10000)-0.327*(log10(T(k)/10000)^2));
ncrh2 = 10.^(4.845 - 1.3*log10(T(k)/10000)+1.62*(log10(T(k)/10000))^2);
nharmonic = 1.0/(10.0/ncrh + 1/ncrh2);
k2 = 4d-9/T(k)^0.17;
GGAS(k)= (2.93e-12*k2*nh2 + 5.65e-12*k4*nh2)*nh2*nharmonic;
GGRAINS(k) = 7.16d-12*k60*nh2*nharmonic;
fi=G*T(k)^0.5/(0.5*ne);
e=(4.9e-2*(1+4e-3*fi)^-1)+(3.7e-2*((T(k)/10000)^0.7)*(1+2e-4*fi)^-1);
GA(k)=1.3e-24*e*1.4*1*1;
GH(k) = 6.4e-13*Rdiss*nh2;
GUV(k) = 2.7e-11*Rdiss*nh2;
end
hold on
%figure(1001)
loglog (T,GA)
loglog (T,GH, '-.')
loglog (T,GUV, '--')
loglog (T,GGAS, ':.')
loglog(T,GGRAINS)
set(gca,'yscale','log')
set(gca,'xscale','log')
legend ('GA','GH','GUV','GGAS','GGRAINS')

Answers (1)

Mathieu NOE
Mathieu NOE on 23 May 2022
hello
I opted for a bar graph of the summed data
I changed the Y scale to log otherwise you could not see the tiny values aside from the large ones
code updated :
clear all
n=1;
z=1;
G=1.4;
Tgr = 15;
N_t = 100000;
T_ini = 10;
T_fin = 1e4;
dT=(1e4-1e1)/N_t;
ne=1e-4;
Rdiss = 5.6e-11;
nh2 = 2.6e-9;
k4 = 6.4e-10;
for k=1:N_t
T(k) = T_ini + dT*(k-1);
k60 = 3.0d-18*T(k)^0.5/(1+4.0d-2*(T(k)+Tgr)^0.5+2d-3*T(k)+8d-6*T(k)^2)/(1+1.0d4*exp(-600/Tgr));
ncrh = 10.^(3-0.416*log10(T(k)/10000)-0.327*(log10(T(k)/10000)^2));
ncrh2 = 10.^(4.845 - 1.3*log10(T(k)/10000)+1.62*(log10(T(k)/10000))^2);
nharmonic = 1.0/(10.0/ncrh + 1/ncrh2);
k2 = 4d-9/T(k)^0.17;
GGAS(k)= (2.93e-12*k2*nh2 + 5.65e-12*k4*nh2)*nh2*nharmonic;
GGRAINS(k) = 7.16d-12*k60*nh2*nharmonic;
fi=G*T(k)^0.5/(0.5*ne);
e=(4.9e-2*(1+4e-3*fi)^-1)+(3.7e-2*((T(k)/10000)^0.7)*(1+2e-4*fi)^-1);
GA(k)=1.3e-24*e*1.4*1*1;
GH(k) = 6.4e-13*Rdiss*nh2;
GUV(k) = 2.7e-11*Rdiss*nh2;
end
hold on
%figure(1001)
loglog (T,GA)
loglog (T,GH, '-.')
loglog (T,GUV, '--')
loglog (T,GGAS, ':.')
loglog(T,GGRAINS)
set(gca,'yscale','log')
set(gca,'xscale','log')
legend ('GA','GH','GUV','GGAS','GGRAINS')
%sum of the elements 'GA','GH','GUV','GGAS','GGRAINS' and display on a common graph
GA_sum = sum(GA);
GH_sum = sum(GH);
GUV_sum = sum(GUV);
GGAS_sum = sum(GGAS);
GGRAINS_sum = sum(GGRAINS);
data = [GA_sum GH_sum GUV_sum GGAS_sum GGRAINS_sum];
figure,
b = bar(data,.75,'grouped');
b.Parent.YScale = 'log';
name = {'GA';'GH';'GUV';'GGAS';'GGAS'};
set(gca,'xticklabel',name)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!