Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
54 views

Matlab Intro

The document provides an introduction to basic MATLAB topics including starting MATLAB, matrices and arrays, graphics, symbolic math toolbox, control flow, functions, and linear algebra functions. It describes the MATLAB interface, command window, command history. It provides examples of simple calculations, special variables, common mathematical functions, getting help, creating m-files, running m-files. It also discusses matrices and arrays, operations, descriptive statistics, polynomials, linear algebra functions, plotting, and changing plot appearance.

Uploaded by

Mohan Babu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Matlab Intro

The document provides an introduction to basic MATLAB topics including starting MATLAB, matrices and arrays, graphics, symbolic math toolbox, control flow, functions, and linear algebra functions. It describes the MATLAB interface, command window, command history. It provides examples of simple calculations, special variables, common mathematical functions, getting help, creating m-files, running m-files. It also discusses matrices and arrays, operations, descriptive statistics, polynomials, linear algebra functions, plotting, and changing plot appearance.

Uploaded by

Mohan Babu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Out Line

This introduction will gives


• Starting a MATLAB
• Matrices and Arrays
• Graphics
• Symbolic Math Toolbox
• Control Flow and if Statement
• Functions
1
Basic MATLAB Interface

Command window:
Type your
instructions here
and press ENTER to
execute them.

2
Basic MATLAB Interface

Command history: a list


of instructions executed
by MATLAB is shown
here.

3
Simple calculations

4
Some special variables and constants are:

ans Most recent answer.


eps Floating point relative accuracy.
pi π
inf ∞
NaN Not a number.

5
Some Common Mathematical Function:

abs(x) Absolute value sqrt(x) Square root

sin(x) Sine asin(x) Inverse sine

cos(x) Cosine acos(x) Inverse cosine

tan(x) Tangent atan(x) Inverse tangent

log(x) Natural logarithm exp(x) Exponential

log10(x) Base 10 logarithm sign(x) Signum function

6
To get help

If you want to see help, you can type


help at the command window.
Or click ?

7
Click here

8
Example: search for
function mean

9
Create an m-file

To create an m-file,
1) type edit at the command
window, or
2) Press this button.

10
Create an m-file

The previous command will


display the editor window.
The editor creates an m-file
that can be used to write
your MATLAB programs.

11
Create an m-file

A lines starting with % --- is a comments

12
Save an m-file Note the file name will be changed …

To save the file press

13
Run an m-file

To execute a program, press


the RUN button.

14
Run an m-file

This window will appear. Press the


Change Directory button.

15
Run an m-file

You can see that the program


has created new variables in
the Workspace.
16
Matrices and
Arrays
Simple Arrays
>> V = [1 3, sqrt(5)]
V=
1.0000 3.0000 2.2361

>> A = [1 2 3;4 5 6;7 8 0]


A=
1 2 3
4 5 6
7 8 0

18
Arrays operators
+ Addition
- Subtraction
* Multiplication
*. Element-by-element multiplication
/ division
/. Element-by-element division
\ left division
\. Element-by-element left division
^ power
^. Element-by-element power
' array transpose
'. Unconjugated array transpose
19
Arrays operators Example
A = [1 2 3;4 5 6;7 8 0]
A=
1 2 3
4 5 6
7 8 0

A*A = A.*A =
30 36 15 1 4 9
66 81 42 16 25 36
39 54 69 49 64 0

20
Special Matrices and Vectors

Function Description
zeros All zeros
ones All ones
rand Uniformly distributed random elements
randne Normally distributed random elements
Eye(n) Returns the n-by-n identity matrix.

Z = zeros(2,4) F = 5*ones(3,3)
Z = F =
0 0 0 0 5 5 5
0 0 0 0 5 5 5
5 5 5

21
Basic Operations and Descriptive Statistics

Function Description
brush Interactively mark, delete, modify, and save observations
in graphs
cumprod Cumulative product
cumsum Cumulative sum
linkdata Automatically update graphs when variables change
prod Product of array elements
sort Sort array elements in ascending or descending order
sortrows Sort rows in ascending order
sum Sum of array elements
corrcoef Correlation coefficients
max Largest elements in array
mean Average or mean value of array
median Median value of array

22
Basic Operations and Descriptive Statistics

Function Description
cov Covariance matrix
min Smallest elements in array
mode Most frequent values in array
std Standard deviation
var Variance

23
Example
A = [1 2 3; 3 3 6; 4 6 8; 4 7 7]; B = [2,7,8,3];
prod(B)
ans =
336
 
mean(A) % mean of each column
ans =
3.0000 4.5000 6.0000  

mean(A,2) % mean of each row


ans =
2
4
6
6

24
Polynomials

Representing Polynomials

Consider the equation p( x)  x 3  2 x  5


To enter this polynomial into MATLAB, use

p = [1 0 -2 -5];
 

25
Polynomial Functions

Function Description
conv Multiply polynomials
deconv Divide polynomials
poly Polynomial with specified roots
polyder Polynomial derivative
polyfit Polynomial curve fitting
polyval Polynomial evaluation
polyvalm Matrix polynomial evaluation
residue Partial-fraction expansion (residues)
roots Find polynomial roots

26
Examples

To evaluate p at x=5, use


 
polyval(p,5)
ans =
110

X = [2 4 5; -1 0 3; 7 1 5];
Y = polyvalm(p,X)
Y=
377 179 439
111 81 136
490 253 639

27
Examples
Roots of Polynomials
To find the roots p:
r = roots(p)
 
r=
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
 
To find a polynomial that have the roots r:
p2 = poly(r)
 
p2 =
1.0000 -0.0000 -2.0000 -5.0000
28
Examples
Derivatives
To obtain the derivative of the polynomial p = [1 0 -2 -5];
q = polyder(p)
q=
3 0 -2

Polynomial curve fitting


p = polyfit(x,y,n) finds the coefficients of a polynomial
p(x) of degree n that fits the data, x(i) , y(i)

x = 0: 0.1: 2.5; y = erf(x);


p = polyfit(x,y,4)

p=
0.0238 -0.0262 -0.4343 1.2840 -0.0096 29
Linear Algebra Functions
Function Description Function 2 Description 3

norm Matrix or vector norm inv Matrix inverse


rank Matrix rank subspace Angle between two subspaces
det Determinant linsolve Solve a system of linear
equations
trace Sum of diagonal lu LU factorization
elements
null Null space chol Cholesky factorization

orth Orthogonalization eig Eigenvalues and eigenvectors

rref Reduced row echelon expm Matrix exponential


form
pinv Pseudoinverse funm Evaluate general matrix
function
30
Examples

1  2 3
A  4 0 6
2  1 3 U=

A=[1 -2 3;4 0 6; 2 -1,3] 4.0000 0 6.0000


[L,U,P] = lu(A) 0 -2.0000 1.5000
0 0 -0.7500
A=
1 -2 3 P=
4 0 6 0 1 0
2 -1 3 1 0 0
0 0 1
L=
1.0000 0 0
0.2500 1.0000 0
0.5000 0.5000 1.0000 31
Linear system of equations
The system
  1 0 3  x1   5 
0 5 6  x     2
  2   
7 8 0  x3   3 
can be solved as:
A = [1 0 3;0 5 6;7 8 0];b = [5;-2;3]; Or
y=linsolve(A,b)
x = inv(A)*b
y=
x=
2.1765
2.1765
-1.5294
-1.5294
0.9412
0.9412

32
Plot
Two-Dimensional Graphics
Suppose we want to graph the function y = 5x3 over the
x domain -1 to 1 .

x = -1:0.1:1;
y = 5*x.^3;
plot(x,y)

34
Changing the Appearance
Symbo Color Symbol Marker Symbol Line style
l 2 3
b blue . point - solid line
g green o circle : dotted lin
r red x x-mark .- dash-dot line
c cyan + plus -- dashed line
m magenta * star    
y yellow s square    
k black d diamond    
w white ^ triangle    
    < triangle left    
    > triangle right    
    P pentagram    
    h hexagram    

35
To add a title and axis labels write

36
Three-Dimensional Graphics
1. Line plotes
The plot3 function displays a three-dimensional plot of a set of
data points. Here is an example of a three-dimensional helix:

37
Operators
Often we need some relational or logical operators to companion if
statement. Operators are shown in the following table:

Relational Operators Logical Operators

< Less than && Logical AND


=< Less than or equal || Logical OR
to
> Greater than & Logical AND for arrays
=> Greater than or | Logical OR for arrays
equal to
== Equal to ~ Logical NOT
=~ Not equal to    

38
for loop

for loop allow a group of commands to be repeated a fixed,


predetermined number of times. For example:

for i = 1:4
x(i) = i^2
end
 
for k = 2:5:20, y = k^3 - 7, end

for x = [2 0 3], y = x^3 - 5*x, end

39
while loop

A while loop evaluates a group of statements an indefinite


number of times such as

c = 0; i=1;
while c==0
i=i+2
s=1/i
if s<=0.1 c=1
end
end

40
Bisection Method Example
Here is a complete program, illustrating while, if, else, and end,
which uses interval bisection to find a zero of a polynomial:

a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if fx == 0
break
elseif sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
x 41
Functions
You will often need to build your own MATLAB functions
as you use MATLAB to solve problems.


Inline •
function_handle (@)

g = inline('t^2') sqr = @(x) x.^2

g(3) Sqr(5)

f = inline('sin(alpha*x)') sqrpy = @(x,y) x.^2+y

f(3,pi/2) srtpy(2,1)

function [out1, out2, ...] = funname(in1, in2, ...)

43
Functions

This function calculates the mean and standard deviation of a vector:

function [mean,stdev] = stat(x)


n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));

[mean stdev] = stat( [12.7 45.4 98.9 26.6 53/1] )


mean =
47.3200
stdev =
29.4085

44
ODE function

Defining an ODE function in an M-file

45
... Solving first-order ODEs

Example

46
47
48

You might also like