Introduction to MATLAB

Download Report

Transcript Introduction to MATLAB

Introduction to MATLAB
MECH 300H
Spring 2008
Starting of MATLAB
Starting of MATLAB
• MATLAB file operations use the current directory and the search path as
reference points. Any file you want to run must either be in the current
directory or on the search path
Arithmetic Operators
MATLAB works according to the priorities:
1. quantities in brackets
2. powers, 2 + 3^2 => 2 + 9 = 11
3. * /, working left to right (3*4/5=12/5)
4. + -, working left to right (3+4-5=7-5)
Number and format
• The ‘e’ notation is used for very large or very small numbers:
• The constant ‘π’ is represented by the command ‘pi’
• All computations in MATLAB are done in double precision, which
means about 15 significant figures. The format how MATLAB prints
numbers is controlled by the ‘format’ command
Common built-in function
Suppressing input
• One often does not want to see the result of intermediate calculations
terminate the assignment statement or expression with semicolon, ‘;’
• The value of x is hidden. Note also we can place several statements
on one line, separated by commas or semicolons.
M-files
• In writing programs, it is suggested to write it in the editor and save it
in a file named, abc.m, instead of writing in the command window
• The Editor/Debugger provides a graphical user interface for text
editing, as well as for M-file debugging. To create or edit an M-file
use File -> New or File -> Open, or use the edit function
Matrix
To entry a matrix:
• Separate the elements of a row with blanks or commas
• Use a semicolon ; to indicate the end of each row
• Surround the entire list of elements with square brackets, [ ]
For example:
A is created and is a 3x3 matrix
16 5 4
A   7 8 9 
 2 3 4
Matrix
To extract a element from a matrix or to insert a element to a matrix:
• recall the name of matrix
• insert the row and column number into a bracket
• A ( i, j ) – i and j represent row and column no. respectively
For example,
16 5 4


A   7 8 9
 2 3 4
The Colon Operator
• Colon ‘:’ generate vector, say x, from n to m with unit increment
• Use increment other than one, say pi/4
Plotting graph
The default is to plot solid lines. A solid white line is produced by
To put a title and label the axes, we use
Plotting graph
Example:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
First curve
Second curve
-0.6
-0.8
-1
To edit the range of axis:
On the figure window
EDIT Axes properties
0
0.5
1
1.5
2
2.5
3
3.5
Export Figures
In the figure window, one can save the current figure in
many different formats, such as “tiff”, “jpg”, “eps”,”fig”
MATLAB only recognizes “.fig” format.
Flow control statement
• The flow control statement used in
MATLAB is similar to that in
C/C++ language
• For Loops - repeats a group of
statements a fixed, predetermined
number of times
• While Loops - repeats a group of
statements an indefinite number of
times under control of a logical
condition
• If -evaluates a logical expression
and executes a group of
statements when the expression is
true
Example for Loop
• Plot the function
y  sin(x) x  (0,2 )
• MATLAB code:
1
0.6
0.4
0.2
y
% N=the number of interval
N=100;
for i=1:N
x(i)=i/N*2*pi;
y(i)=sin(x(i));
end
plot(x,y,'*')
0.8
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
2
3
4
x
5
6
7
Function statement
• Function is the statements which can be used by other
programs or functions.
function [] = function_name ();
• Example
%The main program
N= 100;
dx = 10/N;
for i = 1:N
x = i * dx;
end
[f,n] = funeval(x);
plot(x,f)
%function
function [f,n] = funeval(x);
n = length(x);
f=sin(x).*exp(x)+ones(n,1);