- 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
Saving data calculated for each temperature with varying time interval
    6 views (last 30 days)
  
       Show older comments
    
    Sudhir Rai
      
 on 25 May 2023
  
    
    
    
    
    Commented: Sudhir Rai
      
 on 25 May 2023
            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)
0 Comments
Accepted Answer
  Edoardo Mattia Piccio
      
 on 25 May 2023
        Hi Sudhir, let's analyze your code:
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
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
More Answers (0)
See Also
Categories
				Find more on Matrix Indexing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
