Sei sulla pagina 1di 3

MATLAB tutorial 1: using the command line

Start matlab: open a terminal and type matlab &


At the matlab command prompt type
a=1
and press enter. Next
b = 1.0
and press enter. To see what variable are currently defined type
whos
You should see something like:
>> whos
Name
Size
Bytes Class Attributes
a
b

1x1
1x1

8 double
8 double

An array is a list of numbers assigned to one variable. Type


x=[012345]
we may also use
y = 0:0.5:5
Try
x(3) = 22
To prevent matlab from printing out the entire array every time we change something, we add a
semicolon at the end of the command:
x(4) = -10;
Arrays can be manipulated using matrix algebra:
z = 2*y
or
w = 2*y+z
Now try
x*x
x.*x
x*x'
x'*x
Note that the single dash after an array means transpose i.e. a swap of the rows and columns. Note
also that the dot in front of the multiplication sign of the second command means we multiply elementby-element.

Let's try to plot something:


plot(x,3*x)
A new plot window should have appeared. Now try
u = x.*x
plot(x,u)

We can save this plot as a png file by


print -dpng
or as an encapsulated postscript file by
print -depsc2
These commands created the files figure1.png and figure1.eps, which we could rename (e.g. mv
figure1.png plot_x-squared.png on the linux command line) if we wanted to keep them.
We will now read in some data from the file partdiv.txt. Looks what's inside this file by typing less
partudiv.txt on the LINUX command line. Use the up and down arrows and page-up & page-down to
navigate the file. When you're done press q.
Load the data into matlab with:
[congress, year, total, dems, reps] = textread('partydiv.txt', '%s %f %f %f %f',78,'headerlines',3)
Be careful here, there is a space before each % sign.
Let's make some plots:
plot(year,seats)
xlabel('Year')
ylabel('Total House Seats')
Now try
plot(year,dems./total,'-b')
xlabel('Year')
ylabel('Percentage')
We can overplot by turning the hold on
hold on
plot(year,reps./total,'-r')
hold off
We can add a legend with
legend('democrats', 'republicans')
You can move the legend around using the GUI by first clicking the arrow, then selecting the legend
box and dragging it.
You can get a sense for how correlated (or anti-correlated) a dataset is by looking at the correlation
coefficient:
corrcoef(dems./total,reps./total)
gives you a 2x2 matrix of coefficients relating each column to itself (along the diagonal) and to the
other column(s) (off-diagonals).

TASK
Now that you have completed your tutorial, lets do some science!
Inside your home directory, you should find a file called RecentIndices.txt. Use Linux to see what is
inside the file and what every column of the data is. The first column is the year and the second is the
month of the observation. The third column is the observed sunspot number, the 8th is the observed
radio flux, and the 10th is the observed AP index (a geomagnetic index).
To complete this task, you must
1) generate an image (png) files that contain the sunspot number as a function of time, the radio flux as
a function of time, and the observed AP index as a function of time. Plot the lines in different colors
and make a legend
2) generate 2 postscript files, one that contains the scatter plot of the sunspot number on the x-axis and
radio flux on the y axis and one that contains the sunspot number vs AP index. Calculate the
correlation coefficient between the two values and include that information in the title of the plot.
Be sure to label all axes of your plots.
When you are done with this task, email the results (3 files) to jacob.heerikhuisen@uah.edu

Potrebbero piacerti anche