Title: MATLAB Tutorial
1MATLAB Tutorial
- ECE 002
- Professor S. Ahmadi
2Tutorial Outline
- Functions using M-files.
- Numerical Integration using the Trapezoidal Rule.
- Example 1 Numerical Integration using Standard
Programming (Slow). - Example 2 Numerical Integration using
Vectorized MATLAB operations (Fast). - Student Lab Exercises.
3What Are Function M-Files
- Function M-files are user-defined subroutines
that can be invoked in order to perform specific
functions. - Input arguments are used to feed information to
the function. - Output arguments store the results.
- EX a sin(d).
- All variables within the function are local,
except outputs.NOTE Local variables are
variables that are only used within the function.
They are not saved after the M-file executes.
4Example of a Function M-File
- function answer average3(arg1,arg2, arg3)
- A simple example about how MATLAB deals with
user - created functions in M-files.
- average3 is a user-defined function. It could
be named anything. - average3 takes the average of the three input
parameters arg1, - arg2, arg3
- The output is stored in the variable answer and
returned to the - user.
- This M file must be saved as average3.m
- answer (arg1arg2arg3)/3
-
5How to call a function in matlab
- X sin(pi)
- call the interior function of Matlab, sin
- Y input(How are you?)
- call the interior function of Matlab, input
- input is a function that requests information
from - the user during runtime
- Yaverage3(1, 2, 3).
- average3 is a user-defined function.
- The average value of 1,2 and 3 will be stored
in Y -
6Example Calling a Function from within the Main
program.
- Example1 This is a main program example to
illustrate how functions are called. - input is a function that requests information
from - the user during runtime.
- VarA input('What is the first number?')
- VarB input('What is the second number?')
- VarC input('What is the third number?')
- VarAverageaverage3(VarA, VarB, VarC)
- num2str converts a number to a string.
- resultThe average value was,
num2str(VarAverage)
7Numerical Integration
- General Approximation Made With Num. Integration
- b n
- ? f(x) dx ? ci f(xi)
- a i0
- Trapezoidal rule
- Integration finds the Area under
- a curve, between two points (a and b).
- To approximate the area under a curve,
- use a familiar shape whose area we
- already know how to calculate easily.
- In this case, well use a trapezoid shape.
- Area of trapezoid ½ (b1 b2) h
f(b)
f(a)
base 2 (b1)
h
base 2 (b2)
8Numerical Integration
- Area under curve Area of trapezoid
- under the
curve - Area of trapezoid ½ h b1 b2
- Area under curve ½ (b-a) f(a) f(b)
- Therefore, Trapezoidal Rule
- b
- ? f(x) dx ½ (b-a) f(a) f(b)
- a
-
f(b)
f(a)
- How can we get a better approximation of the area
under the curve? - Answer Use More Trapezoids
9Numerical Integration
- More trapezoids give a better approximation of
the area under curve
- Add area of both trapezoids together,
- more precise approx. of area under curve
- Area of trapezoid ½ h b1 b2
- Area under curve
-
- ½ (x1-a) f(a) f(x1) ½ (b-x1) f(x1)
f(b) - Simplify
- ½ ((b-a)/2) f(a) f(x1) f(x1) f(b)
- (b-a) f(a) 2 f(x1) f(b)
f(b)
f(x1)
f(a)
Area of Trapezoid 1
Area of Trapezoid 2
x1
Area under curve
10Numerical Integration
- Using more trapezoids to approximate area under
the curve is called Composite Trapezoidal Rule - The more trapezoids, the better, so instead of
two trapezoids, well use n trapezoids. - The greater the value of n, the better our
approximation of the area will be.
11Composite Trapezoidal Rule
- Divide interval a,b into n equally spaced
subintervals (Add area of the n trapezoids) - b x1
x2
b - ? f(x) dx ? f(x) dx ? f(x) dx ? f(x)
dx - a a
x1
xn-1 - (b-a)/2n f(x) f(x1) f(x1)
f(x2) f(xn-1) f(b) - (b-a)/2n f(x) 2 f(x1) 2
f(x2) 2 f(xn-1) f(b) - b
- ? f(x) dx ?x/2 y0 2y1 2y2 2yn-1
yn - a
Composite Trapezoidal Rule
12Example 1 on Numerical Integration
- Implementing Composite Trapezoidal Rule in Matlab
- Example Curve f(x) 1/x , lets integrate
it from e,2e - 2e 2e
- ? 1/x dx ln (x) ln (2e) ln
(e) ln (2) 0.6931 - e
e - Matlab Equivalent
- function ITrapez(f, a, b, n) take f, add n
trapezoids,from a to b - Integration using composite trapezoid rule
- h (b-a)/n increment
- s feval(f,a) starting value
- for i1n-1
- x(i) a ih
- s s2 feval (f,x(i))
- end
- s s feval(f,b)
- I sh/2
Area under curve 1/x, from e,2e
Inline(1/x)
In our case, input to the function will be f
inline (1/x) a e 2.7182818 b 2e
13Example 2 on Numerical Integration Using MATLAB
Vectorized Operations
- This function carries out the same function as
the previous example, but by using MATLAB
Vectorized operations, it runs much faster. - function IFastTrap(f, a, b, n)
- Same as the previous Trapezoidal function
example, but using more - efficient MATLAB vectorized operations.
- h(b-a)/n Increment value
- sfeval(f, a) Starting value
- in1n-1
- xpointsainh Defining the x-points
- ypointsfeval(vectorize(f),xpoints) Get
corresponding y-points - sig2sum(ypoints) Summing up values in
ypoints, and mult. by 2 - sssigfeval(f,b) Evaluating last term
- Ish/2
14Example 3 Integrating Trapezoidal/FastTrap
Function into Main Program
- Main program to test numerical integration
function, and to measure difference in speed
between the two previous functions. - Example 3 Main program to run the numerical
integration function, - using the user-created
Trapezoidal/FastTrap methods. - foninline('log) Defines the function we
wish to integrate. - aexp(1) Starting point.
- b2a Ending point.
- n1000 Number of intervals.
- tic Start counter.
- OutValueTrapez (fon, a, b, n) Calling
Trapezoidal function. - toc Stop counter, and print out counters
value. - Try replacing the Trapez function with the
vectorized FastTrap - function, and notice the difference in speeds
between the two.
15Example 3 Continuation
- Try two different values of N
- N1,000
- N100,000.
- For both N values, test the code using both
functions for the Trapezoidal method The normal
Trapez Method, and the FastTrap Method. - Compare the difference in execution time between
the standard way (Trapez), and the vectorized
approach (FastTrap).
16Additional MATLAB Exercise
- Function y(i) sin x(i)
- Where x(i) is defined by 101 uniformly spaced
points in 0, 2p. - Define the integral
- x(i)
- Int (i) ? sin (t) dt
- 0
- Calculate Int(i) for all values, i 1, , 101
- Plot y(i) and Int(i) versus x(i), for i 1, , 101