Saving data calculated for each temperature with varying time interval

3 views (last 30 days)
I want to save my data for each temperature where equation is summation of n term and need to solve it by varying time.
I have written this but it is not working for temp variation to store data
Z=zeros(0,1500);
tot= zeros(0,4);
for kk=300:50:450 % temperature variation
K1=12e-10*exp(-6e4/(4*kk)); % temperature dependent term
for ii=1:1500 % time variation
sum=0;
for jj=1:1000; %n= summation term
T1=(1/jj^2*exp(-K*jj^2*ii*3.2^2));
sum=sum+T1;
end
sumtot=1-4/11*sum;
Z(ii) =sumtot;
end
end
I need output saved as array for each temperature it should store time vs summation term (Z)

Accepted Answer

Edoardo Mattia Piccio
Edoardo Mattia Piccio on 25 May 2023
Hi Sudhir, let's analyze your code:
  • zeros (and other similar functions) wants the dimensions of the array or matrix. Please, check the documentation about it
  • then, when you calculate T1 I think you use K1 instead of K
  • sum is a built-in function witch calculate the sum of the elements of array or matrix. In general, never use as a variable name a function
With this changes, the code runs but it doesn't calculate right values. Maybe formulas contain errors, please let me know after check them.
tempVariation= 300:50:450; numbOfTemp= length(tempVariation); % temperature variation
Z=zeros(1,1500); % not zeros(0,1500)
tot= zeros(numbOfTemp,1500);
for kk= 1:numbOfTemp
K1=12e-10*exp(-6e4/(4*kk)); % temperature dependent term
for ii=1:1500 % time variation
S=0;
for jj=1:1000 % n= summation term
T1=(1/jj^2*exp(-K1*jj^2*ii*3.2^2));
S=S+T1;
end
sumtot=1-4/11*S;
Z(ii) =sumtot;
end
tot(kk,:)= Z; % store results in a matrix
end
tot
tot = 4×1500
0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022
Moreover, instead of for loops, you can try to use array operations. Here there is an example for your code:
tempVariation= 300:50:450; numbOfTemp= length(tempVariation); % temperature variation
Z= zeros(1,1500); nTerm= 1:1000;
tot= zeros(numbOfTemp,1500);
for kk= 1:numbOfTemp
K1=12e-10*exp(-6e4/(4*tempVariation(kk))); % temperature dependent term
for ii= 1:1500
Z(ii)=sum((1./nTerm.^2).*exp(-K1.*nTerm.^2.*ii.*3.2^2));
end
tot(kk,:)= 1-4*Z/11;
end
tot
tot = 4×1500
0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022 0.4022
  2 Comments
Sudhir Rai
Sudhir Rai on 25 May 2023
Thank you very much. This worked perfectly.
for calculation of K1 in for loop need to use tempVariation(kk) instead of kk then it worked.
There was error in formula of K1, it was too small which I have corrected and it is working fine.
Thanks again for the help !
Sudhir Rai
Sudhir Rai on 25 May 2023
code with array operations is also working with corrected formula of K1.
Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!