Vectorize output of for loop, but using specific vector values

조회 수: 8 (최근 30일)
Seth Antozzi
Seth Antozzi 2023년 2월 28일
답변: Voss 2023년 2월 28일
I am sorry if this is already posted somewhere, but I can't find an example that uses a vector with specific values, not say, [0,1,2,3].
I need to use the following code:
k=1.38E-23;
E0= 2.72361635536024e-21
M=3;
myVector = zeros(M+1,1);
for T = [100,300,1000,3000]
ERatio0=exp(-E0/(k*T));
myVector(T) = ERatio0;
end
to give me a vector 4 values long, with the resulting ERatio0 values for the corresponding T values, should be only 4 of them. Matlab is calculating all 3000 values right now as if I wanted all the integer values. Probably simple, I'm just missing it somehow.
If someone really wants to be nice, can they explain how I could do this for multiple Eratio's, each using a different E0, E1, so on:
T=1000; %Kelvins
k=1.38E-23;
h=6.626e-34;
h_bar=6.626e-34/(2*pi);
c=2.998*10^8;
ccm=2.998*10^10; %speed of light in cm
w=1730 ; %in cm
xe=.00815; %in cm
%% a
E0=(h_bar*ccm*w*((0+.5)-xe*(0+.5)^2)); % Gives answer in J
E1=(h_bar*ccm*w*((1+.5)-xe*(1+.5)^2));
E2=(h_bar*ccm*w*((2+.5)-xe*(2+.5)^2));
E3=(h_bar*ccm*w*((3+.5)-xe*(3+.5)^2));
E4=(h_bar*ccm*w*((4+.5)-xe*(4+.5)^2));
E0_w=(h_bar*ccm*w*((0+.5)-xe*(0+.5)^2))/1.986e-23 % Gives answer in wavenumber (cm^-1)
E1_w=(h_bar*ccm*w*((1+.5)-xe*(1+.5)^2))/1.986e-23
E2_w=(h_bar*ccm*w*((2+.5)-xe*(2+.5)^2))/1.986e-23
E3_w=(h_bar*ccm*w*((3+.5)-xe*(3+.5)^2))/1.986e-23
E4_w=(h_bar*ccm*w*((4+.5)-xe*(4+.5)^2))/1.986e-23
ERatio0=(exp(-E0/(k*T)));
ERatio1=(exp(-E1/(k*T)));
ERatio2=(exp(-E2/(k*T)));
ERatio3=(exp(-E3/(k*T)));
ERatio4=(exp(-E4/(k*T)));
sumEratio=ERatio0+ERatio1+ERatio2+ERatio3+ERatio4;
nRatio0=ERatio0/sumEratio
nRatio1=ERatio1/sumEratio
nRatio2=ERatio2/sumEratio
nRatio3=ERatio3/sumEratio
nRatio4=ERatio4/sumEratio
nRatio0+nRatio1+nRatio2+nRatio3+nRatio4% summed, equals 1 as it should
p1=plot([0,1,2,3,4],[nRatio0,nRatio1,nRatio2,nRatio3,nRatio4]) %T=1000K
%%
%T=100K
T100=100;
ERatio0=(exp(-E0/(k*T100)));
ERatio1=(exp(-E1/(k*T100)));
ERatio2=(exp(-E2/(k*T100)));
ERatio3=(exp(-E3/(k*T100)));
ERatio4=(exp(-E4/(k*T100)));
sumEratio=ERatio0+ERatio1+ERatio2+ERatio3+ERatio4;
nRatio0=ERatio0/sumEratio
nRatio1=ERatio1/sumEratio
nRatio2=ERatio2/sumEratio
nRatio3=ERatio3/sumEratio
nRatio4=ERatio4/sumEratio
nRatio0+nRatio1+nRatio2+nRatio3+nRatio4% summed, equals 1 as it should
Again, I am missing some easy stuff here, but I bet this is simple for some of you.
Eventually I want to be able to make a plot of Eratios vs Temperature. There are 4 temperature values, and 5 Eratios, as you can see. Maybe output into vectors is the way?
Thanks.

채택된 답변

Voss
Voss 2023년 2월 28일
k=1.38E-23;
E0= 2.72361635536024e-21
M=3;
myVector = zeros(M+1,1);
T = [100,300,1000,3000];
for ii = 1:numel(T)
ERatio0=exp(-E0/(k*T(ii)));
myVector(ii) = ERatio0;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by