GS 2001/Math - Collins
Lab 2
Programming in MATLAB

Preliminaries

Start MATLAB (from Start Menu).
Arrange your windows so you can see MATLAB and this webpage.

Scripts and Functions

Programming in MATLAB is pretty straight-forward as all you
do is put the commands you want to use together in a file.

There are two types of programs that MATLAB uses:
    Scripts   and    Functions
Both are called "M-Files" because the file name is whatever.m

To create a M-File, go under the file menu and choose New M-File.
This will bring up a new window which you can type in.  You might
want to move/resize windows so you can see all three at once.

First Script

The first commands we will use are input, disp and for.  Type
     help input
     help disp
     help for
to learn more about these commands.

Our first program will be a simple script, it will
    Prompt the user to enter their name
    Prompt the user to enter a number
    Print out their name the given number of times

To do this you will type the appropriate commands in the M-file window.
When you are done, save the file with the name "myname.m". Make sure
that the file goes in the Work Folder under MATLABR11.
To run your program, you just type myname in the 
MATLAB window.

You can try to write this program now, or you can read on and get more hints.
I put a complete version of this script at the bottom of this page.  You
can copy and paste if you don't want to program it.















Hints:

    Put a semi-colon (;) at the end of statements to keep them
    from printing out.


    To read a string of characters (like a name), you use
      name = input('What is your name','s')
      (the 's' tells MATLAB to interpret the input as characters)

    To do something N times where N is a variable, you need to use a
    for loop,  like this
      for i = 1:N
         do the stuff
      end

Second Script

This script (call it "mymatrix.m") will do the following:
    Read in a number from the user (call it N).
    Create a N x N matrix (call it A), with entries
         A(i,j) = i/j
    Compute the determinant of A and display it


When you get done, try it with various sizes, like N = 2, 3, 4, ..., 10.

Do you see a pattern in the determinant?

Again, you can start doing it right now, or read some hints, or skip
down to my version at the bottom.
















Hints:

    Use two for loops to go through all the entries of A.


    The two loops will look like
        for i = 1:N
           for j = 1:N
              stuff here
           end
        end



    The command for determinant of A is det(A).

First Function

The main two differences between a script and a function are that
(1) all the calculations and variables in a function are only available
    in the function; while in the script they are available in the main
    part of MATLAB.  This is called the scope of the variables.
(2) because of the scope, functions start with a line telling which
    variables will be passed in and which will be passed out of the
    function

A function starts with a line like
    function output = nameoffunction(input)
You can have several inputs and several outputs, but we'll start with
one of each.

Write a function called "myfun.m" which does the following
    Takes a value N as input
    Computes the matrix A as in the 2nd Script A(i,j) = i/j
    Passes the resulting matrix back to the user

To test it compare the results from "mymatrix" with N = 4 and
the results from typing myfun(4).

As before, you can start now, read some hints, or jump to the answer.























Hints:

    The first line might look like
       function A = myfun(N)
    

    The body of the function will be the two loops from "mymatrix"

    
    Only A will be sent back to the user, so make sure that you call
    your answer A.


Challenges

Here are some additional program ideas you might try.  You can
do all the changes in the "myfun.m" M-file.

    a.  Create a more complicated formula for the elements of A.
        For example, make A(i,j) = 0 if  i<2 or j>4.
        
    b.  Have the function return A and the determinant of A.

    c.  Pass the function two values N and M.  Make the matrix be
        N x M.

    d.  Re-write "mymatrix.m" so that is uses "myfun.m"









---------------------------------SOLUTIONS-------------------------------
To use these, select all the lines in green, copy them, and paste them
into the M-file editor window.  Save and run.


SCRIPT 1  myname.m

% SCRIPT 1  myname.m  by Dr. Collins 6/19/01 for GSS:Math

name = input('Enter your name: ','s');
N = input('How many times do you want to see it? ');
for i = 1:N
   disp(name)
end


SCRIPT 2  mymatrix.m

% SCRIPT 2  mymatrix.m  by Dr. Collins 6/19/01 for GSS:Math

N = input('Enter the size of the matrix: ');
for i = 1:N
   for j = 1:N
      A(i,j) = i/j;
   end
end
disp(det(A))


FUNCTION 1 myfun.m

function A = myfun(N)
% A = myfun(N)

% FUNCTION 1  myfun.m  by Dr. Collins 6/19/01 for GSS:Math

for i = 1:N
   for j = 1:N
      A(i,j) = i/j;
   end
end




Mail: ccollins@math.utk.edu