Matlab III

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

Matlab #1

Getting started

Matlab #2

Dice

Matlab #3

Circles

Matlab #4

Histograms

Matlab #3

Synopsis
  1. plotting;
  2. colours;
  3. saving plots to file;
  4. more number sequences;
  5. nested loops;
  6. taking input during runtime from keyboard (standard input);
  7. arrays;
  8. functions and returns;
  9. while;
  10. storing values before they get overwritten.

Do not forget

In every program you encounter you should be able to

  1. Identify parts you may change:
    • variables;
    • arrays;
    • arguments.
  2. Identify parts you may not change:
    • built-in functions;
    • keywords.
  3. List the exact order line by line how Matlab executes the program.
  4. Draw the flowchart.

Exercise
  1. For program version #4, mark out the:
    • variable names;
    • arguments;
    • functions;
    • keywords.
  2. Repeat Exercise 3 with program version #5.
  3. Repeat Exercise 3 with program version #7.
  4. Repeat Exercise 3 with program version #8.
  5. Repeat Exercise 3 with program version #9.
  6. Repeat Exercise 3 with program version #10.
  7. Repeat Exercise 3 with program version #11.
  8. Repeat Exercise 3 with program version #12.
  9. Repeat Exercise 3 with program version #13.
  10. Draw the flowchart for program version #8.
  11. Draw the flowchart for program version #12.
  12. In this lesson we made 12 revisions to the program. Summarise the improvement in each version in a 3-column table:
    versionimprovement in plain Englishtechnical modification in Python
    #1 to #2
    #2 to #3
    #3 to #4
    #12 to #13
  13. Write a program that prints the following with as few repetitions as you can:
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
               *..*..*..*..*
                *..*..*..*..*
                 *..*..*..*..*
              
  14. Write a program that prints the following with as few repetitions as you can:
               *
               **
               ***
               ****
               *****
               ******
               *******
               ********
               *********
               **********
               *********
               ********
               *******
               ******
               *****
               ****
               ***
               **
               *
              
  15. Write a program that prints the following with as few repetitions as you can:
                      *
                     * *
                    *   *
                   *     *
                  *       *
                 *         *
                *           *
               *             *
              *               *
               *             *
                *           *
                 *         *
                  *       *
                   *     *
                    *   *
                     * *
                      *
              

    Hints:

    • Identify what is varying by a periodic trend: is it the number of asterisks or the number of spaces?
    • Build a two-column table, each row of your table corresponding to each row from the diamond above:
      • 1st column: with the number of spaces before the first asterisk on each line;
      • 2nd column: with the number of spaces between the asterisks on each line.
    • Let the 1st column be x, let the 2nd column be y, we can see that they have a linear relation and will fit into an equation y = mx + c, where m is the gradient and c is the offset/constant.
    • Select any pair of rows from your table, use the x and y values to solve the simultaneous equations to derive m and c.
    • Apply the formula you have just derived for the nested loops in your program.
  16. Write a program that prints the following with as few repetitions as you can:
                      *               *
                     * *             *
                    *   *           *
                   *     *         *
                  *       *       *
                 *         *     *
                *           *   *
               *             * *
              *               *
               *             * *
                *           *   *
                 *         *     *
                  *       *       *
                   *     *         *
                    *   *           *
                     * *             *
                      *               *
              


Matlab programming

Matlab #1

Getting started

Matlab #2

Dice

Matlab #3

Circles

Matlab #4

Histograms