Euler s Method and Functions



Similar documents
Scientific Programming

Objectives. Materials

Lecture 2 Mathcad Basics

By Clicking on the Worksheet you are in an active Math Region. In order to insert a text region either go to INSERT -TEXT REGION or simply

1.7 Graphs of Functions

Beginner s Matlab Tutorial

Student Performance Q&A:

Temperature Scales. The metric system that we are now using includes a unit that is specific for the representation of measured temperatures.

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Curve Fitting, Loglog Plots, and Semilog Plots 1

Updates to Graphing with Excel

Cooling and Euler's Method

Overview. Observations. Activities. Chapter 3: Linear Functions Linear Functions: Slope-Intercept Form

List the elements of the given set that are natural numbers, integers, rational numbers, and irrational numbers. (Enter your answers as commaseparated


Because the slope is, a slope of 5 would mean that for every 1cm increase in diameter, the circumference would increase by 5cm.

Elements of a graph. Click on the links below to jump directly to the relevant section

Summary of important mathematical operations and formulas (from first tutorial):

The Fourth International DERIVE-TI92/89 Conference Liverpool, U.K., July Derive 5: The Easiest... Just Got Better!

Worksheet 1. What You Need to Know About Motion Along the x-axis (Part 1)

AP Physics 1 and 2 Lab Investigations

3. Mathematical Induction

2.2 Derivative as a Function

the points are called control points approximating curve

DERIVATIVES AS MATRICES; CHAIN RULE

Review of Fundamental Mathematics

Graphing calculators Transparencies (optional)

Acquisition Lesson Planning Form Key Standards addressed in this Lesson: MM2A2c Time allotted for this Lesson: 5 Hours

Excel -- Creating Charts

F.IF.7b: Graph Root, Piecewise, Step, & Absolute Value Functions

1 Error in Euler s Method

Math 120 Final Exam Practice Problems, Form: A

Worksheet for Exploration 2.1: Compare Position vs. Time and Velocity vs. Time Graphs

Numerical Methods for Differential Equations

Computational Mathematics with Python

Graphic Designing with Transformed Functions

Absolute Value Equations and Inequalities

2.5 Transformations of Functions

Computational Mathematics with Python

AP CALCULUS AB 2007 SCORING GUIDELINES (Form B)

2.1 Increasing, Decreasing, and Piecewise Functions; Applications

In following this handout, sketch appropriate graphs in the space provided.

A Determination of g, the Acceleration Due to Gravity, from Newton's Laws of Motion

Week 1: Functions and Equations

Sample Fraction Addition and Subtraction Concepts Activities 1 3

1 The Brownian bridge construction

MATLAB Workshop 14 - Plotting Data in MATLAB

correct-choice plot f(x) and draw an approximate tangent line at x = a and use geometry to estimate its slope comment The choices were:

TI-83/84 Plus Graphing Calculator Worksheet #2

Linear and quadratic Taylor polynomials for functions of several variables.

Graphing Quadratic Functions

TImath.com Algebra 1. Absolutely!

Part 1: Background - Graphing

Natural cubic splines

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

EXCEL Tutorial: How to use EXCEL for Graphs and Calculations.

Homework 2 Solutions

AP CALCULUS AB 2006 SCORING GUIDELINES (Form B) Question 4

Excel Basics By Tom Peters & Laura Spielman

Microeconomic Theory: Basic Math Concepts

Computational Mathematics with Python

Doing Multiple Regression with SPSS. In this case, we are interested in the Analyze options so we choose that menu. If gives us a number of choices:

Derive 5: The Easiest... Just Got Better!

9. Momentum and Collisions in One Dimension*

0 Introduction to Data Analysis Using an Excel Spreadsheet

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

Graphical Integration Exercises Part Four: Reverse Graphical Integration

EL-9650/9600c/9450/9400 Handbook Vol. 1

Linear Programming. March 14, 2014

Fixed Point Theorems

In order to describe motion you need to describe the following properties.

Calculus AB 2014 Scoring Guidelines

Introduction to MATLAB IAP 2008

Numerical Solution of Differential

Guide to Using the Ti-nspire for Methods - The simple and the overcomplicated Version 1.5

arxiv: v1 [math.pr] 5 Dec 2011

The Graphical Method: An Example

INTRUSION PREVENTION AND EXPERT SYSTEMS

Relationships Between Two Variables: Scatterplots and Correlation

ECON 459 Game Theory. Lecture Notes Auctions. Luca Anderlini Spring 2015

VHDL Test Bench Tutorial

PGR Computing Programming Skills

CHAPTER FIVE. Solutions for Section 5.1. Skill Refresher. Exercises

Visualizing Differential Equations Slope Fields. by Lin McMullin

Fractions as Numbers INTENSIVE INTERVENTION. National Center on. at American Institutes for Research

Absorbance Spectrophotometry: Analysis of FD&C Red Food Dye #40 Calibration Curve Procedure

440 Geophysics: Heat flow with finite differences

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Intermediate PowerPoint

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

Scientific Graphing in Excel 2010

Introduction to Matrices

Graphing in excel on the Mac

1 Review of Newton Polynomials

Questions: Does it always take the same amount of force to lift a load? Where should you press to lift a load with the least amount of force?

Representation of functions as power series

Hypothesis testing. c 2014, Jeffrey S. Simonoff 1

EdExcel Decision Mathematics 1

Calculator Notes for the TI-Nspire and TI-Nspire CAS

SDC. Schroff Development Corporation PUBLICATIONS. MultiMedia CD by Jack Zecher

Transcription:

Chapter 3 Euler s Method and Functions The simplest method for approximately solving a differential equation is Euler s method. One starts with a particular initial value problem of the form dx dt = f(t, x), x(t 0) = x 0, (3.0.1) such as dx = x(1 x), x(0) = 0.1 dt or dx = x(1 x) + 1 + sin(t), x(0) = 0.1 dt The equation and the initial condition combine to tell us the derivative of the solution x(t) at time t 0, x (t 0 ) = f(t 0, x 0 ). Knowing the derivative, we can expect that the solution will look linear, with the given slope, if t is near t 0. Pick a value t 1, and let h = t 1 t 0. The (Euler) approximate solution at t 1 is x(t 1 ) x 1 = x(t 0 ) + hx (t 0 ) = x(t 0 ) + hf(t 0, x 0 ). This value x 1 can be used with the equation to obtain an approximate value for the derivative at t 1, x (t 1 ) x 1 = f(t 1, x 1 ). 23

24 CHAPTER 3. EULER S METHOD AND FUNCTIONS The method continues in this fashion, with sample times t n = t 0 + nh, t n+1 t n = h, and approximate solution values defined iteratively, x(t n+1 ) x(t n ) + hf(t n, x n ). Program 2 uses the Matlab commands and arrays previously introduced to compute an approximate solution of the initial value problem dx dt = x(1 x), x(0) = 0.1 Recall that Matlab arrays have initial index 1, so we let T(i) = t i 1, X(i) = x i 1, i = 1,...,N. Programs (or main programs) are usually saved as a script file in Matlab. If you click on File, new, and script, you should get the correct file format. Function subprograms, which will be discussed soon, should be saved as new function files.

3.1. PROGRAM 2 25 3.1 Program 2 % This program is designed to exercise some of the basic % capabilities of Matlab for studying differential equations. % The initial problem is solving the differential equation % x = x(1-x) % using Euler s method, then plotting the results. % Euler s method computes values of an approximate solution % on an interval [a,b] % In this example the interval will be [0,10], and % the solution will be computed at 1000 points. % Initialize parameters and vectors N = 1000; % N is the vector size for the problem. T = zeros(n,1); % T will contain samples from the t-axis. X = zeros(n,1); % X will contain samples of the solution. % The vector T will be needed for plotting the results. % It is not part of the basic Euler s method. %This method uses a step size % h which needs to be defined. % We also need an initial value for the solution. h =.01; X(1) = 0.1; % Next, Euler s method calculates the values of the solution X, % and the corresponding value of T. for i=2:n T(i) = (i-1)*h; X(i) = X(i-1) + h*x(i-1)*(1 - X(i-1)); end % The plot can be generated using the following commands. % The axis command helps control the display, forcing the % x-axis to be displayed from 0 to 10, and the y-axis % to be displayed from 0 to 1. plot(t,x); axis([0 10 0 1]);

26 CHAPTER 3. EULER S METHOD AND FUNCTIONS Although Euler s method is simple, it is usually inefficient for high accuracy computations. As an example, you could consider the problem of computing the constant e = exp(1) to 12 digits, which is typically the accuracy of your calculator. Since exp(t) = e t satisfies the initial value problem dx dt = x, x(0) = 1, the value of e could be calculated using Euler s method. Program 3 carries out this computation. Since scientific software has highly precise algorithms for basic computations like the value of e x, we can easily compare Euler s method with the known value. This code allows you to compare results graphically. Notice that the command plot(t,x,t,y); plots the two arrays X and Y as functions of T. The second set of plotting commands is more sophisticated. The command axis([0 5 0 200]); controls the displayed domain and range. A title and a label for t-axis are added with the instructions title( Comparing exp(t) with Euler computation ); xlabel( t ); One of the functions is plotted with dashes using plot(t,y, -- ); A legend connecting the graphs to their display formats is created with legend( Euler computation, exp(t) ); Finally, the computer has to be warned that it should not throw away the first graph before displaying the second. This is handled with the commands hold on;... hold off;

3.1. PROGRAM 2 27 I ve also tossed in a command which is useful for interrupting the program and restarting it, for instance after you have examined some result. The command control = input( Hit enter (or return) to continue ); causes the computer to type the string Hit enter (or return) to continue to the screen. It then waits until you hit return before continuing to execute the program.

28 CHAPTER 3. EULER S METHOD AND FUNCTIONS 3.2 Program 3 % One problem with numerical computations like Euler s method % is that they produce results which may not be accurate. % One way to assess accuracy is to compare their computations % with known results. For instance, we could solve the simple % differential equation y = y, with solution y(t) = exp(t). % Here s a piece of code that computes this function with % Euler s method, and graphically compares the result with % the exact solution. % Initialize parameters and vectors N = 100; % N is the vector size for the problem T = zeros(n,1); % T holds the t-axis samples X = zeros(n,1); % X holds the computed solution Y = zeros(n,1); % Y holds the exact solution. % Let s solve the differential equation % x = x, x(0) = 1 % on the interval [0,5] using Euler s method. h = 5/N; % Define step size h X(1) = 1; % Set initial value for computed solution Y(1) = 1; % Set initial value for exact solution T(1) = 0; % Initialize T for i=2:n T(i) = (i-1)*h; Y(i) = exp(t(i)); X(i) = X(i-1) + h*x(i-1); end plot(t,x,t,y); % Obviously, the functions are not the same over the % range [0,5]. % To make the plot more useful, here is an alternative set of % commands that adds captions as it plots one graph at a time.

3.2. PROGRAM 3 29 % The hold on command is needed to overlay plots. % Wait for a key to be pressed on the keyboard control = input( Hit enter (or return) to continue ); plot(t,x); axis([0 5 0 200]); title( Comparing exp(t) with Euler computation ); xlabel( t ); hold on; plot(t,y, -- ); legend( Euler computation, exp(t) ); hold off;

30 CHAPTER 3. EULER S METHOD AND FUNCTIONS Program 4 is a revision of the Euler s method code which includes two important features: (i) input of parameters from the keyboard, and (ii) use of function subprograms. Both features are helpful if you want to develop a piece of software that can be used repeatedly for solving similar problems. In this case we want to solve initial value problems using Euler s method. Euler s method computes values of an approximate solution to x = f(t, x), x(t 0 ) = x 0, on an interval [a, b]. The algorithm also requires us to specify a number N of algorithmic increments, corresponding to N + 1 sample points t 0,...,t N. Input of the parameters N, a and b from the keyboard is controlled by the following instructions. N = input( Enter the number of sample points, then hit return \n ) a = input( Enter the value of "a" to define the interval [a,b] \n ) b = input( Enter the value of "b" to define the interval [a,b] \n ) Each command prints the character string in single quotes to the screen, waits until you enter a value and hit return, then assigns the typed value to the variable on the left of the equal sign. The \n at the end of the strings tells the computer to advance one line on the display device. This makes it easier to see what values are being entered.

3.3. PROGRAM 4 - MAIN PROGRAM 31 3.3 Program 4 - Main program % This program revises the Euler s method code to include a function. % In addition, part of the problem description will be input from % the keyboard. % Euler s method computes values of an approximate solution to % x = f(t,x), x(t_0) = x_0 % on an interval [a,b] % In this example the interval and the number of sample points % will be input from the keyboard. flag = 0; while(flag == 0) N = input( Enter the number of sample points, then hit return \n ) a = input( Enter the value of "a" to define the interval [a,b] \n ) b = input( Enter the value of "b" to define the interval [a,b] \n ) flag = input( Enter 1 to proceed, or 0 to redefine parameters \n ); end % Initialize vectors T = zeros(n,1); % T will contain sample values from the t-axis. X = zeros(n,1); % X will contain samples of the computed solution. % The vector T will be needed for plotting the results. It is not % part of the basic Euler s method. %This method uses a computed step size h. h = (b-a)/n % We also need an initial value for the solution. X(1) = input( Enter the initial value of the solution x at t = a \n ); T(1) = a; % Next, Euler s method calculates the values of the solution X, and % the corresponding value of T. for i=2:n T(i) = a + (i-1)*h; X(i) = X(i-1) + h*myfunction1(x(i-1));

32 CHAPTER 3. EULER S METHOD AND FUNCTIONS end % The plot can be generated using the following commands. % The axis command helps control the display, forcing the % x-axis to be displayed from 0 to 10, and the y-axis % to be displayed from 0 to 1. plot(t,x); axis([a b 0 1]);

3.3. PROGRAM 4 - MAIN PROGRAM 33 Notice that the program contains extra code, which is a bit more complex than is absolutely necessary. Experience teaches that this approach is useful. When you are entering control parameters for a familiar program, mistakes are fairly common. Instead of executing the program with incorrect values, which in some cases could mean a delay of hours or more, or interupting the computer, a practical idea is to deliberately stop the program execution until you confirm that you want to proceed. That is the purpose of the given segment of instructions. flag = 0; while(flag == 0) N = input( Enter the number of sample points, then hit return \n ) a = input( Enter the value of "a" to define the interval [a,b] \n ) b = input( Enter the value of "b" to define the interval [a,b] \n ) flag = input( Enter 1 to proceed, or 0 to redefine parameters \n ); end The while loop repeatedly executes the input commands until the value of flag changes from 0 to another value. If you enter a 1 at the final prompt, the loop will stop repeating, and the program will continue. Matlab has based much of its programming structure on the programming language C. Along with C, Matlab has an AWFUL feature illustrated by the while statement while(flag == 0) Notice that there is a double equal sign ==. To execute the while command, the computer evaluates the truth of the statement flag equals 0 if you type flag == 0. However, if you type flag = 0 with a single equal sign, you get an error message. The situation is much worse in C, where the computer assigns the value 0 to the variable flag, then tries to evaluate the truth of that statement. This can lead to major problems. The second feature in program 4 is the use of a function subprogram, which in this case is called Myfunction1. In this example the function is simply used to provide a concrete example of the general function f(t, x) that we see in the problem or algorithm description.

34 CHAPTER 3. EULER S METHOD AND FUNCTIONS Function subprograms are an essential part of good software development. Suppose we wanted to expand this program to include a graph of f(t, x) = x(1 x), as well as a comparison of several different algorithms for solving the differential equation, each of which requires the x(1 x), perhaps at different sample points. We could easily end up writing the expression x(1 x) several times. As long as f = x(1 x), the repetitions don t seem like much of a problem. Suppose now that we have to replace x(1 x) by a complex expression requiring many lines of code. For instance, f may have a piecewise definition with 10 different pieces. Now the problem of repetitions is much more severe. Not only is there more typing, but each time we type a new copy there is a chance of making an error. Suppose the function needs to be changed. Then we have to track down each place that it occurs, and make sure the changes are identical. This situation quickly becomes a maintainence nightmare. The solution is to use a function subprogram. This is a separate piece of code, kept (in Matlab) in a separate file. Every time the main program needs to evaluate x(1 x) we send a message to the function, which sends back the desired value. If we have to make changes, or keep several copies of different functions, the typing of changes is minimized, and the changes that do occur do not effect the integrity of the rest of the program. It is hard to overstate the importance of using functions to organize programs. The programs we have seen so far are tiny, with a transparent organization. Out in the real world, programs frequently have hundreds of thousands or millions of instructions. It is generally impossible to understand what the software is doing, get it to function correctly, or maintain it as it goes through revisions, unless it is carefully decomposed into easily understood (and well documented) functions. There are various rules for writing functions. In Matlab, functions should be in a file whose name matches the function name. As you can see from the example following the main program 4, the first line announces that this is a function, and identifies the variable which will be output, y, and names the input x. (These names are arbitrary.) After handling these preliminaries, we simply write instructions to find y as a function of x. In this example, the original function x(1 x) has been modified for values of x when the original function is negative.

3.4. PROGRAM 4 - FUNCTION DEFINITION 35 3.4 Program 4 - function definition function [y] = Myfunction1(x); % This file defines a function for use with % Euler s method for solving initial value problems y = x*(1-x); if y < 0 y = 0; end if y > 1 y = 1; end

36 CHAPTER 3. EULER S METHOD AND FUNCTIONS 3.5 Exercises 1. Modify program 3 so you can find the error in computing e using Euler s method on the interval [0, 1] using N steps. Discuss your results. Approximately how many steps will be required to calculate e by this method with an error smaller than 10 12? Why isn t this a practical method? 2. One way to assess the accuracy of an algorithm like Euler s method is to increase the number of steps N and see how much the solution changes. The usual assumption is that the error in computation with the smaller number of steps is approximately the difference between the two solutions. That is, the case with larger N is treated as if it were the exact solution. Rewrite program 2 so that it solves the same problem with N = 10, N = 100, and N = 1000 steps. Evaluate the differences both numerically and graphically. When N is changed, the value of h will also change. Modify the code so that h is automatically computed based on N. Hand in the code for this problem. The graphical evaluation should show all three plots on the same graph. Estimate the largest error made for each value of N. 3. Our Euler s method code is a bit sloppy in its handling of sample points. Recall that the array value T(n) corresponds to the sample point t n 1. Typically, we would really like sample points t 0,...,t N and sample values x 0,...,x N. The code is actually computing x 0,...,x N 1. Repair the code so that this defect is corrected. Describe your correction, and how you checked it.