Top Banner
Math 2280-002 Wednesday Jan 29 2.4-2.6 numerical methods for solving DE's. Announcements: We'll talk about the Matlab computer scripts that I used to generate the Euler, Improved Euler, and Runge Kutta approximate solutions and slope-field pictures, in Monday's and Tuesday's notes. I'll demo matlab, using these scripts. We'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically, in a different case. I've created a directory in the CANVAS files, which has the files I'll be using today. There is an open- source version of Matlab called GNU-Octave, which you can download to a laptop. Alternately, Matlab is available on University computers, for example in our Math Student Center computer lab. If you prefer, you may adapt or write your own subroutines for other software such as Python, Mathematica, Maple, etc. Our quiz today will be a linear DE IVP, with a bit of modeling. Warm-up Exercise : Next HW at end of notes we'll discuss some of this today I'll be in Math Ctm lab Fri 2 3 for Friday we start Chapter 3 numerical help none today any questions
10

of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

Apr 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

Math 2280-002Wednesday Jan 292.4-2.6 numerical methods for solving DE's.

Announcements: We'll talk about the Matlab computer scripts that I used to generate the Euler, Improved Euler, and Runge Kutta approximate solutions and slope-field pictures, in Monday's and Tuesday's notes. I'll demo matlab, using these scripts. We'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically, in a different case.

I've created a directory in the CANVAS files, which has the files I'll be using today. There is an open-source version of Matlab called GNU-Octave, which you can download to a laptop. Alternately, Matlab isavailable on University computers, for example in our Math Student Center computer lab. If you prefer, you may adapt or write your own subroutines for other software such as Python, Mathematica, Maple, etc.

Our quiz today will be a linear DE IVP, with a bit of modeling.

Warm-up Exercise:

Next HW at end of notes we'll discuss someof this todayI'll be in MathCtmlab Fri 2 3 for

Friday we start Chapter3 numerical help

none today any questions

Page 2: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

class_example.m % Input your slope function f(x,y) (as a string), % i.e. for the DE y'(x)=f(x,y). In this linear DE example, % f(x,y) = 1-3x+y but you can enter whatever function you want. % So we would enter: f = '1-3.*x+y'; f='1-3.*x+y'; % use "3." instead of "3" (which would cause an error) for % scalar multiplication. xmin = -1; xmax = 1; ymin = -1; ymax = 1; SlopeField(xmin,xmax,ymin,ymax,f) % Draw the slope field in the % region xmin<=x<=xmax, ymin<=y<=ymax for dy/dx = f(x,y) hold on % the "hold on" command lets us add more plots to the display % of the first plot, e.g. the slope field in this script, rather % than creating different displays for each plot. % Use Euler method to solve the IVP on % the interval [x0,xmax] with n subintervals : % dy/dx = f(x,y) % y(x0) = y0 x0 = 0; y0 = 0; b=1; n = 5; % Use Euler Method to Solve, then add scatter plot: [xE yE]=Eulers(x0,y0,b,n,f); scatter(xE,yE,20,'blue','filled'); % Use Improved Euler Method to solve [xI yI]=Improved_Eulers(x0,y0,b,n,f); scatter(xI,yI,20,'red','filled'); % Use Runge Kutta method to solve [xR yR]=Runge_Kutta(x0,y0,b,n,f); scatter(xR,yR,20,'black','filled'); sol='2+3.*x-2.*exp(x)'; % exact solution in our example sol = str2func(['@(x)',sol]);

Z=0:.02:1; % x-coords from 0 to 1, partition width .02. W=sol(Z); % y-coords for exact solution plot(Z,W,'color','black','LineWidth',1); xlabel('x');ylabel('y') legend('Slope Field','Euler','Improved Euler','Runge Kutta','Exact') title(['dy/dx = ' f])

script that calls other scripts to create plot atthe end

linestartwithis a comment field

0 y I 3xtyf is RHS ofDE y107 0

size of in matlab everythingslopefield is a matrix

SlopeFeld is a subroutine

intervalimkalize Exo b

Eales is a subroutinereturns vectors XE I YE

ofthe pts wordsofgoogle tale silk

scattermatlab command

convertssymbolicstring into a usable ten

Page 3: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

%Eulers.m

function [x,y] = Eulers(x0,y0,b,n,f)% Eulers method with n intervals to solve the initial value problem dy/dx=f(x,y) with% initial conditions y(x0)=y0 on the interval [x0,b]. The function% f(x,y) must be entered as a string.

% For example, if you wish to solve dy/dx=y*cos(x) with y(0)=1 on the% interval [0,15] with 50 intervals you would enter% Eulers(0,1,15,50,'y.*cos(x)')

f = str2func(['@(x,y) ',f]); %converts the string input into a function handle

h=(b-x0)/n;x=zeros(n+1,1); y= zeros(n+1,1); %Pre-allocating vectors for speedx(1)=x0; y(1)=y0;

for i=1:n x(i+1)=x0+i*h; y(i+1)=y(i)+h*f(x(i),y(i));endend

%Improved_Eulers.m

function [x,y] = Improved_Eulers(x0,y0,b,n,f)% Improved Eulers method with n intervals to solve the initial value % problem dy/dx=f(x,y) with% initial conditions y(x0)=y0 on the interval [x0,b]. The function% f(x,y) must be entered as a string.

% For example, if you wish to solve dy/dx=y*cos(x) with y(0)=1 on the% interval [0,15] with 50 intervals you would enter% Improved_Eulers(0,1,15,50,'y.*cos(x)')

f = str2func(['@(x,y) ',f]); % converts the string input into a function handle

h=(b-x0)/n;x=zeros(n+1,1); y= zeros(n+1,1); % Pre-allocoting vectors for speedx(1)=x0; y(1)=y0;for i=1:n x(i+1)=x0+i*h; k1=f(x(i),y(i)); u=y(i)+h*k1; k2=f(x(i+1),u); y(i+1)=y(i)+h*(k1+k2)/2;endend

for i L to n

ends subroutine

Page 4: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

%Runge_Kutta.m

function [x,y] = Runge_Kutta(x0,y0,b,n,f)% Runge Kutta method with n subintervals to solve the initial value problem dy/dx=f(x,y) with% initial conditions y(x0)=y0 on the interval [x0,b]. The function% f(x,y) must be entered as a string.

% For example, if you wish to solve dy/dx=y*cos(x) with y(0)=1 on the% interval [0,15] with 50 intervals you would enter% Runge_Kutta(0,1,15,50,'y.*cos(x)')

f = str2func(['@(x,y) ',f]); % converts the string input into a function handle

h=(b-x0)/n;x=zeros(n+1,1); y= zeros(n+1,1); % Pre-allocating vectors for speedx(1)=x0; y(1)=y0;for i=1:n x(i+1)=x0+i*h; k1=f(x(i),y(i)); k2=f(x(i)+h/2,y(i)+h/2*k1); k3=f(x(i)+h/2,y(i)+h/2*k2); k4=f(x(i+1),y(i)+h*k3); y(i+1)=y(i)+h*(k1+2*k2+2*k3+k4)/6;endend

%SlopeField.m

function []= SlopeField(xmin,xmax,ymin,ymax,f)% the function f(x,y) must be entered as a string. For example if%f(x,y)=y*cos(x), then you need to enter 'y.*cos(x)' for f

f = str2func(['@(x,y) ',f]); % converts the string input into a function handle that % Matlab recognizes

stepsize1=abs(xmax-xmin)/40;stepsize2=abs(ymax-ymin)/40;[X,Y] = meshgrid(xmin:stepsize1:xmax, ymin:stepsize2:ymax);

dY=f(X,Y);dX=ones(size(dY)); %the vector [dX,dY]=[1,f(X,Y)] will have the correct slope, % and the "AutoScaleFactor" below will scale the slope % segments appropriately if we pick a nice relative factor % like e.g. 0.8quiver(X,Y,dX,dY,'Color','k','LineWidth',1,'AutoScaleFactor',.8,'ShowArrowHead','Off');axis tightend google quiver

for meto fields

Page 5: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

y I 3xty yl07 0

Page 6: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

%famous_numbers.m

format long %display 15 digitsf='y'; % because we are solving y'(x)=f(x,y) for f(x,y)=y, % and estimating y(1), since the solution to y'=y % with y(0)=1 is exp(x) and exp(1)=e.x0 = 0; y0 = 1;b=1;n = 40;

% Use Runge Kutta method to solve: [xR yR]=Runge_Kutta(x0,y0,b,n,f); % compare our estimate to actual value of e, as a column vector. ans1=[n;yR(n+1);exp(1)] % everything in Matlab is a matrix. Here ans1 is a 3 by 1 matrix, % because I used semicolons between the quantities. If I'd have used % commas Matlab would've made a 1 by 3 matrix.

Output from command window, running with n=10,20,40:

>> famous_numbers

ans1 =

10.000000000000000 2.718279744135166 2.718281828459046

>> famous_numbers

ans1 =

20.000000000000000 2.718281692656335 2.718281828459046

>> famous_numbers

ans1 =

40.000000000000000 2.718281819792855 2.718281828459046

351 axe

Page 7: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

Math 2280-002Week 4-5 concepts and homework, due February 5.

Recall that problems which are not underlined are good for seeing if you can work with the underlying concepts; that the underlined problems are to be handed in; and that the Wednesday quiz will be drawn from all of these concepts and from these or related problems.

2.5.4, 2.6: 4: (postponed from last week)3.1: 1, 6, 10, 11, 12, 14, 17, 27, 33, 39.3.2: 1, 2, 5, 8, 11,13, 16, 21, 25, 26

Here are two problems that explicitly connect ideas from sections 3.1-3.2 with linear algebra concepts from Math 2270

w4.1) Consider the 3rd order homogeneous linear differential equation for y x y x = 0

and let W be the solution space.a) Use successive antidifferentiation to solve this differential equation. Interpret your results using vector space concepts to show that the functions y0 x = 1, y1 x = x, y2 x = x2 are a basis for W . Thus the dimension of W is 3.

b) Show that the functions z0 x = 1, z1 x = x 2, z2 x =12

x 2 2 are also a basis for W . Hint:

If you verify that they solve the differential equation and that they're linearly independent, they will automatically span the 3-dimensional solution space and therefore be a basis.c) Use a linear combination of the solution basis from part b, in order to solve the initial value problem below. Notice how this basis is adapted to initial value problems at x0 = 2, whereas for an IVP at x0 = 0 the basis in a would have been easier to use.

y x = 0y 2 = 7

y 2 = 13y 2 = 5.

w4.2) Consider the three functions

y1 x = cos 2 x , y2 x = sin 2 x , y3 x = sin 2 x6

.

a) Show that all three functions solve the differential equationy 4 y = 0 .

b) The differential equation above is a second order linear homogeneous DE, so the solution space is 2 dimensional. Thus the three functions y1, y2, y3 above must be linearly dependent. Find a linear dependency. (Hint: use a trigonometry addition angle formula.)c) Exhibit a basis for the solutions space to the homogeneous differential equation in a.d) Find by inspection and trial, particular solutions y x to each of the two non-homogeneous differential equations

y 4 y = 28, y 4 y = 16 x Hint: one of them could be a constant, the other could be a multiple of x .e) Use superposition (linearity) and your work from c,d to find the general solution to the non-homogeneous differential equation

Page 8: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

y 4 y = 28 16 x .f) Solve the initial value problem, using your work above:

y 4 y = 28 16 x y 0 = 0

y 0 = 0 .

w4.3) Runge-Kutta is based on Simpson's rule for numerical integration. Simpson's rule is based on the fact that for a subinterval d, d h of length h , the parabola y = p x which passes through the points

d, y0 , dh2

, y1 , d h, y2 has integral

d

d h

p x dx = h6

y0 4 y1 y2 .

a) The integral approximation above follows from one on the interval 1, 1 by an affine change of variables. So first consider the interval 1, 1 . We wish to find a parabolic function on this interval, which we will denote by q x :

q x = a x2 b x c with unknown parameters a, b, c. We want q 1 = y0, q 0 = y1, q 1 = y2 . This gives 3 equations in 3 unknowns, to find a, b, c in terms of y0, y1, y2 , namely

q 0 = y1 = cq 1 = y2 = a b c

q 1 = y0 = a b c.Find a, b, c. (See left-hand diagram below.)

b) Compute 1

1q x dx for the values of a, b, c you find in part a, and verify the equality

1

1

q x dx =13

y0 4y1 y2

Now, the formula for general interval follows from a change of variables, as indicated below. Verify that the indicated substitution yields the claimed formula.

Page 9: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

Side Remark: If you've forgotten, or if you never talked about Simpson's rule in your Calculus class, here's how it goes: In order to approximate the definite integral of f x on the interval a, b , you

subdivide a, b into n subintervals of width x =b a

n= h . Then add the midpoints of each

subinterval. Label these x values (including midpoints) as

x0 = a, x1 = ah2

, x2 = a h, x3 = x03 h2

, x4 = x0 2 h, ... x2 n = x0 2nh = b ,

with corresponding y values yi = f xi , i = 0,...2 n . On each successive pair of intervals x2 k, x2 k 1 use the parabolic estimate

x2 k

x2 k

h

f u du h6 f x2 k 4 f x2 k 1 f x2 k 2 =

h6 y2 k 4 y2 k 1 y2 k 2

above, estimating the integral of f by the integral of the interpolating parabola on the subinterval. This yields the very accurate (for large enough n) Simpson's rule formula

a

bf x dx

h6 y

04 y

1y2

y2

4 y3

y4

... y2 n 2

4 y2 n 1

y2 n

,

i.e.

a

bf x dx

b a6 n y

04 y

12 y

24 y

32 y

4... 2 y

2 n 24 y

2 n 1y2 n

.

http://en.wikipedia.org/wiki/Simpson's_rule

Page 10: of thiskorevaar/2280spring20/jan29post.pdfWe'll look over the next homework assignment and illustrate one of the "famous numbers" custom problems which you will solve numerically,

> >

(1)(1)

> >

w4.4) (Famous numbers revisited, section 2.6, page 135, of text). The mathy numbers e, ln 2 , can bewell-approximated using approximate solutions to differential equations. We illustrate this on Wednesday Jan 29 for e, which is y 1 for the solution to the IVP

y x = yy 0 = 1.

Apply Runge-Kutta with n = 10, 20, 40 ... subintervals, successively doubling the number of subintervals until you obtain the target number below - rounded to 8 decimal digits - twice in succession. We will do this in class for e, and you can modify that code if you wish, as part of the Matlab code that I will post to CANVAS.

a) ln 2 is y 2 , where y x solves the IVP

y x =1x

y 1 = 0 (since y x = ln x ).

b) is y 1 , where y x solves the IVP

y x =4

x2 1y 0 = 0

(since y x = 4 arctan x and arctan 1 =4

).

Note that in a,b you are actually "just" using Simpson's rule from Calculus, since the right sides of these DE's only depend on the variable x and not on the value of the function y x . For reference:

Digits 16 : #In Maple it's easy to specify the number of working digits for calculations. evalf e ; #evaluate the floating point of e evalf ; evalf ln 2 ;

2.7182818284590453.1415926535897930.6931471805599453

w4.5 ) (Extra credit) Consider the following differential equation, which could be modeling the velocity in a linear drag problem for a falling object, if we choose "down" to be the positive direction:

v t = 9.8 .2 v.a) Find the terminal velocityb) Solve the IVP for this differential equation with inititial condition v 0 = 0. Verify that your solution limits to the terminal velocity.c) Modify the Matlab code posted on CANVAS to do the following: (i) Find the Euler, Improved Euler, and Runge Kutta numerical approximations to the solution of the IVP with n = 10 time steps, on the time interval 0 t 5. (ii) Create a single display which contains the slope field for this differential equation in the box 0 t 5, 0 v 50; scatter plots of the Euler, Improved Euler and Runge Kutta approximations; and a plot of the actual solution.