73110: Computer Lab 1

In this lab you review some basic Matlab skills that will be useful in the later labs.

Every student should fill out his/her own lab worksheet. Do as many problems as you can.

This is not an exam! You are encouraged to discuss the problems with the teaching assistant and with your neighbour.

Problem 1

Introduce yourself to the student sitting closest to you. Answer: My neighbour's name is:__________________

Problem 2

Go to the Help menu in Matlab 6 and choose Matlab Help.
  1. Select the Getting Started document. The first two words in Chapter 5 "Programming with MATLAB" are Flow            . (Now that you know where this document is, you should come back some time and read it!)
  2. What do the commands semilogx, semilogy, and loglog do? Answer: ________________
  3. Use the search engine to find all functions that have something to do with Gauss. Answer: ____________
  4. How can you interrupt a running Matlab program? Answer: _____________  (Try it with the Matlab command 0:.01:10000 )

Problem 3

Enter the vectors y=[-2;-1;1], x=(1:3)' and explain what is being computed by the following commands:
  1. diag(y)\x   Answer: ____________
  2. y.\x  Answer: ____________
  3. y\x  Answer: ____________
  4. y(:,ones(3,1))  Answer: ____________
  5. diag(diag(y))  Answer: ____________

Problem 4

The Lucas numbers L(n) are generated by the recursive formula L(1) = 1, L(2) = 3, and L(n) = L(n-1) + L(n-2) for n=3,4,... Find L(42). (Hint: help for ) Answer: L(42) = __________ Plot L(n)/L(n-1) as a function of n and describe the curve. Answer: _____________

Problem 5

Explain the differences between the following commands, where A and B are matrices.
  1. A^2 vs A.^2   Answer: ____________
  2. A*B vs A.*B  Answer: ____________
  3. A==B vs A=B  Answer: ____________
  4. A' vs A.'  Answer: ____________  (hint: try a complex matrix)

Problem 6

Vectorise the following codes.
  1. for i=1:50, A(i,2)=1; end     Answer: _____________________
  2. for j=1:100, x(j)=1/j; end        Answer: __________________
  3. s=0; for k=1:100, s=s+rand; end     Answer: __________________

Problem 7

Write a script m-file (call it doit.m) that produces a single plot of the three Chebyshev polynomials x, 2x^2-1, 4x^3-3x, on the interval [-1,1]. Your plot should have a title, an x-axis label, a y-axis label, and a legend. Show your TA how your script works. Answer: The TA's comment was "___________"

Problem 8

Write a function m-file (call it harm.m) that computes the harmonic sum . What is harm(26000)? Answer: __________.

Problem 9

Modify your harm function to compute  for various positive values of a. Your function should use a default value of a=1 (use the nargin function). It should return an error message for invalid input values of n and a. It should have some documentation lines so that help harm prints out a useful explanation. Show your improved function to the TA. Answer: The TA's comment was: "_____________".

Problem 10

Before you exit Matlab, delete your doit.m and harm.m files.