Lecture 1 Introduction

- Lecture series based on the text
- Essential MATLAB
- for Engineers and Scientists
- By
- Hahn Valentine
- -------------------------------------------------

Objectives of course

- Learn how to examine, explore and evaluate

MATLAB. - Learn how to do technical computing with MATLAB.
- Learn how to design programs to solve technical

problems via structure plan (i.e., a design

methodology). - Learn to formulate algorithms for the steps of

the structure plan. - Learn how to translate the steps into computer

programs to solve engineering and scientific

problems.

Objective of this lecture

- Provide an overview of some of the features of

MATLAB as a way to begin your - Evaluation of this new technical computing tool.
- Training in the art of computer programming.
- Learning to use MATLAB as a notepad for

mathematical computations that arise in

engineering and science courses.

MATLAB desktop

Command Window Command History Window Workspace

Window Current Directory Window Start Button

Command Window

- The Command Window on the right is the main panel

where you interact with MATLAB. - You key (or type) and ltEntergt commands after the

prompt gtgt MATLAB executes the commands and

displays results (if requested). - Some commonly used tools and commands
- ? (up arrow) returns last command input, can be

repeated - clc clears the screen
- whos shows list of variables
- clear clears variables

Evaluation of MATLAB

- HANDS-ON with MATLAB
- Type
- gtgt 23 ltEntergt
- into the Command Window
- gtgt clc ltEntergt
- gtgt whos ltEntergt
- Throughout the lecture, yellow text indicates

what you should type into MATLAB.

Command History Window

- The Command History Window logs all of the

commands you enter in MATLAB. - It should have logged 23.
- Use the Command History Window to reenter 23 in

the command window (use copy-and- paste or double

click on 23). - This is useful to retrieve past commands.
- Use Shift key to select multiple lines.

Arithmetic with MATLAB

- Let us explore by doing exercises
- gtgt 32 ltEntergt
- gtgt 32 ltEntergt
- gtgt 3/2 ltEntergt
- gtgt 3\2 ltEntergt
- gtgt 32 ltEntergt
- gtgt 2/0 ltEntergt
- gtgt 0/2 ltEntergt
- gtgt 3Inf ltEntergt

Algebraic-numeric computations

- Let us explore by doing exercises
- gtgt a 3 ltEntergt
- gtgt b 2 ltEntergt
- gtgt a b ltEntergt
- gtgt a / b ltEntergt
- gtgt a2 ltEntergt
- gtgt c a b ltEntergt
- gtgt d c(b1) ltEntergt
- gtgt who

Hiding Output

- Let us explore by doing exercises
- gtgt clear clc ltEntergt
- gtgt whos ltEntergt
- gtgt a 3 ltEntergt
- gtgt b 2 ltEntergt
- gtgt c a b ltEntergt
- gtgt d c(b1) ltEntergt
- gtgt who ltEntergt
- gtgt a, b, c, d are in workspaceltEntergt
- gtgt a, b, c, d ltEntergt

Plot y versus x

- Introduction to plotting displaying data
- gtgt clear clc ltEntergt
- gtgt x 00.11 ltEntergt
- gtgt y x.2 ltEntergt
- gtgt whos ltEntergt
- gtgt plot(x,y,x,y,o) ltEntergt
- gtgt disp(' '),disp('...... x ........ y

.....'),disp(x y') ltEntergt - gtgt x ltEntergt
- gtgt y ltEntergt
- gtgt x and y are 1-by-11 arrays of numbers!

Write a Simple Program

- Consider computing the volume of a cone
- Volume (pi.r.2.h)./3
- radius 6 inches
- height 12 inches
- In the command window key in
- gtgt clear clc ltEntergt
- gtgt r 6 ltEntergt
- gtgt h 12 ltEntergt
- gtgt v (pi.r.2.h)./3 ltEntergt
- gtgt whos ltEntergt

Editor M-Files

- An M-file in MATLAB is analogous to a txt-file in

Microsoft Notepad. - An M-file is created in MATLAB text editor.
- M-files
- You can save your programs (i.e., list of

executable commands) as M-files. - You can reopen and modify your program.
- They are useful for debugging (correcting

errors) as you develop your programs (your

technical computing tools).

Comments in programs

- Every time you write a program to be saved, it is

helpful for you to comment (i.e., describe) it

well. - To insert a comment on a line in the editor or in

the Command Window, use the comment operator ,

then type your comment. - MATLAB
- will not run lines that begin with the comment

operator (in the editor comments appear in

green). - Comments
- Comments allow you (and others) to more easily

understand your program. - When your lines of code are easy to understand,

your code will be easier to use later.

Art of well-written code

- A well-written program is like literature it

contains comments explaining - what your program requires as input.
- what the variables in the program represent.
- what your program computes and displays.
- It is useful for you to add a set of header

comments that include the name of the program,

your name (as the programmer), and the date the

program was created or modified.

Saving code in an M-File

- Open the editor by
- Entering the command edit in the command window.
- Or click the white-sheet-of-paper icon in the

upper left hand corner directly below file. - Now enter the lines of code to find the volume of

a cone - rr 4
- h 12
- v (pi.r.2.h)./3
- REMARK If you save it, add header comments and

comments explaining what the program does. - After you have typed in the code, save it as

cone.m.

This is cone.m in the editor

Tool to compute the volume of a cone. A

simple sample for a first lecture. B.H.

Daniel........... January 2007 rr 4

radius of the cone h 12 height of the cone v

(pi.r.2.h)./3 Volume of the cone

Execute an M-file as a Command

- Now execute (or run) the program by pushing F5,

or by typing on the command line - gtgt cone ltEntergt
- or by clicking the run button. (Note that the

run button looks like a page with a down arrow to

its left. It can be found below help on the

toolbar of the edit window.) - If you entered the code as written on the

previous slide you will get an error! - What went wrong?
- Repair your program (Change rr 4 to r 4.),

save it, and run it again. - Now change the height to 24, save and run your

program again.

Summary

- MATLAB can be used like a hand calculator to do

arithmetic. - You can define (or assign) variables with numbers

and expressions to do calculations as illustrated

by the volume-of-cone example. - The advantage of saving programs as M-files is

that you open it, make changes and/or execute it

again without having to type it all over again. - This concludes our overview of MATLAB and a taste

of things to come!