solving odes uc berkeley fall 2004, e77 pack/e77 copyright 2005, andy packard. this work is licensed...
TRANSCRIPT
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.
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?
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.
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)
Example: Ordinary Differential Equations
Consider a simple example
Exact Solution is easy:
Consider a less-simple example
Exact solution is (integrating factor)
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.
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)
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
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
Converting to first-order form
Starting with
Define
Write each in terms of all of the
Two, 2nd-order coupled ODEs
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!
Beam Theory: convert to first-order form
Governing equation is
With boundary conditions
Define
reformulated
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?
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
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.
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)
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)
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
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
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).’
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);