1

 Goals
 Learn basic concepts
 Learn how to use MATLAB as a
 calculator

2

 MATLAB basics
 the Command Window
 online Help
 numbers, variables, operators, functions
 the Workspace Browser
 Using MATLAB as a calculator
 Drawings 15 & 16 due today

3

 Basic Programming Skills
 Variables
 Arrays
 Plotting and graphing
 Script Mfiles
 Function Mfiles
 Selection structures: if/else, switch
 Repetition structures: for loop, while loop
 Input/Output

4

 Widely used computational software in engineering includes: spreadsheets
(e.g., Excel), MATLAB, Mathcad, Mathematica, Maple, C/C++, Visual Basic,
Fortran, etc.
 Each tool has advantages and limitations.
 Choosing the best computational tool for a particular task is key to
working efficiently and productively.

5

 as a calculator
 to prepare sophisticated graphics
 to write and execute programs, ranging from “simple” programs to
complete software applications, including a GUI (“graphical user
interface”)

6


7


8


9

 Variable names are casesensitive and can be no longer than 31
characters in length.
 No type declaration or dimension statements are required.

10

 In other programming languages, you need to specify the type of
variables you are going to use, for example:
 Integer, floating point
 Character
 Array et.

11

 In MATLAB you don’t need to make any type declarations or dimension
statements.
 When MATLAB encounters a new variable, it automatically creates the
variable and allocates the appropriate amount of memory.

12

 Variable naming rule:
 Variable names consist of a letter, followed by any number of letters,
digits, or underscores.
 MATLAB is case sensitive
 A and a are not the same variable

13


14

 Try the following in MATLAB:
 ^{>> }x = 32;
 ^{>> }y = 5 * 12 + 40;
 ^{>> }z = x / y
(do not end line with ; and note effect of ;)
 ^{>> }z = 55 + x
(note the reassignment of the value of z)
 ^{>> }x (the
value of a variable can be seen by entering its name)
 ^{>> }d = 0.5 * y^0.5

15


16

 sin(x): returns sine value of x
 cos(x): returns cosine value of x
 log(x): returns natural log value of x
 log10(x): returns base 10 log value of x
 sqrt(x): returns square root value of x
 abs(x): returns absolute value of x

17

 Some functions simply return a constant and need no input argument:
 while other functions require an “argument”:

18

 Try the following:
 ^{>> }x = pi
 ^{>> }y = sin (x)
 ^{>> }z = exp(1)
 ^{>> }w = log10 (100)/2.5
(base 10 log function is log10)
 ^{>> }v = log (z) (natural log function
is log)

19


20


21

 disp(x)
 X is a string, the text is displayed
 input()
 These two functions are combined to display input and output

22

 Ask the user to input two numbers and subtract the first number from the
second number, display the result.
 disp(‘please input the first number’)
 x1=input()
 disp(‘please input the second number’)
 x2=input()
 x3=x2x1
 disp(the resule is:)
 disp(x3)

23

 Follow the same format, solve the following calculations
 x1=2+2i
 x2=1+3i
 Ask to calculate
 Y1=x1+x2
 Y2=x1*x2
 Y3=x1/x2
 Display the corresponding results

24

 Follow the same format, solve the following calculations
 x1=2
 x2=1.5*pi
 Ask to calculate
 Y1=log(x1)+sin(x2)
 Y2=sin(x1)/sin(x2)
 Y3=x1^x2
 Display the corresponding results

25

 First tip: don’t just read the book, do some exercises.
 You can read 1000 programming books, but if you don’t do any exercises,
you can’t code, period.
 Remember how you learned to speak English?

26

 Second tip: read the book
 This seems to conflict the first tip, but when you try to solve some
programming problems, you could (and believe me, you will) hit a brick wall.
 Read the book with the question in your mind, look for answers in the
book.

27

 Tip 3: read the other’s source codes.
 Assuming these “others” are good programmers.
 You will learn a lot by reading how the others do programming.
 Could be a painful process, but worth it.

28

 Tip 4: consult experienced programmers.
 Pro: Get your problem solved quickly.
 Con: Experienced programmers sometimes are grumpy monkeys. So bag your
pride b4 you ask for help.

29

 Tip 5: ask for help from your TAs.
 Your TAs all once struggled with learning how to program.

30

 And remember:
 There are no dumb questions. You don’t look bad by asking questions.

31

 From the Matlab text
 Problem 21 Section 2 (pg.27)
 Copy and paste your command window into Word or another text file to
turn in. You can also save this
on a disk. The first command in
the window should be
 Name = ‘Your name, Assignment no _.’
 Also mark your final answer clearly by circling or highlighting it.
 READING Assignment:
 Matlab text Chapter 2, Sections 2.2 & 2.3
