

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


MATH 313 Spring 2009, Homework 3, Computer-based problems
As I mentioned in the first Matlab assignment, in Matlab, almost everything is a matrix or a vector. In that assignment, though, we didnât really work much with matrices. In this assignment we do! Matrices can be loaded into Matlab in a variety of ways. The easiest is to simply list the entries with semicolons to separate rows:
theta=pi/4; a=[cos(theta) sin(theta); -sin(theta) cos(theta)]
If you want to know how big a matrix is, use the âsizeâ command:
size(a)
will tell you that a is 2x2. For small matrices, this isnât very useful, but for larger ones itâs handy. If you want a matrix with random entries, for instance a 7x matrix, use
b=rand(7,4)
To get a new random matrix of the same size, you can use
c=rand(size(b))
There are other useful matrix-making commands, such as âzerosâ, âonesâ, âeyeâ, and âmeshgridâ. Read about them! (Also, Matlab can read almost any data file format you can imagine, like JPEG images! Look into the documentation or ask me if youâre interested...) In the previous assignment, we used the elementwise operations to avoid loops. Now that you know more about matrices, we can use matrix operators to do useful things to matrices. For instance, we can multiply two random matrices
rand(5,3)*rand(3,4)
In Matlab, we have easy access to gaussian elimination. Itâs essentially matrix division, so Matlab represents it by â\â. In particular, if you want solve Ax = b, weâd execute
x=A\b
Imagine that weâre writing Aâ^1 b as (^) Ab , and youâll remember to use the backslash. (If you wanted to write bAâ^1 for b a row vector, you can use
x=b/A
which result in x being a row vector...) Matlab also provides a way to calculate matrix inverses using the âinvâ com- mand. As we discussed in class, matrix inversion is not (numerically) the same as gaussian elimination. In this problem, we examine this a little (courtesy of the MATLAB documentation). Type the following into Matlab:
n = 500; Q = orth(randn(n,n)); d = logspace(0,-10,n); A = Qdiag(d)Qâ; x = randn(n,1); b = A*x;
2
Now, youâve got a matrix A (yes, itâs invertible) with a random vector x, and b, their product. The task is to recover an approximation of x... Obviously, x = Aâ^1 b, so try
y = inv(A)*b;
Now, using gaussian elimination, we should execute
y2=A\b;
Now these two arenât very different, but compare the error
err = norm(y-x)
with
err2 = norm(y2-x)
Itâs not so different... But the residuals
res = norm(A*y-b)
and
res2 = norm(A*y2-b)
differ significantly! Matlab provides the ability to define new functions, which is an important way to manage complicated programs. A function has a number of inputs and a number of outputs... Like scripts, functions are stored in plain text files with the extension â.mâ. However, the first line of a function file must begin with the word âfunctionâ and then an input/output specification. For instance, the following could be stored in âhelix.mâ
function [x,y,z]=helix(t) % Compute points on a parametric curve (a helix) % Input: t = matrix or vector of parameter values to evaluate % Output: x,y,z = coordinates of points computed
x=cos(t); y=sin(t); z=t;
Notice that the function name in the top line must match the filename! If this file is stored in Matlabâs current directory, you can call it:
[x,y,z]=helix(0:0.1:3*pi); plot3(x,y,z);
Problem 1 Write a function to do something interesting. For instance, you could wite a function that computes the number of bus rides I can take, given whatâs in my pocket: pennies, nickels, dimes, quarters, and SEPTA tokens... (Discrete differentiation and integration.) Suppose v is a column vector, which might represent samples of data. If we wanted to approximate a running integral of v =
â« (^) t 0 v(x)dt, we might compute a cumulative sum of^ v.^ (Thereâs a Matlab command to do this, âcumsumâ, but we wonât use it for this problem.) This can be computed by multiplying v with a lower-triangular matrix of ones: