Documente Academic
Documente Profesional
Documente Cultură
Third Edition
Tradition & Excellence since 1821 Mechanical Engineering
TOPIC
01
ENGR 680
Advanced Computational
Methods
Introduction to MATLAB
The clc • The clc Command clears the Command Window. It will not
Command change anything that was done before.
The help • The help Command displays the description of the command
Command with instructions.
help command_name
For Example,
help sin
SIN sine.
SIN(X) is the sine of the elements of X.
MATLAB Variables
Variable • A variable is a name made of a letter or combination of letters
(and digits), and is assigned a numerical value (for numerical
variable) or character string (for string variable) .
Define a • The assignment (replacement) operator, =, is used to assign a
Variable value to a variable:
variable_name = A value, or a computable expression
a = 1;
b = a^2 + 1 + sin(a);
name = 'John Doe';
d = input(‘Please input d'); (keyboard input)
• A variable must be defined (initialized) before it can be used.
some other programs may have a default initialization (e.g., 0),
but MATLAB does not.
• To assign an character string to a variable, the character string
should be enclosed in single quotes.
If the character string already contains the single-quote
character, that quote can be specified by two single quotes:
answer = 'I don''t know'
MATLAB Variables: The Assignment Operator, =
The • The assignment operator, =, works differently than the equals
Assignment sign (=) we know from mathematics.
Operator, = For a simple case like a=1, the usage is practically no
different than in mathematics. However, it really means to
assign a value 1 to the variable a to replace the original value
in a (if there is one).
We cannot switch both sides of the expression a=1 to have
1=a in MATLAB, which is OK in mathematics.
We can have something like a = a+1 in MATLAB, which
tells the computer to add 1 to the current value of a. However,
in mathematics, it is an invalid equation.
• Avoid using built-in functions, e.g., cos, sin, exp, sqrt, abs, …
The who • The who Command displays a list of variables currently in the
Command memory.
The whos • The whos Command displays a list of variables currently in the
Command memory and their size together with information about their bytes
and class.
The save save fname stores all variables in MATLAB Binary
Command format in the MAT-file fname.mat in
the current directory.
save fname x stores the variables x in MATLAB Binary
format the file fname.mat in the current
directory.
The load load fname loads all variables in the file
Command fname.mat into the workspace.
load fname x loads the variables x in the file
fname.mat into the workspace.
More on the save and load Commands
The MAT- • The save and load commands save and load data in a special
file compact format that preserves many details, such as the name and
type of the variable, etc.
• The MAT-file created by the save command on any platform can
be read on any other platform which runs MATLAB.
• However, the MAT-file is in a format that cannot be read by other
programs.
• If data must be shared with other programs, then the data must be
saved in ASCII format by adding -ascii option.
save fname.dat x -ascii
saves the variable x in the file fname.dat in ASCII format.
The extension of the file name must be specified.
Notes • If the data is saved in ASCII format, the special information, such
as the variable names and types, is lost.
• If data must be exchanged between MATLAB and other
programs, save the data in ASCII format. If the data will only be
used in MATLAB, save the data in MAT-file format.
Displaying Output Data
Default • MATLAB automatically displays the result of an expression
Format without a “;” in the Command Window using a default format as
follows:
Change • We may need more digits after the decimal point, or we may want
Format see the data in a different format.
• The default format can be changed using the format command.
A summary of the command is displayed on the next Slide
Display Output Data: Change Formats
Command Description Example (>>290/7)
format short Fixed-point with 4 decimal digits for 41.4286
0.001<=number<=1000
Otherwise display format short e
format Long Fixed-point with 14 decimal digits for 41.4285714285714
0.001<=number<=100
Otherwise display format long e
format short e scientific notation with 4 decimal digits 4.1429+e001
format long e scientific notation with 15 decimal digits 4.142857142857143+e001
format short g Best of 5-digit fixed or floating point 41.429
format long g Best of 15-digit fixed or floating point 41.4285714285714
format bank Two decimal digits 41.43
format compact Eliminates empty lines to allow more lines displayed on the screen
format loose Adds empty lines (opposite of compact)
Note: The displaying format does not affect how MATLAB computes and stores numbers
Displaying Output Data: Other Commands
The disp • disp(x) displays the array (matrix or vector), without printing
Command the array name.
The disp Command accepts only one argument, i.e., it can
only display one array (vector, matrix) at a time.
The disp Command only displays data on the screen.
The • fprintf can display one or more values with related text,
fprintf according to the user-defined format.
Command
The fprintf Command can display data on the screen or
write the data to a file.
The general form of the fprintf Command is
fprintf(fid,format,data,…)
fid: file identifier if the data is written to a file.
If fid is missing, the data will be displayed on
the screen.
format: format describing how the data is to be printed.
data: the data to be printed (displayed).
More on the fprintf Command: the format
Example • >> fprintf('The value of pi is %f\n',pi)
3. variable_name=linspace(x1,x2,n)
x1= first term, x2= last term, and n= number of elements, e.g.,
x=linspace(0,8,6)
x=
0 1.6000 3.2000 4.8000 6.4000 8.0000
4. Programming. (see Vector.m)
Create Two-Dimensional Arrays (Matrix Variables)
2D Arrays • A 2D has numbers placed in rows and columns – A Matrix
variable, e.g., 1 2 3
3 4 5
How to variable_name=[1st row;2nd row;…;last row] :
Create 2D
• x=[1 2 3;3 4 5]
Arrays
x=
1 2 3
3 4 5
• x=[1:2:7;linspace(10,40,4);67 2 45 8]
x =
1 3 5 7
10 20 30 40
67 2 45 8
Example x = -10:1:10;
y = x.^2;
plot(x,y,'--ro','linewidth',2,'markersize',12)
Two-Dimensional Plots: The fplot Command
fplot • The fplot command is used to create a given function:
fplot('function',[limits],'line specifiers')
Example fplot('x^2+4*sin(2*x)-1',[-3,3],'-b+')
Hold on hold on starts the process to add graphics to the previous plot.
Hold off hold off stops the above process.
Change position of
labels, legends and
other objects by
clicking on the object
and dragging.
An Example
Try Matlab_Command.m to test some basic MATLAB commands.
Simple if Clauses
General if expression
Form Statements
end
Analysis • If the logical expression is TRUE, the statements between the
if statement and the end statement are executed.
• If the logical expression is FALSE, program control jumps
immediately to the statement following the end statement.
count = 0, n = input('Please input n = ');
if n < 50
count = count + 1
end
Notes • The logical expression is generated from relational operators
(>, >=, <, <=, ==, ~=) and logical operators [~ (not), & (and), |
(or), xor (logical exclusive or)]
• It is a good programming practice to indent the statements within
an if structure, for readability.
• An if statement can have a nested if statement inside it.
if Clauses: The Relational and Logical Operators
Operators • Complicated tests can be performed using the logical operators in
conjunction with the relational operators.
General if expression
Form statement t1
statement t2
…
else
statement f1
statement f2
…
end
Analysis • When the program encounters the while statement for the first time,
the condition is tested:
If the condition is false (=0), MATLAB jumps to the statement
following the end statement and continues with the rest of the
program, if any.
If the condition is true (=1), the statements are executed,
and the program jumps back to the while statement and test the
condition again.
The loops repeats indefinitely until the condition becomes false.
Notes • If the condition never becomes false, an infinite loop results.
i = 10;
while i<=10
i = i – 1;
a=i^2;
end
While Loops: An Example
Example Calculate the sum s = 1 + 2 + 3 +…+ n
for loop n = input('Please input n:');
s = 0;
for i = 1:n
s = s + i;
end
fprintf('1+2+...+ %d = %d\n',n,s)
• Formatting plots
• More programming
• 3D plots
• Images, Movies, and sound
• Printing and Exporting Graphics
• Handle Graphics
• Graphical user Interface (GUI)
• …