MATLAB Tutorial ECE 002 Professor S. Ahmadi.

Slides:



Advertisements
Similar presentations
MATLAB Tutorial ECE 002 Professor S. Ahmadi.
Advertisements

ES 240: Scientific and Engineering Computation. Chapter 17: Numerical IntegrationIntegration  Definition –Total area within a region –In mathematical.
Parallel Computing in Matlab
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
Chapter 9 Numerical Integration Numerical Integration Application: Normal Distributions Copyright © The McGraw-Hill Companies, Inc. Permission required.
Numerical Integration
Numerical Computation
Chapters 5 and 6: Numerical Integration
4.6 Numerical Integration Trapezoid and Simpson’s Rules.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
AP CALCULUS PERIODIC REVIEW. 1: Limits and Continuity A function y = f(x) is continuous at x = a if: i) f(a) is defined (it exists) ii) iii) Otherwise,
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Riemann Sums, Trapezoidal Rule, and Simpson’s Rule Haley Scruggs 1 st Period 3/7/11.
Scientific Computing Introduction to Matlab Programming.
Calculus: The Key Ideas (9/3/08) What are the key ideas of calculus? Let’s review and discuss. Today we review the concepts of definite integral, antiderivative,
Definite Integrals Riemann Sums and Trapezoidal Rule.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Copyright © Cengage Learning. All rights reserved. 16 Vector Calculus.
Lesson 7-7 Numerical Approximations of Integrals -- What we do when we can’t integrate a function Riemann Sums Trapezoidal Rule.
Integration/Antiderivative. First let’s talk about what the integral means! Can you list some interpretations of the definite integral?
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
數值方法 2008, Applied Mathematics NDHU 1 Numerical Integration.
CMPSC 200 Fall 2013 Lecture 37 November 18, 2013.
Numerical Differentiation and Integration in MATLAB; Function M-files
List manipulation;curve fitting
Lecture #5 מבוא מורחב.
Algorithms and Programming
Introduction to Computing Science and Programming I
MATLAB – More Script Files
NUMERICAL DIFFERENTIATION Forward Difference Formula
Trapezoid Method and Iteration & Acceleration
NUMERICAL INTEGRATION
MTH1170 Numeric Integration
Copyright © Cengage Learning. All rights reserved.
Functions CIS 40 – Introduction to Programming in Python
Numerical Integration Formulas
5. 7a Numerical Integration. Trapezoidal sums
Numerical Integration
The Area Question and the Integral
Higher-Order Procedures
TECHNIQUES OF INTEGRATION
PROBLEM SOLVING CSC 111.
Copyright © Cengage Learning. All rights reserved.
Chapter 7 Numerical Differentiation and Integration
Lecture #5 מבוא מורחב.
Copyright © Cengage Learning. All rights reserved.
5. 7a Numerical Integration. Trapezoidal sums
6.001 SICP Streams – the lazy way
Python programming exercise
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Copyright © Cengage Learning. All rights reserved.
Arc Length … x y a b xi ... Pi P0 P1 Pn
Matlab Basics Tutorial
Assignment 1: due 1/17/19 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
1.2 Commutative, Identity, and Associative Properties
Comp 208 Computers in Engineering Yi Lin Winter, 2007
APPLICATIONS OF DIFFERENTIATION
5.1 Areas and Distances Approximating the area under a curve with rectangles or trapezoids, and then trying to improve our approximation by taking.
Chapter 6 Integration.
Game Description Player 1: Decides on integer x > 0
Copyright © Cengage Learning. All rights reserved.
Presentation transcript:

MATLAB Tutorial ECE 002 Professor S. Ahmadi

Tutorial 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.

What 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.

Example 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 = (arg1+arg2+arg3)/3;

How 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 Y=average3(1, 2, 3);. % average3 is a user-defined function. % The average value of 1,2 and 3 will be stored in Y

Example: 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?'); VarAverage=average3(VarA, VarB, VarC); % num2str converts a number to a string. result=[‘The average value was’, num2str(VarAverage)]

Numerical Integration General Approximation Made With Num. Integration: b n ∫ f(x) dx ≈ ∑ ci f(xi) a i=0 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, we’ll use a trapezoid shape. Area of trapezoid = ½ (b1 + b2) h x y f(b) f(a) base 2 (b1) a b h base 2 (b2)

Numerical Integration Trapezoidal Rule (con’t): x y 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) a b How can we get a better approximation of the area under the curve? Answer: Use More Trapezoids

Numerical Integration More trapezoids give a better approximation of the area under curve x y 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 a b x1 Area under curve ≈

Numerical 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, we’ll use n trapezoids. The greater the value of n, the better our approximation of the area will be.

Composite 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

Example 1 on Numerical Integration Implementing Composite Trapezoidal Rule in Matlab Example Curve: f(x) = 1/x , let’s 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 I=Trapez(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 i=1:n-1 x(i) = a + i*h ; s = s+2 * feval (f,x(i)) ; end s = s + feval(f,b) ; I = s*h/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

Example 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 I=FastTrap(f, a, b, n) % Same as the previous Trapezoidal function example, but using more % efficient MATLAB vectorized operations. h=(b-a)/n; % Increment value s=feval(f, a); % Starting value in=1:n-1; xpoints=a+in*h; % Defining the x-points ypoints=feval(vectorize(f),xpoints); % Get corresponding y-points sig=2*sum(ypoints); % Summing up values in ypoints, and mult. by 2 s=s+sig+feval(f,b); % Evaluating last term I=s*h/2;

Example 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. fon=inline('log‘); % Defines the function we wish to integrate. a=exp(1); % Starting point. b=2*a; % Ending point. n=1000; % Number of intervals. tic % Start counter. OutValue=Trapez (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.

Example 3: Continuation Try two different values of N N=1,000 N=100,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).

Additional MATLAB Exercise Function: y(i) = sin [ x(i) ] Where x(i) is “defined” by 101 uniformly spaced points in [0, 2π]. Define the integral: x(i) Int (i) = ∫ sin (t) dt Calculate Int(i) for all values, i = 1, … , 101 Plot y(i) and Int(i) versus x(i), for i =1, …, 101