Matlab How to plot values from a loop

  • Thread starter Thread starter jkthejetplane
  • Start date Start date
  • Tags Tags
    Loop Matlab Plot
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 3K views
jkthejetplane
Messages
29
Reaction score
4
Homework Statement
Ok so i am relatively new to matlab but my course had an emphasis much more on the applications of numerical methods rather than teaching coding (same with text). So most of my previous plotting was from modifying given programs to use a differnt math method of some sorts. Basically i am unsure how to make this T vs C plot correctly haha
In previous codes i have seen where they store the value during the loop (T and C in my case) for each iteration
Relevant Equations
No Equations needed
Matlab:
%Debeye theory Heat Capacity of Copper
%Garcia Problem 10.16
%Find molar heat capacity from 0-1083K (melting point of Cu)

k = 1.38064852e-23; %Boltzmann's constant
N = 6.02214e23; %Avagadro's number (atoms per mol)
T = 0:1083 ; %Initialize Temp at 0K-1083K
theta = 309; %Debeye temp of Cu (K)
coef = 9.*k.*N %Coef of constants
f = @(x) (x.^4).*(exp(x)./(exp(x) - 1)); %Function inside integralfor i = 1:length(T)   
    top = theta./T(i); %Top boundary of integral
    Int = integral(f,0,top);
    C = 9.*k.*N.*((T(i)/theta).^3).*Int; %Debye Heat Capacity Eq
      
end
 
on Phys.org
FactChecker said:
Store your values in an array, C(i), and plot C versus the array T.
Well that's the problem...idk how to do that...
 
FactChecker said:
plot(T,C);
There are many options that you can add. See this.

i meant i don't know how to store the values...
 
jkthejetplane said:
i meant i don't know how to store the values...
The easiest is to just change line 16 to C(i) = ...
But MATLAB is slow to increase the dimension of an array one at a time in the loop. So it can be sped up by adding an initialization of the C array with a line before the loop like this:
C = zeros(1, length(T))
 
FactChecker said:
The easiest is to just change line 16 to C(i) = ...
But MATLAB is slow to increase the dimension of an array one at a time in the loop. So it can be sped up by adding an initialization of the C array with a line before the loop like this:
C = zeros(1, length(T))
Thank you!