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

Discussion Overview

The discussion revolves around plotting values from a loop in MATLAB, specifically focusing on calculating and visualizing the molar heat capacity of copper using Debye theory. The scope includes technical explanations and programming challenges related to MATLAB coding.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant presents a MATLAB code snippet for calculating the molar heat capacity of copper using Debye theory, outlining the necessary constants and the integral function.
  • Another participant suggests storing the calculated heat capacity values in an array, C(i), and plotting C against the temperature array T.
  • A participant expresses uncertainty about how to store the values in the array, indicating a lack of understanding of the process.
  • Multiple participants reiterate the need to plot the values and provide similar suggestions for storing the values in the array.
  • One participant advises that to improve performance, the C array should be initialized before the loop to avoid slowdowns associated with dynamically resizing the array during iterations.

Areas of Agreement / Disagreement

Participants generally agree on the need to store values in an array for plotting, but there is some uncertainty regarding the implementation details, particularly about how to effectively store the values in MATLAB.

Contextual Notes

There are limitations regarding the clarity of the code implementation, particularly in the initial setup of the array and the handling of MATLAB's performance considerations when resizing arrays.

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
2K
  • · 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