ch13-1 chap 13 introduction to matlab 13.1 introduction matlab : the matrix laboratory program not...

16
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language except ionally straight forward, but also MATLAB program code will be much shorter and simpler than an equi valent implementation in C,FORTRAN, or Java. MATLAB is an ideal language for creating prototype s of software solutions to engineering programs, a nd using them to validate ideas and refine project specifications. Once these issues have been worked out, the MATLAB implementation can be replaced by a C or Java impl ementation that enhances performance and allows fo r extra functionality. Currently, MATLAB is a commercial matrix laborator y package that operates as an interactive programm ing environment with graphical output.

Upload: sharon-barber

Post on 18-Jan-2016

235 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-1

Chap 13 Introduction to Matlab13.1 Introduction

MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally st

raight forward, but also MATLAB program code will be much shorter and simpler than an equivalent implementation in C,FORTRAN, or Java.

MATLAB is an ideal language for creating prototypes of software solutions to engineering programs, and using them to validate ideas and refine project specifications.

Once these issues have been worked out, the MATLAB implementation can be replaced by a C or Java implementation that enhances performance and allows for extra functionality.

Currently, MATLAB is a commercial matrix laboratory package that operates as an interactive programming environment with graphical output.

Page 2: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-2

13.1.1 Professional and Student

Versions of MATLAB Student edition of MATLAB is limited in:

Each matrix is limited in 16,384 elements. ( i.e., 128*128 matrix)

13.1.2 Entering and Leaving MATLAB A MATLAB session may be entered by simply typing

prompt >>matlab

>>

>>quit

click on the icon for MATLAB or STUDENT-MATLAB

EDU>>

Page 3: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-3

13.1.3 Online Help >>help

>>help demo

>>helpdesk

13.2 Variables and variable Arithmetic The name and (data) types of MATLAB variables do not need to be declared

because MATLAB does not distinguish between integer, real, and complex values.

Any variable can take integer, real, or complex values.

13.2.1 Defining Variables

>>x=3

x=

3

>>

Page 4: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-4

Variables names :

(1)a mixture of letters,digits,and underscore character.

(2)the first character must be a letter.

(3)within 31 characters.

(4)variables x and X are distinct.

Page 5: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-5

Program output can be suppressed by simply appending a semicolon (;) to command lines, as in

>>x=3;

>>x

x=

3

>>

Page 6: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-6

More then one command may be entered on a single line if the commands are separated by commas or semicolons.

>>x=3,y=4; z=5 %define and initialize variables x,y, and z.

13.2.2 Arithmetic Expressions

Page 7: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-7

>>2+3; %Compute the sum “2” plus “3”

>>3*4; %Compute the product “3” times “4”

>>4^2; %Compute “4” raised to the power of “2”

>>2+3*4^2;

>>(12+3*4^2)/2;

Ex:

>>4.0*sin(pi/4+pi/4)

is

Page 8: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-8

13.2.3 Numerical Precision of MATLAB Output All arithmetic is done to double precision, which for 32-bit ma

chines means to about 16 decimal digits of accuracy. MATLAB automatically prints integer values as integers and fl

oating points to four decimal digits of accuracy.

Page 9: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-9

13.2.4 Built-In Mathematical Functions MATLAB has a platter if built-in functions for mathematical a

nd scientific computations.>>x=pi/3;>>sin(x)^2 + cos(x)^2-1.0ans =

0>>

Active Variables:When you want to know the active variables, you can use who. >>who Your variables are:

ans x>>whos>>clear x

Page 10: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-10

Page 11: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-11

Page 12: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-12

13.2.5 Program Input and Output MATLAB has two functions for the basic input of variables from t

he keyboard and for formatted output of variables.Input of Variables from Keyboard: A=input(‘Please type the value of coefficient A:’);will print the message Please type the value of coefficient A:Formatted Output of Variables:fprintf(format,matrices or variables)

Ex:>>fprintf(‘Volume of sphere=%f\n’,3/4)

Volume of sphere=3.400000>>

Note: (1) Matlab’s use of single quotes and C’s use of double quotes. (2) Matlab : fprintf C : printf

Page 13: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-13

13.4 Control Structures Control of flow in the MATLAB :

logical expressionsbranching constructs looping constructs

13.4.1 Logical Expressions >>3 < 5

ans= 1

>>a = 3 = = 5 a=

0>>

Page 14: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-14

Page 15: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-15

13.4.2 Selection Constructs

>>a=2;b=1;>>if a<b, c=3; else c=4; end;>>c c=

4>>

Page 16: Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally

Ch13-16

13.4.3 Looping Constructs

>> for I=1:21,

c=2*i

end

>>