Top Banner
Introduction to MATLAB 7 for Engineers William J. Palm III Chapter 1 An Overview of MATLAB
28

Introduction to MATLAB 7 for Engineers

Mar 19, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to MATLAB 7 for Engineers

Introduction to MATLAB 7 for Engineers

William J. Palm III

Chapter 1

An Overview of MATLAB

Page 2: Introduction to MATLAB 7 for Engineers
Page 3: Introduction to MATLAB 7 for Engineers

>> 8/10 % Note ans = 0.8000

>> 5*ans ans =

4

>> r=8/10 r = 0.8000

>> r r = 0.8000

>> s=20*r

s =

16

Note: The MATLAB use high precision for

its computations, but displays the

default short format.

Page 4: Introduction to MATLAB 7 for Engineers

Operation Symbol

a^b exponentiation: aª ^

a*b multiplication: ab *

a/b right division: a/b /

a\b left division: b/a \

a+b addition: a + b +

a-b subtraction: a -b -

Page 5: Introduction to MATLAB 7 for Engineers

Precedence operation:

First: Parentheses ( ), evaluated starting with the innermost pair.

Second: Exponentiation (power) ^, evaluated from left to right.

Third: Multiplication * and division / with equal precedence, evaluated from left to right.

Fourth: Addition + and subtraction - with equal precedence, evaluated from left to right.

Page 6: Introduction to MATLAB 7 for Engineers

>> 8 + 3*5

ans =23

>> 8 + (3*5)

ans =23

>>(8 + 3)*5

ans =55

>>4^2-128/(4*2)

ans =0

>>4^2-128/4*2

ans =-48

Page 7: Introduction to MATLAB 7 for Engineers

3*4^2+5 ans =

53

(3*4)^2 +5 ans =

4101

27^(1/3) + 32 ^(0.2) ans = 5

27 ^(1/3) + 32^0.2 ans = 5

27 ^1/3 + 32^0.2 ans =

11

Page 8: Introduction to MATLAB 7 for Engineers

The term workspace refers to the names and values of any variables in

use in the current work session. Variable names must begin with a

letter; the rest of the name can contain

letters, digits, and underscore characters.

MATLAB is casesensitive.

Thus the following names represent different variables: speed,

Speed, SPEED, Speed_1, and Speed_2. In MATLAB 7, variable

names

can be no longer than 63 characters.

Page 9: Introduction to MATLAB 7 for Engineers

Typing x = 3 assigns the value 3 to the variable x.

We can then type x= x+2. This assigns the value 3+2= 5

to x. But in algebra this implies that 0 = 2 ?!!!.

In algebra we can write x+2=20, but in MATLAB we

cannot?!!!.

Page 10: Introduction to MATLAB 7 for Engineers

Command Description

clc : Clears the Command window.

clear : Removes all variables from memory.

clear v1 v2 : Removes the variables v1 and v2 from memory.

exist (‘var’): Determines if a file or variable exists having the name ‘var’.

quit or exit : Stops MATLAB.

Page 11: Introduction to MATLAB 7 for Engineers

who : Lists the variables currently in memory .

whos : Lists the current variables and sizes, and indicates if they have imaginary parts.

: colon ; generates an array having regularly spaced elements .

, Comma; separates elements of an array.

; Semicolon; suppresses screen printing; also denotes a new row in an array.

Page 12: Introduction to MATLAB 7 for Engineers

Command Description

ans Temporary variable containing the most

recent answer.

i,j The imaginary unit√−1, ( ∠90°).

Inf Infinity ( ∞) (example: 7/0).

NaN Indicates an undefined numerical result

(Not a Number), (example: 0/0).

pi The number π =3.141592653589793...

Page 13: Introduction to MATLAB 7 for Engineers

• The number c1= 1 –2i is entered as follows:

>> c1 = 1-2i % or c1 = 1-2j

• An asterisk (*) is not needed between I or j and a number, although it is required with a variable, such as:

>>c2 = 5 - i*c1

• Be careful. The expressions

>> y = 7/2*i

and

>> x = 7/2i

give two different results: y = (7/2)i = 3.5i

and x = 7/(2i) = –3.5i.

Page 14: Introduction to MATLAB 7 for Engineers

The volume of a circular cylinder of height h and radius r

is given by . A particular cylindrical tank is

15 m tall and has a radius of 8 m. We want to construct

another cylindrical tank with a volume 20 percent

greater but having the same height. How large must its

radius be?

Page 15: Introduction to MATLAB 7 for Engineers
Page 16: Introduction to MATLAB 7 for Engineers

Arrays

The numbers 0, 0.1, 0.2, …, 10 can be assigned to the variable u

by typing

>> u = [0:0.1:10];

To compute w= 5 sin u for u= 0, 0.1, 0.2, …, 10,

the session is;

>>u = [0:0.1:10];

>>w = 5*sin(u);

The single line, w = 5*sin(u),computed the formula

w = 5 sin u; 101 times.

(continued …)

Page 17: Introduction to MATLAB 7 for Engineers

>> u(7)

ans =

0.6000

>> w(7)

ans =

2.8232

• Use the length function to determine how many values are in an

array.

>>m = length(w)

m =

101

Page 18: Introduction to MATLAB 7 for Engineers
Page 19: Introduction to MATLAB 7 for Engineers
Page 20: Introduction to MATLAB 7 for Engineers
Page 21: Introduction to MATLAB 7 for Engineers

Command Description

plot(x,y) Generates a plot of the array y versus

the array x on rectilinear axes.

title(’text’) Puts text in a title at the top of the plot.

xlabel(’text’)

ylabel(’text’)

Adds a text label to the horizontal

axis (the abscissa).

Adds a text label to the vertical axis

(the ordinate).

Page 22: Introduction to MATLAB 7 for Engineers

Command Description

[x , y ]= ginput(n) Enables the mouse to get n points from

a plot, and returns the x and y

coordinates in the vectors x and y,

which have a length n.

grid Puts grid lines on the plot.

gtext(’text’) Enables placement of text with the

mouse.

(continued …)

Page 23: Introduction to MATLAB 7 for Engineers

Q1. Suppose that x = 2 & y = 4.

Write MATLAB command(s) to compute the following. Then write the answer. (Ask the instructor to select 1&3 or 2&4 only).

Q2. Write down a simple MATLAB command(s) to plot

Y= 5 sin2πt function.

Page 24: Introduction to MATLAB 7 for Engineers

Equations as a single matrix equation. For example,

consider the following set:

This set can be expressed in vector-matrix form as

which can be represented in the following compact form

Ax = b x = b / A

Page 25: Introduction to MATLAB 7 for Engineers

MATLAB provides the left-division method for solving the

equation set Ax = b. The left-division method is based on

Gauss elimination. To use the left-division method to solve for

x, type x = A\b.

For example,

>> A = [6, -10; 3, -4]; b = [2; 5];

>> x = A\b % (try b/A)?!!!

x =

7 4

Page 26: Introduction to MATLAB 7 for Engineers

The MATLAB command inv(A)computes the inverse of the matrix A. The following MATLAB session solves the following equations using MATLAB inv command.

>>A = [2,9;3,-4];

>> b = [5;7]

>> x = inv(A)*b

x =

2.3714

0.0286

Note : If you attempt to solve a singular problem using the inv

command, MATLAB displays an error message.

Page 27: Introduction to MATLAB 7 for Engineers

>>A = [6,12,4;7,-2,3;2,8,-9];

>>B = [70;5;64];

>> Solution = A\B %(try B/A)?!!! Solution =

3 5 -2

The solution is x= 3, y= 5, and z= –2.

Page 28: Introduction to MATLAB 7 for Engineers

1- In the interactive mode(similar to using a

calculator), in which all commands are entered

directly in the Command window.

2- By running a MATLAB program stored in script file.

This type of file contains MATLAB commands, so

running it is equivalent to typing all the commands

-one at a time -at the Command window prompt

.You can run the file by typing its name at the

Command window prompt.