Programming in Matlab: (Attaway 2, 5)

Zen and the Art of Programming

Programming is like building a motorcycle in that we want to create a complex and beautiful artifact * that functions correctly and efficiently *.

(Of course in reality, programs are not always beautiful... *, or as functional as hoped... *)

Matlab Is what it Is

Algorithms (Attaway 2.1)

Intuitive Notion of Algorithm

Computational Notion of Algorithm

Input

Processing

Output

Keyboard Input (Attaway 2.3)

Printed Output (Attaway 2.3)

File Input (Attaway 2.6)

File Output (Attaway 2.6)

Review: Scripts and Functions

Write a script that calls functions....

E.g. in current programming exercises, Attaway 5.18, 5.23.

Say we want: function to get input from user, function to compute results, function to plot. All held together by a script. Given a function mpg() that returns mpg given a vehicle's weight and speed, write a script to get data from user, derive his mpg at a range of speeds, and plot the result. Need four .M files and a script:

function [min_speed, max_speed, weight] = get_speedrange_and_weight(); % perhaps query user, read a file, whatever... function mileage = mpg(speed,weight); % given from physics or empirical model: % returns one mpg figure for given speed and weight function [speedvec, mpgvec] = mpg_range(lowspeed, highspeed, wt); % calls mpg() for a wt and range of speeds function display_mpg_graph(speedvec, mpgvec, title_string); % plots mpg for my range of speeds and weight % with given title. ... % main script (note use of names!) [lo, hi, wt] = get_speedrange_and_weight; [xvec, yvec] = mpg_range(lo, hi, wt); display_mpg_graph(xvec, yvec, 'MY Hummer MPG');

Programming and Problem-Solving

Flowcharts

There have always been graphic aids for programmers to plan or document algorithms. They're trendy, depending on the characteristics of the languages and current 'wisdom' (theories).

In flowcharts, emphasis is on program flow, loops, testing. Box shape indicates functionality. Can get huge.

From Google Images, which has quite a lot of amusing and edifying examples.

Unified Modeling Language

What would one expect in the days of XML?

UML Examples

These later diagrams emphasize the object-oriented model: conceive program pieces as objects to which one sends messages and from which one gets replies. What goes on inside is generally not to be visible or accessible.

A Good Bad example: Matlab 2-D arrays are actually stored as 1-D arrays, and you are invited to use this fact (say by indexing with a single dimension) rather than not know it.

Abstract Data Structures, Objects and Methods: Ways to keep knowledge about structure from user. Yields transparency, modularity and allows invisible revision of the implementation.

Programming Hints

Just our opinions..

Programming for Debugging

Attaway 5.5

Demo 1

Debugging Tools

Attaway 5.1

Debugging is optional CSC160 content. It might grab you or leave you cold. Enjoy or ignore. I've only used Matlab's debugger a bit, but it's standard, easy, and worth getting acquainted with, since such a tool can be VERY helpful in a language like C or Java.

A Matlab Debugging Doc Page

Demo 2

"Busy Work !?!"

This course is only partly about programming, and less about Matlab.

It's mostly about the scientific and engineering techniques that make up the rest of the course.

This future material is not "busy work" for random Matlab practice. Far from it -- it is Matlab whose importance recedes (check the grade weightings on the projects).

Matlab, as well as mathematics, are now just tools to use (and we hope expand your command of) for experiments and analysis in several centrally-important engineering techniques.



---

Last update: 04/22/2011: RN