0% found this document useful (0 votes)
58 views20 pages

Computational Lab in Physics: Solving Differential Equations

The document discusses using numerical methods to solve differential equations that commonly arise in physics simulations, as finding analytical solutions can be difficult. It provides examples of ordinary differential equations from classical mechanics and electromagnetism, and describes the Euler method and modified Euler method for numerically approximating solutions to differential equations in a step-wise manner. The document also shows examples of implementing these numerical integration techniques to solve the equations for a 1/x problem and a simple harmonic oscillator.

Uploaded by

benefit187
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views20 pages

Computational Lab in Physics: Solving Differential Equations

The document discusses using numerical methods to solve differential equations that commonly arise in physics simulations, as finding analytical solutions can be difficult. It provides examples of ordinary differential equations from classical mechanics and electromagnetism, and describes the Euler method and modified Euler method for numerically approximating solutions to differential equations in a step-wise manner. The document also shows examples of implementing these numerical integration techniques to solve the equations for a 1/x problem and a simple harmonic oscillator.

Uploaded by

benefit187
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Computational Lab in Physics:

Solving Differential Equations.

Steven Kornreich
www.beachlook.com

Frequently in Physics, we are faced with solving a differential equation. Finding


solutions analytically can be difficult. We can use numerical methods to give us a
good idea of the solution in many cases.
Logistic Map, Cobweb diagram

2
Logistic Map Discussion

 As the control parameter r


is increased in the interval 3
< r < 3.5699456…
 there is “period doubling”
 “pitchfork bifurcations”
increase the number of m-
cycle periods by a factor of 2
at each bifurcation.
 Fixed points obey the
equation x= f(m)(x)
 f(m)(x) = f(f(f(…f(x)…))), m-
times
3
Examples of differential eqs.
r r
 Newton’s Law: d r
F m
dt
 Maxwell’s Eqs: r r 
 Gauss’s Law  E 

 No monopoles r r
 Faraday’s Law  B 
r r B
 Ampere-Maxwell  E  
t
r r r E
 B   J   
t

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

 Homogeneous: no function of the


independent variable appears by itself.
 Physics: 2nd order eqs: need two initial
conditions.

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  vt  x t  t  x t  t v t
dt
dv
  x  v   xt  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

 Much better agreement with Modified Euler Method. 16


Modified Euler Method for SHO
 Need to calculate x and v at
midpoint.
 t  t
v  v  t   v t  x t
 
 t  t
x  x  t   x t  v t
 
x t  t  x t  t v
v t  t  v t  t x

17
Simple vs Modified Euler: SHO

 Works better!
18
For smaller step size, can still have
problems…

 With step size = 0.3, obvious


problems as t grows.
19
Homework
 From Chapter 15
 Exercise (5): Duffing Oscillator
 Damped, nonlinear (pendulum at NLO), driven oscillator.
 Your program should make a plot of the resulting equation x
vs t as well as the v vs x.
 Use Modified Euler Method, (ignore the last sentence on page
201)
 Instead of instructions in page 202, you can use a TGraph for
this, as in the examples shown in lecture.
 Extra Problem (Not in Book)
 Solve the full pendulum equation of motion (no
approximations), using the modified Euler method.
 Use initial angle = π/2, initial angular speed = 0. Take gravity
= 9.81 m/s2 and l=1 m.
 Make a plot of angle vs time.
 In that same plot, compare the plot to the SHO of frequency
g/l and amplitude π/2 which starts at the same angle and
same angular speed.

20

You might also like