259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

Slides:



Advertisements
Similar presentations
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
Advertisements

Chapter 11: Symbolic Computing for Calculus
Follow the link to the slide. Then click on the figure to play the animation. A Figure Figure
Al-Amer An Introduction to MATLAB Lesson 2: M-files Dr. Samir Al-Amer Term 061.
Integration – Adding Up the Values of a Function (4/15/09) Whereas the derivative deals with instantaneous rate of change of a function, the (definite)
Riemann Sums. Objectives Students will be able to Calculate the area under a graph using approximation with rectangles. Calculate the area under a graph.
MTH 252 Integral Calculus Chapter 6 – Integration Section 6.5 – The Definite Integral Copyright © 2005 by Ron Wallace, all rights reserved.
Numerical Solutions of Differential Equations Taylor Methods.
1 Chapter 8 Calculus Operations with MATLAB We are now ready to see how calculus operations can be performed with MATLAB. It should be noted that a digital.
ME457 Mechatronic System Modeling MICHIGAN STATE UNIVERSITY Matlab® refresher Your objective: to dominate! My objective: to help you dominate!
Definition: the definite integral of f from a to b is provided that this limit exists. If it does exist, we say that is f integrable on [a,b] Sec 5.2:
259 Lecture 15 Introduction to MATLAB. 2 What is MATLAB?  MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical.
Numerical Computation
4.6 Numerical Integration -Trapezoidal Rule -Simpson’s Rule
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
259 Lecture 18 The Symbolic Toolbox. 2  MATLAB has a set of built-in commands that allow us to work with functions in a fashion similar to Mathematica.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Georg Friedrich Bernhard Riemann
Chapter 9 Numerical Integration Flow Charts, Loop Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Aim: Riemann Sums & Definite Integrals Course: Calculus Do Now: Aim: What are Riemann Sums? Approximate the area under the curve y = 4 – x 2 for [-1, 1]
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
6.3 Definite Integrals and the Fundamental Theorem.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
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.
The Derivative Definition, Interpretations, and Rules.
Homework questions thus far??? Section 4.10? 5.1? 5.2?
Section 5.4a FUNDAMENTAL THEOREM OF CALCULUS. Deriving the Theorem Let Apply the definition of the derivative: Rule for Integrals!
MAT 1221 Survey of Calculus Maple
CSC 1051 – Finite Differences Dr. Joel Hirsh CSC 1051 M.A. Papalaskari, Villanova University Optional Project.
Riemann Sums, Trapezoidal Rule, and Simpson’s Rule Haley Scruggs 1 st Period 3/7/11.
Outline Comparison of Excel and R R Coding Example – RStudio Environment – Getting Help – Enter Data – Calculate Mean – Basic Plots – Save a Coding Script.
Scientific Computing Introduction to Matlab Programming.
CHAPTER 4 SECTION 4.3 RIEMANN SUMS AND DEFINITE INTEGRALS.
6.5 Applications of the Definite Integral. In this section, we will introduce applications of the definite integral. Average Value of a Function Consumer’s.
CHAPTER Continuity The Definite Integral animation  i=1 n f (x i * )  x f (x) xx Riemann Sum xi*xi* xixi x i+1.
In this section, we will investigate how to estimate the value of a definite integral when geometry fails us. We will also construct the formal definition.
Definite Integrals Riemann Sums and Trapezoidal Rule.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 7.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 8.
Introduction to Matlab
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Antiderivatives and Indefinite Integration
Differentials A quick Review on Linear Approximations and Differentials of a function of one Variable.
Lesson 7-7 Numerical Approximations of Integrals -- What we do when we can’t integrate a function Riemann Sums Trapezoidal Rule.
Riemann Sums and Definite Integration y = 6 y = x ex: Estimate the area under the curve y = x from x = 0 to 3 using 3 subintervals and right endpoints,
Integral Review Megan Bryant 4/28/2015. Bernhard Riemann  Bernhard Riemann ( ) was an influential mathematician who studied under Gauss at the.
CS 100Lecture 231 CS100J Lecture 23 n Previous Lecture –MatLab and its implementation, continued. n This Lecture –MatLab demonstration.
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
Chapter 6 Integration Section 5 The Fundamental Theorem of Calculus (Day 1)
Definite Integral df. f continuous function on [a,b]. Divide [a,b] into n equal subintervals of width Let be a sample point. Then the definite integral.
Sect. 4.1 Antiderivatives Sect. 4.2 Area Sect. 4.3 Riemann Sums/Definite Integrals Sect. 4.4 FTC and Average Value Sect. 4.5 Integration by Substitution.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
Lesson 5-2 The Definite Integral. Ice Breaker Find area between x-axis and y = 2x on [0,3] using 3 rectangles and right-end points v t Area of.
5.2 – The Definite Integral. Introduction Recall from the last section: Compute an area Try to find the distance traveled by an object.
Numerical Differentiation and Integration in MATLAB; Function M-files
259 Lecture 18 The Symbolic Toolbox.
Introduction to MATLAB
Working with Data in MATLAB
Computer Application in Engineering Design
Derivative of an Exponential
MATH 493 Introduction to MATLAB
Code is on the Website Outline Comparison of Excel and R
Definite Integrals Rizzi – Calc BC.
Objectives Approximate a definite integral using the Trapezoidal Rule.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files

22 Derivatives and Integrals  We can use MATLAB to numerically differentiate or integrate functions!  The key is to remember the definitions of derivative and definite integral:

3 Numerical Differentiation  For small x, we have the following estimate for f’(x):  Using the MATLAB commands “linspace” and “diff”, we can find reasonable approximations to f’(x), provided x is small and f is differentiable!

4 Numerical Differentiation  Example 1:  Use MATLAB to find the numerical derivative of y = sin(x) on the interval [0, 2].  Compare this estimate for dy/dx to the actual derivative of y.

5 Numerical Differentiation  Example 1 (cont.):  Try each of the following commands with a = 0, b = 2*pi, and n = 50. linspace(a, b, n) a:(b-a)/(n-1):b  What do you notice?  Try “linspace(a,b)”.  What happens in this case?

6 Numerical Differentiation  Example 1 (cont.):  Next, try these commands: x = linspace(1, 10, 10) diff(x)  What happens?  In general, for x = [a, b, c, d], diff(x) = [b-a, c-b, d-c].  Now we are ready to estimate dy/dx!

7 Numerical Differentiation  Example 1 (cont.):  Enter the following commands to create an estimate for dy/dx, which we’ll call yprime: x = linspace(0, 2*pi, 1000); y = sin(x); deltax = diff(x); deltay = diff(y); yprime = deltay./deltax;

8 Numerical Differentiation  Example 1 (cont.)  To compare our estimate to the actual derivative, let’s look at a table of the first 10 values of yprime and cos(x), via concatenation: [yprime(1:10); cos(x(1:10))]’.  Note the use of the colon (:) and transpose (‘) commands!

9 Numerical Differentiation  Example 1 (cont.)  Let’s also compare graphically!  One way to do this is with the “subplot” command.  Try the following commands: subplot(1,2,1) plot(x(1:999), yprime, 'r‘) title(‘yprime = \Deltay/\Deltax’) subplot(1,2,2) plot(x(1:999), cos(x(1:999)),‘b') title(‘dy/dx = cos(x)’)  Why can’t we just use “x, yprime”, etc. in our plot commands?

10 Numerical Integration  For integrable function f(x), choosing x i = (b-a)/n and x i * to be the right endpoint of the ith subinterval, i.e. x i * =a+i*(b-a)/n, we get the following estimate for large n:  Using the “sum” command, we can find an estimate for definite integrals via Riemann sums!

11 Numerical Integration  Example 2:  Use MATLAB to numerically estimate the definite integral  Compare this estimate as n gets larger to the actual value for the integral.

12 Numerical Integration  Example 2 (cont.)  Enter the following commands to compute Riemann sums for our integral! a = 0; b = 1; n = 50; deltax = (b-a)/n; xstar = a+deltax:deltax:b; Rn = sum((xstar.*xstar) *deltax)

13 Function M-Files  In addition to using M-files to run scripts, we can use them to create functions!  Function M-files can accept input and produce output. One example of a function defined by a function M-file is “linspace”.  Let’s make a function via an M-file!

14 Function M-Files  Within the M-file script editor, type the following: function y = Sample1(x) %Here is where you put in information about how the function is used. %The syntax and variables can be outlined here as well. y = x + x.^2 – x.^4;  Save the file as Sample1.m on the Desktop and make sure the Path is set to see the file!

15 Function M-Files  To use the new function, for example to find the value of Sample1(x) at x = 3, type: Sample1(3)  Find the numerical derivative of Sample1(x) and plot both y = Sample1(x) and its numerical derivative on [-1,1].

16 References  Using MATLAB in Calculus by Gary Jenson