Matlab How to plot values from a loop

  • Thread starter Thread starter jkthejetplane
  • Start date Start date
  • Tags Tags
    Loop Matlab Plot
Click For Summary
To plot values from a loop in MATLAB for the Debye theory heat capacity of copper, initialize an array for heat capacity before the loop using "C = zeros(1, length(T))". Store the calculated values in this array by modifying the loop to "C(i) = ..." for each temperature iteration. This approach improves performance by avoiding dynamic array resizing during the loop. Finally, use "plot(T, C);" to visualize the results. Proper initialization and storage of values are essential for efficient plotting in MATLAB.
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
 
Physics news on Phys.org
Store your values in an array, C(i), and plot C versus the array T.
 
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...
 
plot(T,C);
There are many options that you can add. See this.
 
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!
 

Similar threads

Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
Replies
9
Views
2K
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K