| Differences between revisions 16 and 24 (spanning 8 versions) | Back to page |
|
Size: 2651
Comment:
|
Size: 3214
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 26: | Line 26: |
| end | end |
| Line 34: | Line 34: |
| %Octave-specific -- uncomment this line for using with Octave. %print -djpg E2.3.oct.jpg |
%Comment these out for batch mode %print -djpeg E2p3.jpg %print -depsc E2p3.eps %print -dpng E2p3.png %exit |
| Line 42: | Line 45: |
| To run the above code with Octave, you should un-comment the last line, and then run | To run the above code with Matlab in batch mode (after commenting out at least one of the print lines): |
| Line 44: | Line 47: |
| octave E2.3.oct.m | matlab -nosplash -nodesktop -minimize -r E2p3 |
| Line 46: | Line 49: |
| Of course, for this command to run successfully, octave must be in the path (which is ''not'' the case by default for Windows installation! -- I had to add the Octave bin directory to path manually!), and E2.3.oct.m must be the file name of the above code. | To run the above code with Octave in batch mode (after commenting out at least one of the print lines): {{{ octave E2p3.m }}} Of course, for this command to run successfully, octave must be in the path (which is ''not'' the case by default for Windows installation! -- I had to add the Octave bin directory to path manually!), and E2p3.m must be the file name for the above code. |
| Line 65: | Line 73: |
| plot (dE, 1./(1 + exp (dE / (k * T)))) | plot (dE, 1./(1 + exp (dE / (k * T)))) |
| Line 69: | Line 77: |
| xlabel ('E - E' + r'$_\mathrm{F}$' + ' (eV)') ylabel('f (E)') |
xlabel (r'E - E$_\mathrm{F}$ (eV)') ylabel ('f (E)') |
| Line 72: | Line 80: |
| text(-.04,.12,'T=100K') | text (-.04,.12,'T=100K') |
| Line 74: | Line 82: |
| # after showing, it is possible to save the plot as an image. | # Sort of equivalent to "print" of matlab. "print" is a python keyword. # Comment out a line to get a print-out automatically. #savefig ('E2p3_py.svg') #savefig ('E2p3_py.eps') #savefig ('E2p3_py.png') # After showing, it is possible to save the figure (again). # This line can be commented out if one of the above "savefig" lines are turned on. |
| Line 76: | Line 91: |
| Line 78: | Line 94: |
| You should get an image like this, when you save the image. | You should get an image like this (click to see the full image), when you save the image from the interactive window that pops up on "show ()". |
Here is some discussion of the programming part of Homework #4.
First, here is some base-line discussion for Matlab and Python that you might find useful. Please read it, or at least run a quick scan through it.
Now, for this homework, you basically need to calculate some functions and plot up the results. Example 2.3 of the textbook is a pretty good "template" for this task, and so I will discuss that example here.
Here is the Matlab code for Example 2.3. This code is somewhat different from the code in the text book, but does the same thing.
%E2.3: Fermi Function Calculation, f(E-EF,T)
%Initialization
clear
close
%Constant
%25.85 meV for 300 K is an equivalent way to remember it.
k=8.617e-5;
%Google for "Matlab linspace", to find out what linspace does!
dE=linspace(-0.2,0.2);
for ii=1:4;
T=100*ii;
kT=k*T;
f(ii,:)=1./(1+exp(dE./kT));
end
%Plotting result
close
plot(dE,f); grid;
xlabel('E - E_F (eV)'); ylabel('f (E)');
text(.05,.22,'T=400K'); text(-.03,.12,'T=100K');
%Comment these out for batch mode
%print -djpeg E2p3.jpg
%print -depsc E2p3.eps
%print -dpng E2p3.png
%exit
The obtained images by Matlab and Octave are as follows (click to see full images).
To run the above code with Matlab in batch mode (after commenting out at least one of the print lines):
matlab -nosplash -nodesktop -minimize -r E2p3
To run the above code with Octave in batch mode (after commenting out at least one of the print lines):
octave E2p3.m
Of course, for this command to run successfully, octave must be in the path (which is not the case by default for Windows installation! -- I had to add the Octave bin directory to path manually!), and E2p3.m must be the file name for the above code.
Here is the Python code for Example 2.3.
# E2.3: Fermi Function Calculation, f(E-EF,T)
# pylab is part of matplotlib, a matlab like environment in python.
# http://matplotlib.sourceforge.net/index.html
# Lots of names are imported by this (numpy, scipy, matplotlib, I think).
from pylab import *
# 25.85 meV for 300 K is an equivalent way to remember it.
k = 8.617e-5
# linspace defaults to 50 points -- I want 100 (like in matlab).
dE = linspace (-0.2, 0.2, 100)
Ts = [100, 200, 300, 400]
for T in Ts:
plot (dE, 1./(1 + exp (dE / (k * T))))
grid ()
# matplotlib comes with its own LaTeX parser -- cool!
xlabel (r'E - E$_\mathrm{F}$ (eV)')
ylabel ('f (E)')
text (.05,.22,'T=400K')
text (-.04,.12,'T=100K')
# Sort of equivalent to "print" of matlab. "print" is a python keyword.
# Comment out a line to get a print-out automatically.
#savefig ('E2p3_py.svg')
#savefig ('E2p3_py.eps')
#savefig ('E2p3_py.png')
# After showing, it is possible to save the figure (again).
# This line can be commented out if one of the above "savefig" lines are turned on.
show ()
You should get an image like this (click to see the full image), when you save the image from the interactive window that pops up on "show ()".


