E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS

Description:

Edit a line using the left- and right-arrow keys the Backspace ... The left-hand-side needs to be a single variable name. Variables and the assignment operator ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 22
Provided by: pack3
Category:

less

Transcript and Presenter's Notes

Title: E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS


1
E77 INTRODUCTION TO COMPUTER PROGRAMMING FOR
SCIENTISTS AND ENGINEERS
  • Lecture Outline
  • Matlab basics
  • 2. Matlab as a calculator
  • 3. Saving and loading files in matlab

2
Matlab basis
  • The default MATLAB Desktop

3
Matlab basis
  • Starting Matlab
  • double-click the Matlab icon, or
  • in a terminal window, type matlab, and return
  • The Command Window and the
  • Matlab prompt gtgt

4
Matlab basis
  • The Desktop menu
  • undocking command window
  • tiling other windows (command history, workspace,
    current directory, profiler)
  • The File menu
  • Preferences
  • The Help menu

5
Matlab as a calculator
  • Type expressions at the gtgt, and press return
  • Result is computed, and displayed as ans
  • Use numbers, , , /, -, (), sin, cos, exp, abs,
    round,

6
Entering Commands and Expressions
  • MATLAB retains your previous keystrokes.
  • Use the up-arrow key to scroll back back through
    the commands.
  • Press the key once to see the previous entry, and
    so on.
  • Use the down-arrow key to scroll forward. Edit a
    line using the left- and right-arrow keys the
    Backspace key, and the Delete key.
  • Press the Enter key to execute the command.

7
Scalar Arithmetic Operations
Symbol Operation MATLAB form
exponentiation ab ab multiplication
ab ab / right division a/b a/b \ left
division b/a a\b addition a b a
b - subtraction a - b a - b
8
Order of Precedence
Precedence Operation
First Parentheses, evaluated starting with the
innermost pair. Second Exponentiation,
evaluated from left to right. Third Multiplicatio
n and division with equal precedence, evaluated
from left to right. Fourth Addition and
subtraction with equal precedence, evaluated
from left to right.
9
Examples of Precedence
gtgt 8 35 ans 23 gtgt 8 (35) ans
23 gtgt(8 3)5 ans 55
gtgt 4212 8/42 ans 0 gtgt (42)12
(8/4)2 ans 0 gtgt 4212 8/(42) ans
3
10
Special Variables and Constants
Symbol Description
ans most recent answer. eps Specifies the
accuracy of floating point precision. i,j The
imaginary unit Ö-1. Inf Infinity. NaN Indicates
an undefined numerical result. pi The number p.
11
Variables and the assignment operator ()
  • Use names to assign result of an expression to a
    variable
  • i.e. assign the value 3 to the variable x
  • Variables do not need to be declared before
    assignment
  • The right-hand-side needs to be a legal Matlab
    expression
  • The left-hand-side needs to be a single variable
    name

12
Examples of Variables and Assignment
means
gtgt A sqrt(4) A 2
gtgt C tan(pi/4) A C 3
means
(variable A must have been defined)
13
Examples of Variables and Assignment
  • What is the answer to

gtgt A sqrt(4) gtgt A tan(pi/4) A
gtgt A 3
  • A semicolon at the end of the RHS expression
    suppresses the display.
  • However, the assignment still takes place.

14
The workspace
  • All variables that you create are accessible from
    the prompt gtgt
  • Variables are accessed using their name as a
    reference
  • Builtin Matlab commands
  • gtgt who
  • gtgt whos
  • are used to see what is in the workspace.
  • You can clear (i.e., erase) variables with the
    clear command
  • gtgt clear A
  • clears the variable A from the workspace.

15
The workspace
  • All variables that you create are accessible from
    the prompt gtgt
  • Variables are accessed using their name as a
    reference
  • Builtin Matlab commands
  • gtgt who
  • gtgt whos
  • are used to see what is in the workspace.
  • gtgt clear all
  • clears ALL the variables from the workspace.

16
Quiting Matlab
  • Type quit at the prompt, or
  • Select Exit Matlab from the File menu
  • However

17
Saving the workspace
  • When you quit Matlab, the variables in the
    workspace are erased from memory.
  • If you need them for later use, you must save
    them.
  • gtgt save
  • saves all of the variables in the workspace into
    a file called matlab.mat (it is saved in the
    current directory)

18
Saving the workspace
  • gtgt save Roberto
  • saves all of the variables in the workspace into
    a file called Roberto.mat
  • gtgt save Important A B C D
  • saves the variables A, B, C and any variable
    beginning with D into a file called Important.mat

19
Loading from a .mat file
  • load is the opposite of save.
  • It reads a .mat file, putting all variables
    contained in the .mat file into the workspace
  • gtgt load
  • loads all of the variables from the file
    matlab.mat

20
Loading from a .mat file
  • gtgt load Roberto
  • loads all of the variables from the file
    Roberto.mat
  • There are no known security problems with load.
  • Hence, you can safely send (as attachment),
    receive and use .mat files from others.

21
Summary
  • MATLAB can be used as a high-end calculator
  • variables can be defined and manipulated.
  • Precedence left to right and , / ,
  • When in doubt for precedence, use parentheses!
  • Variables can be cleared, saveed and loaded.
  • MATLAB has special symbols e.g. pi , ANS , j ,
    etc.
Write a Comment
User Comments (0)
About PowerShow.com