Download presentation
Presentation is loading. Please wait.
Published bySharon Barber Modified over 9 years ago
1
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally straight 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.
2
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>>
3
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 >>
4
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.
5
Ch13-5 Program output can be suppressed by simply appending a semicolon (;) to command lines, as in >>x=3; >>x x= 3 >>
6
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
7
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
8
Ch13-8 13.2.3 Numerical Precision of MATLAB Output All arithmetic is done to double precision, which for 32-bit machines means to about 16 decimal digits of accuracy. MATLAB automatically prints integer values as integers and floating points to four decimal digits of accuracy.
9
Ch13-9 13.2.4 Built-In Mathematical Functions MATLAB has a platter if built-in functions for mathematical and scientific computations. >>x=pi/3; >>sin(x)^2 + cos(x)^2-1.0 ans = 0 >> Active Variables: When you want to know the active variables, you can use who. >>who Your variables are: ansx >>whos >>clear x
10
Ch13-10
11
Ch13-11
12
Ch13-12 13.2.5 Program Input and Output MATLAB has two functions for the basic input of variables from the 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 : fprintfC : printf
13
Ch13-13 13.4 Control Structures Control of flow in the MATLAB : logical expressions branching constructs looping constructs 13.4.1 Logical Expressions >>3 < 5 ans= 1 >>a = 3 = = 5 a= 0 >>
14
Ch13-14
15
Ch13-15 13.4.2 Selection Constructs >>a=2;b=1; >>if a<b, c=3; else c=4; end; >>c c= 4 >>
16
Ch13-16 13.4.3 Looping Constructs >> for I=1:21, c=2*i end >>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.