Professional Documents
Culture Documents
Steven Kornreich
www.beachlook.com
2
Logistic Map Discussion
d g
Pendulum:
dt l
h
Schrodinger Eq:
ih V
t m
4
Ordinary Differential Eqs.: 1-variable
Order : highest derivative
Linear: each term has only 1st power of
function and derivatives
no 2nd or higher powers
no cross-terms
5
Simple and Driven Harmonic oscillator
SHO d xt
m kx t
dt
Driven Harmonic
d xt
Oscillator m kx t F t
dt
6
Euler Method: Simplest way
Approximate derivative: y x h y x
y x
x h x
Rewrite
Taylor expansion up to y x h y x hy x
linear term
If we know
y(x0), h, y’(x0)
then we know y(x0+h)
Drawback:
Not very precise
Error propagation is an
issue
7
Example: xy’+y=0
Analytic Numerical
solution: solution:
dy dy y
x y
dx dx x
dy dx y x
y x h y x h
y x x
y x A Start from a
y
C point x0, going
x up to xf, and use
step size h. 8
Comparison of Numerical/Analytic
void eulerExample1() {
TF1* analytic = new TF1("analytic","1/x",0.1,5);
analytic->SetNpx(1000);
TCanvas* euCnv1 = new TCanvas("euCnv1","ODE",500,500);
analytic->Draw();
gPad->SetLogy();
double xmin=0.1;
double xmax=5.0;
double step=0.01;
int Nsteps=static_cast<int>((xmax-xmin)/step);
const int maxPoints = 1000;
if (Nsteps>maxPoints) {
cout << "Need to declare array of larger size: " << Nsteps << endl;
return;
}
cout << "Nsteps " << Nsteps << endl;
double yVal[maxPoints];
double xVal[maxPoints];
xVal[0] = xmin;
yVal[0] = 10;
for (int i=0; i<Nsteps; ++i) {
xVal[i+1]=xVal[i]+step;
yVal[i+1]=yVal[i]-step*yVal[i]/xVal[i];
}
TGraph* numerical = new TGraph(Nsteps,xVal,yVal);
numerical->SetLineColor(2);
numerical->Draw("L");
return;
}
9
Harmonic oscillator solution
d xt
m kx t
Transform 2nd order eq. dt
into two coupled linear dx
m mv
equations: dt
Solution, with initial dv
m kx
conditions dt
v(0)=0
x(0)=A
xt A t
v t A t
Set m=k=1 for
simplicity.
k
m
10
Numerical Solution
Equations become:
dx
v x vt x t t x t t v t
dt
dv
x v xt v t t v t t x t
dt
11
ROOT macro for SHO with Simple
Euler
cout << "Nsteps " << Nsteps << endl;
double vVal[maxPoints];
double xVal[maxPoints];
void eulerExample2() { double tVal[maxPoints];
TF1* analytic = new tVal[0] = 0;
TF1("analytic","sin(x+TMath::Pi()/2.0)",0.1,20); xVal[0] = 1;
analytic->SetNpx(1000); vVal[0] = 0;
TCanvas* euCnv2 = new TCanvas("euCnv2","SHO- for (int i=0; i<Nsteps; ++i) {
Euler",500,500);
tVal[i+1]=tVal[i]+step;
analytic->Draw();
xVal[i+1]=xVal[i]+step*vVal[i];
vVal[i+1]=vVal[i]-step*xVal[i];
}
double tmin=0.0;
TGraph* numericalX = new
double tmax=20.0; TGraph(Nsteps,tVal,xVal);
double step=0.01; numericalX->SetLineColor(2);
int Nsteps=static_cast<int>((tmax-tmin)/step); numericalX->Draw("L");
const int maxPoints = 2001; TGraph* numericalV = new
if (Nsteps>maxPoints) { TGraph(Nsteps,tVal,vVal);
cout << "Need to declare array of larger size: " << numericalV->SetLineColor(4);
Nsteps << endl; numericalV->Draw("L");
return; return;
} }
12
Result of Simple Euler Method for SHO
Analytic solution
x(t)=sin(t+π/2) in
BLACK
Numerical solutions:
x(t) in RED
v(t) in BLUE
Problem:
Amplitude of numerical
solution grows!
13
Spring is gaining energy?!
As particle propagates from say, x=0 to x=1:
Force is evaluated at x(ti)
Average displacement of spring during propagation:
x(ti+∆t/2).
Velocity value used is also larger than average
velocity over the displacement.
Result:
restoring force is weaker than it should be.
Acts on the particle a shorter amount of time.
Displacement overshoots the true maximum.
When returning to zero, force is overestimated…
Cycle repeats.
x(ti+∆t/2)
xi xi+1 x
14
Modified Euler Method
Simple Euler:
derivative at the beginning of interval
y x h y x hy x
Modified Euler
derivative at the midpoint of interval
h
y x h y x hy x
dy y
For first problem: dx x
h h
y x y x y x
y x h
y x h y x h 15
x h
Simple vs Modified Euler: 1/x
17
Simple vs Modified Euler: SHO
Works better!
18
For smaller step size, can still have
problems…
20