Matlab How to plot values from a loop

In summary, the Debeye theory is used to calculate the heat capacity of copper, with a specific focus on finding the molar heat capacity from 0-1083K (melting point of Cu). This involves using various constants such as Boltzmann's constant and Avogadro's number, as well as utilizing a specific function and integral. The values are stored in an array and plotted against the temperature, with the option to add additional features. To optimize the speed of the process, an initialization of the array is suggested before the loop.
  • #1
jkthejetplane
29
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
  • #2
Store your values in an array, C(i), and plot C versus the array T.
 
  • #3
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...
 
  • #4
plot(T,C);
There are many options that you can add. See this.
 
  • #5
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...
 
  • #6
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))
 
  • #7
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!
 

1. How do I plot values from a loop in Matlab?

To plot values from a loop in Matlab, you can use the "plot" function. Within the loop, you can store the values in an array and then use the "plot" function to plot the array against the loop index.

2. Can I plot multiple lines from a loop in Matlab?

Yes, you can plot multiple lines from a loop in Matlab. You can use the "hold on" command before the loop and then use the "plot" function inside the loop to plot each line. The "hold on" command ensures that the previous plot is not overwritten.

3. How can I customize the plot from a loop in Matlab?

To customize the plot from a loop in Matlab, you can use various functions such as "xlabel", "ylabel", "title" to add labels and title to the plot. You can also use the "legend" function to add a legend to the plot.

4. Can I save the plot from a loop in Matlab?

Yes, you can save the plot from a loop in Matlab by using the "saveas" function. This function allows you to save the plot in various formats such as png, jpg, pdf, etc.

5. How can I improve the efficiency of plotting values from a loop in Matlab?

To improve the efficiency of plotting values from a loop in Matlab, you can preallocate the array before the loop using the "zeros" or "ones" function. This will save time and memory as Matlab won't have to resize the array within the loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Advanced Physics Homework Help
Replies
1
Views
653
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Advanced Physics Homework Help
4
Replies
118
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top