Matlab

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:

Matlab #1

Getting started

Matlab #2

Dice

Matlab #3

Circles

Matlab #4

Histograms

Matlab #1

Preliminaries
  1. You have 3 choices of running the programs presented on this site:
    • Matlab on your computer or network. Matlab license has to be paid for. Many institutions have Matlab licenses for staff and students.
    • Octave on your computer or network. In case you do not have access to any Matlab license, you may use Octave. Octave is free to download and install. It runs on Windows, Mac and Linux. Most Matlab and Octave programs can be used interchangeably. Programs given here have been tested on both Matlab and Octave.
    • Octave on PythonAnywhere. This option is highly recommended if you are starting fresh without pre-installed Matlab or Octave. You won't need to install anything anywhere, just follow the slides step by step.
  2. Flexi-programmers at Penang Science Cluster should keep a green exercise book recording all solved exercises. We'd like to be as green as possible — save your answers as files in a designated directory/folder for easy retrieval anytime in the future. For exercises you find difficult to put in soft copy e.g. flowchart drawing, you may draw by hand and retain the hard copy for easy retrieval in the future.
  3. Slides here are continuously being revised and updated. If you have been here before, your browser might have some old files cached. To ensure that you see the latest version, please follow the instructions here: ☞ how to clear browser cache.

Synopsis
  1. the fprintf function;
  2. variables;
  3. arithmetic operators;
  4. running Matlab interactively vs running from saved .m files;
  5. auto-completion (TAB key) and command history (UP and DOWN arrow keys);
  6. for…end loops:
  7. sequence of numbers;
  8. commenting;
  9. the help function.
Exercise
  1. Refer to the program on slide #33. What happens if we make five changes
    • the 'n' on line #16 to 'a';
    • the 'n' on line #22 to 'b';
    • the 'n' on line #28 to 'c';
    • the 'n' on line #34 to 'd';
    • the 'n' on line #40 to 'e',
    and then run? What changes on other lines do we need in order to make the program work?
  2. Write a program that prints the following with as few repetitions as you can:
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
                 .oooooooooooooooooo.
              
  3. Write a program that prints the following with as few repetitions as you can:
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
                 .o.o.o.o.o.o.o.o.o.o
              
  4. Write a program that prints the following with as few repetitions as you can:
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
                 .ox.ox.ox.ox.ox.ox.ox
              
  5. Write a program that prints the following with as few repetitions as you can:
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
                 .ooxxx.ooxxx.ooxxx
              
  6. Write a program that prints the following with as few repetitions as you can:
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              *...*****.......*********
              *********.......*****...*
              
  7. Write a program that prints a multiplication table for 3:
    133
    236
    339
    12336

  8. Write a program that prints the multiplication tables for 2, 3, 4, 5, 6, 7, 8 and 9:
    122
    224
    326
    12224
    133
    236
    339
    12336
    14 4
    24 8
    34 12
    124 48
    15 5
    25 10
    35 15
    125 60
    16 6
    26 12
    36 18
    126 72
    17 7
    27 14
    37 21
    127 84
    18 8
    28 16
    38 24
    128 96
    19 9
    29 18
    39 27
    129 108
  9. Write a program that prints a combined multiplication table for 2, 3, 4, 5, 6, 7, 8 and 9 as follows:
    n22*n33*n 44*n 55*n 66*n 7 7*n 8 8*n 9 9*n
    122334455667 7 8 8 9 9
    224 36 48 510 612 7 14 8 16 9 18
    326 39 412 515 618 7 21 8 24 9 27
    2 3 4 5 6 7 8 9
    2 3 4 5 6 7 8 9
    12224 336 448 560 672 7 84 8 96 9 108

Matlab programming

Matlab #1

Getting started

Matlab #2

Dice

Matlab #3

Circles

Matlab #4

Histograms