GEOS 597e: Spatiotemporal Data
Analysis Workshop
Homework 1: Practice programming in MATLAB
Last updated 8/30/06. To be completed prior to class
session Weds., Sept. 7th, with results handed in as hard copy.
Introduction. Now that we're set up to
compute in MATLAB, let's take a session to perform some simple
problem-solving exercises in the environment. The following
exercises are either verbatim or modified from Pratap
(2002), Getting
Started with MATLAB: A quick introduction for scientists and engineers,
Chapter 2. Reporting your results to three decimal places should
be sufficient. Alhtough you needn't script anything until problem 6,
you may find it helpful to script your answers, naming results as
variables q1a,
q1b, q1c, q2a, q2b, ... or some other convention.
- Arithmetic operations.
Compute the following quantities:
- 25/(25-1)
and compare with (1-2-5)-1.
- 3(51/2-1)/(51/2+1)2
- 1. The square root x0.5 can be calculated with the
command sqrt(x)
or x^0.5.
- Area = pi*r2,
with r=pi1/3-1.
(pi=3.14159265358979...
in MATLAB).
- Exponential and logarithms.
The mathematical quantities ex, ln x and log x are calculated with exp(x), log(x), and
log10(x),
respectively. Calculate the following quantities:
- e3, ln(e3),
log10(e3), and log10(105).
- Solve 3x
= 17 for x and check
the result.
- Complex numbers.
MATLAB recognizes the letters i and j as the
imaginary number -11/2. A complex number 2+5i may be input as 2+5i or 2+5*i in
MATLAB. The former case is always interpreted as a complex number
whereas the latter case is taken as complex only if i has not
been assigned any local value. The same is true for j.
This kind of context dependence, for better or worse, pervades
MATLAB. Compute the following quantities:
- e(i*pi/4). Check Euler's
formula eix
= cosx + i sinx by computing right and left hand sides of the
equation with x=pi/4.
- Execute the commands exp(pi/2*i)
and exp(pi/2i).
Can you explain the difference between the two results?
- Multiply, divide and
exponentiate vectors. Create a vector t with 3 elements:
1,2,3. Now compute the following quantities:
- x=tsin(t).
- y=(t-1)/(t+1).
- z=sin(t2)/t2.
- Matrices, vectors, and plotting.
Create a vector and a matrix as follows: v=0:0.2:12;
and M=[sin(v);
cos(v)].
- Find the dimensions of v and M using the
size command.
- Extract the first 10 elements of each row of the matrix, and
plot them as a scatter plot. Label the x and y axes, and give
your plot a title.
- Use the command plot3(x,y,z)
to plot the circular helix x(t)=sint,
y(t)=cost, z(t)=t, 0<=t<=20. Label axes
and give your plot a title.
- Plot y=cosx and z=1-x2/2+x4/24
for 0<=x<=pi on the
same plot. (Hint: you can use plot(x,y,'-o',x,z,'--')
or you can plot the first curve, use the hold on
command, then plot the second curve, and then use hold off.)
- Scripting. Write a simple
script called circle.m to draw a circle with radius input at a
prompt. Here's an outline of the script in comments:
% circle - a
script file to draw a circle of radius input by the user
% after file
written by Rudra Pratap (6/28/98); last modified DD/MM/YY by MNE.
% get radius r from data input
% create vector theta with
100 elements from 0 to 2*pi
% generate x coordinate; x=rcos(theta)
% generate y coordinate: y=rsin(theta)
% plot the circle
% set an equal scale on both x and y axes
title(['radius=',
num2str(r)]) % give the graph a title
with the value of r
% end of
script
-----------------------------------------------------------------------------------
Commands you may use in your script
are: input,
linspace, cos, sin, plot, axis, title, num2str, help.
- Check in. Send me an email telling me
whether this
introduction to MATLAB programming was
- old hat; not too useful
- somewhat unfamiliar territory; somewhat useful
- very unfamiliar and new; very useful.
Please add to your email any
outstanding questions
we didn't answer in class.
Back
to Schedule/Syllabus.