(Alternate title: BUILD A BASIC SCRIPT WITH MATRICES IN MATLAB)

In the first part of this lab, you created a Windows-based console application in C++ using Microsoft Visual Studio. This time you need to achieve the same thing in the Matlab environment.

Step 1: Find and get familiar with Matlab

This lab assumes you’ve already got Matlab installed on your computer or in your lab.

If that’s right then go find it and get familiar with the environment.

Here’s a decent first tour of the Matlab Development Environment »

(If you don’t already have it you might qualify for the student edition »)

Step 2: Create a script file

You can use Matlab as a super souped up calculator but it’s also a powerful programming language, an interactive computational environment, and a great way to visualize things.

A simple way to tap this power is through scripts.

So, follow these steps » to create and run a simple script file.

Be sure to give it an appropriate name, e.g. YourName-Lab1Part2.m

When doing this myself I found the context and tips provided here to be useful:

And again, Google has pretty much every answer to every question. Don’t hesitate to use it.

You’ll notice that you don’t need to include a linear algebra library in Matlab since all of that is built in. And that things like declaring variables memory allocation are usually done more easily.

Step 3: Make your script do some great stuff

Once you’ve got a simple script up and you know how to run it (Step 2 above), I want you to have your program do the following – just as you did in the C++ application from Part 1:

1. Output a short welcome message in this format:

Hi. My name is [your first name]. Welcome to my program!

I want you to use the fprint() function for messages like this.

2. “Declare” a 2 x 2 matrix (you’ll see that this is done rather differently in Matlab than in C++)

3. Assign values to your matrix one element at a time such that it has elements with values given by the following:

    \begin{equation*} $\mathbf{M} =\begin{bmatrix} m_{1,1} & m_{1,2} \\ m_{2,1} & m_{2,2} \end{bmatrix} \end{equation*}

where

    \begin{equation*} \begin{split} m_{1,1} = 3 \\ m_{1,2} = 2.5 \\ m_{2,1} = -1 \\ m_{2,2} = m_{2,1} + m_{1,2} \end{split} \end{equation*}

4. Output your matrix to the screen after the following message:

The first matrix is as follows:

You can use the disp() function for outputting vectors and matrices in this lab. (And, if you wish to get ahead, you can learn more about the difference between disp() and fprintf() here »)

5. Resize the original matrix to a 3 x 3 matrix and output its contents after the following message:

After resizing, my matrix is as follows:

6. Assign the following new values to the resized matrix, all at once (not one element at a time):

    \begin{equation*} $\mathbf{M} =\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \end{equation*}

7. Output the matrix to the screen again after the following message:

With new elements, my matrix is as follows:

8. Output the sum of all elements of the matrix after the following message:

The sum of all its elements is:

9. Output the transpose of the matrix (without altering the contents of the matrix), after the following message:

And the matrix has the following transpose:

Step 4: Properly document and organize your code

Make sure to properly document and organize your code. You’ve already seen the Guidelines for C++ » which you will have to modify for Matlab using its rules:

Again, be sure to include the course name and number as well as your name and number in the opening comments.

Step 5: Submit your script and output

When you’ve completely finished your application you need to prepare it for submission.

To do this, add the following to your report:

  1. The script (the .m file) as an appendix to the report you submit for this lab
  2. A screenshot of the output you get when running the application yourself – include this in the body of your report for this part

Step 6: Move to Part 3 of this lab when you’re ready

Once you’ve finished this part of the lab you can proceed to Part 3 in the next lesson using the buttons below.

In order to do this you will need to be signed in (at the top right corner of the page) and then press “Unlock the next lesson”.