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

Riemann Sums. Objectives Students will be able to Calculate the area under a graph using approximation with rectangles. Calculate the area under a graph.
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.
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:
Math 5A: Area Under a Curve. The Problem: Find the area of the region below the curve f(x) = x 2 +1 over the interval [0, 2].
259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.
259 Lecture 15 Introduction to MATLAB. 2 What is MATLAB?  MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical.
4.6 Numerical Integration Trapezoid and Simpson’s Rules.
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.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
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!
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.
CHAPTER 4 SECTION 4.3 RIEMANN SUMS AND DEFINITE INTEGRALS.
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.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 7.
Antiderivatives and Indefinite Integration
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,
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
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.
5.2 – The Definite Integral. Introduction Recall from the last section: Compute an area Try to find the distance traveled by an object.
[5-4] Riemann Sums and the Definition of Definite Integral Yiwei Gong Cathy Shin.
MTH 252 Integral Calculus Chapter 6 – Integration Section 6.6 – The Fundamental Theorem of Calculus Copyright © 2005 by Ron Wallace, all rights reserved.
Area Under the Curve We want to approximate the area between a curve (y=x2+1) and the x-axis from x=0 to x=7 We will use rectangles to do this. One way.
Lesson 46 – Area Under the Curve – Riemann Sums
MTH1170 Integrals as Area.
259 Lecture 18 The Symbolic Toolbox.
Introduction to MATLAB
Chapter 5 The Definite Integral. Chapter 5 The Definite Integral.
Working with Data in MATLAB
Computer Application in Engineering Design
NUMERICAL INTEGRATION
Lesson 47 - Area under Curves as Limits and Sums
Derivative of an Exponential
. N=n => width = 1/n US = =1/n3[1+4+9+….+n2] US =
L 12 application on integration
Basic operations in Matlab
ISHIK UNIVERSITY FACULTY OF EDUCATION Mathematics Education Department
6-4 Day 1 Fundamental Theorem of Calculus
5.1 – Estimating with Finite Sums
Integration & Area Under a Curve
TECHNIQUES OF INTEGRATION
Math 200 Week 7 - Friday Double Integrals.
§4.9 Antiderivatives There are two branches in calculus:
MATH 493 Introduction to MATLAB
Fundamental Theorem of Calculus Indefinite Integrals
Accumulation AP Calculus AB Days 11-12
Fundamental Theorem of Calculus (Part 2)
Code is on the Website Outline Comparison of Excel and R
MATLAB Tutorial ECE 002 Professor S. Ahmadi.
Copyright © Cengage Learning. All rights reserved.
Section 5.2 Definite Integrals.
The Fundamental Theorem of Calculus
Definite Integrals Rizzi – Calc BC.
Riemann Sums and Definite Integrals
Arc Length … x y a b xi ... Pi P0 P1 Pn
Numerical Integration
Objectives Approximate a definite integral using the Trapezoidal Rule.
Your objective: to dominate! My objective: to help you dominate!
Section 5.2 Definite Integrals
Symbolic Integral Notation
Copyright © Cengage Learning. All rights reserved.
4.2 – Areas 4.3 – Riemann Sums Roshan Roshan.
Calculus I (MAT 145) Dr. Day Wednesday April 17, 2019
Presentation transcript:

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

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: 2

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!

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.

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?

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!

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;

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!

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?

Numerical Integration For integrable function f(x), choosing xi = (b-a)/n and xi* to be the right endpoint of the ith subinterval, i.e. xi*=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!

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.

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)

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!

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!

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

References Using MATLAB in Calculus by Gary Jenson 16