solving odes uc berkeley fall 2004, e77 pack/e77 copyright 2005, andy packard. this work is licensed...

21
Solving ODEs UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~ pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative

Upload: willis-malone

Post on 23-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Solving ODEsUC Berkeley

Fall 2004, E77http://jagger.me.berkeley.edu/~pack/e77

Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to

Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Page 2: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Numerical Soln of Differential Equations

Recall the population problem you worked out in HW–Independent variable: time.–Dependent variables: eggs, larvae, adults–Governing rule for their “one-step” evolution

An initial condition (values for e0, v0 and a0) and a for loop leads to the solution (over time).

Here, the variables evolve discretely in time. This is called a discrete-time equation. What if the variables evolve continuously in time?

Page 3: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Differential Equations

Applying the “conservation laws” that make up a scientific field often leads to differential equations. Examples

–“rate of change of linear momentum of a mass particle” equals the forces applied to the particle

–“rate of change of internal energy of a system” equals the net flow of energy into the system

–“rate-of-change of concentration of a molecule” equals a sum of terms proportional to the products of concentrations of other molecules

Rate-of-change means derivative. We’ll use to denote derivative of y with respect to its argument.

If the argument is time, convention is to use to denote derivative.

Page 4: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Ordinary Differential Equations

Initially, use the following notation–Independent variable, x–Dependent variable, y

• y is a function of x, y(x) (notate the derivative as y´(x))

–Known function, f

Given information–Initial condition

• The value of y(0) is known, say y0

–The “governing equation,” which gives y´ in terms of y

Goal: find “solution”, namely a function y which satisfies–The initial condition, and–The governing equation for all x

Fact: For most right-hand-sides, there exists a unique soln

The rate-of-change of y at x depends only on the value of x, and the value of y(x)

Page 5: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Example: Ordinary Differential Equations

Consider a simple example

Exact Solution is easy:

Consider a less-simple example

Exact solution is (integrating factor)

Page 6: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Ordinary Differential Equations

An “initial value” problem–the initial value of y as known/given–the goal is to compute y for “later” (more positive) values of x.

A “final value” problem–the final value of y as known/given–the goal is to compute y for “earlier” (less positive) values of x.

A “boundary value” problem–the initial value of some components of y is known/given–the final value of some components of y is known/given–the goal is to compute y for the intermediate values of x.

Initial value and final value problems are basically the same. Boundary value problems are different.

Page 7: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Ordinary Differential Equations

Initially, use the following notation–Independent variable, x

–Many dependent variables, y1, y2, …, yn

–Known function, f

Given information–Initial condition

• The value of y(0) is known, say y1,0, y2,0, …, yn,0

–The governing equation, which gives y´ in terms of y

The rate-of-change of y at x depends only on the value of x, and all of the values of y(x)

Page 8: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Coupled, 1st-order Ordinary DiffEq

Use the following terminology–Coupled, since there are n dependent variables, and their

values influence the rate-of-change of each other–1st order, since only 1 derivative appears–Ordinary, since there is only one independent variable

Write concisely

Page 9: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Satellite orbiting Earth

The motion of a point-mass, m, orbiting a stationary massive object M is governed by

Accelerations in radial and tangential directions

Forces in radial and tangential directions

Page 10: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Converting to first-order form

Starting with

Define

Write each in terms of all of the

Two, 2nd-order coupled ODEs

Page 11: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Beam Theory (CE 130)

Consider a beam.

Under the influence of a distributed load...

The relationship between the load, w(x), and the transverse deflection v(x) is

Boundary-value problem!

w(x), distributed load, in Newtons/meter, function of location

it deflects!

Page 12: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Beam Theory: convert to first-order form

Governing equation is

With boundary conditions

Define

reformulated

Page 13: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Euler Approximation to solution

Write the system of equations

Here, f maps a scalar (x), and an n-by-1 vector (y) into a n-by-1 vector.

Consider an IVP. The value of y at x0 is given as y0. What is the solution for x>x0?

Page 14: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Euler Approximation to solution

At x0, the governing equation implies

Use forward finite difference approximation to the left-hand side (ie., the derivative)

Equate these, giving the approximate value for y(x0+h)

known

Page 15: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Euler Approximation to solution

The Euler approximation to the solution is obtained by repeating this process using

–the forward finite difference approximation to the derivative, and

– the approximate solution as the value of y at x0+h.

The forward finite difference says

But we don’t know these.

Approximate value is available via the previous step,

Use this approximate value, and continue in this fashion.

Page 16: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Euler Approximation to solution

Let yE denote the Euler solution. Specifically, denotes the approximate solution at the kth “step”.

Using a stepsize of h, the Euler approximate solution is evolved (as a discrete equation)

Page 17: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Euler psuedo-code

function [x,y] = eulersolve(fh,xspan,y0,h)

n = length(y0); % assume column vector

x = xspan(1):h:xspan(2);

M = length(x);

y = zeros(n,M);

y(:,1) = y0;

xk = xspan(1);

for k=1:M-1

yk = y(:,k);

y(:,k+1) = yk + h*feval(fh,xk,yk);

xk = xk + h;

end

y = y.’; % matlab convention(see ode23/45)

Page 18: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Adaptive StepSizes

In some algorithms, called adaptive, the stepsize is–adjusted automatically, –based on the magnitude of f.

In this case, the stepsize is written hk and the iteration is

More details, as usual, in Math 128A

Page 19: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Higher Order Methods (Runge-Kutta)

In the Euler method, the approximate solution at the next step is based on evaluating the function f at one point,

In Runge-Kutta algorithms, the approximate solution at the next step is based on evaluating the function f at several points.

More details, as usual, in Math 128A

Page 20: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

ode23, ode45: Runge-Kutta

Syntax is

[x,y] = ode45(fh,xspan,y0)

The input arguments are–fh is a function handle to the function describing the governing

equation. Called repeatedly, by ode45, with two arguments,

feval(fh,xvalue,yvalue). Should return a column vector representing y´

–xspan, 1-by-2 row vector, [X0 Xfinal]–y0, initial condition, n-by-1 column vector

The output arguments are–x, M-by-1 vector of x-values where approx soln is computed

–y, M-by-n “solution”.

–Approximate solution at x(k)is y(k).’

Page 21: Solving ODEs UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative

Example: Ordinary Differential Equations

Simple example

First, write

function yp = cclin(x,y)

yp = -2*y;

Complex example

function yp = ncclin(x,y)

yp = (-0.75+sin(x))*y;

Then

>> [xS,yS] = ode45(@cclin,[0 5],1.5);

>> [xC,yC] = ode45(@ncclin,[0 10],1.5);