class33 - PHYS 5900 Class 33 (11/20/2009Fri) Zi-Wei Lin...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
PHYS 5900 Class 33 (11/20/2009Fri) Zi-Wei Lin Example4 If a Mathematica operation takes a long time, it may be time to think how to make it more efficient: In[1]:= v @ x_, y_, nmax_ D : = 16 π 2 n = 1 nmax m = 1 nmax Sin @H 2n 1 L x D Sin @H 2m 1 L y D H 2n 1 L H 2m 1 L In[2]:= Plot3D @ v @ x, y, 10 D , 8 x, 0, Pi < , 8 y, 0, Pi < , PlotRange 8 0.9, 1.1 <D êê AbsoluteTiming Out[2]= : 7.7656250, > In[3]:= Plot3D @ v @ x, y, 20 D , 8 x, 0, Pi < , 8 y, 0, Pi < , PlotRange 8 0.9, 1.1 <D êê AbsoluteTiming Out[3]= : 23.3281250, >
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In[4]:= Plot3D @ v @ x, y, 100 D , 8 x, 0, Pi < , 8 y, 0, Pi < , PlotRange 8 0.9, 1.1 <D êê AbsoluteTiming Out[4]= $Aborted In[5]:= Plot3D @ Evaluate @ v @ x, y, 20 D , 8 x, 0, Pi < , 8 y, 0, Pi < , PlotRange 8 0.9, 1.1 <D êê AbsoluteTiming Out[5]= : 0.5468750, > In[6]:= Plot3D @ Evaluate @ v @ x, y, 100 D , 8 x, 0, Pi < , 8 y, 0, Pi < , PlotRange 8 0.9, 1.1 <D êê AbsoluteTiming Out[6]= : 15.5937500, > In the above, wrapping Evaluate[ ] around the function makes the computation much faster. In[7]:= ClearAll @ v D 2 class33.nb
Background image of page 2
Procedural Programming in the form of name[arg1_,arg2_, . ..] := Module[ { localVar1, localVar2=value2, . .. }, procedure] Example : Given the Sun at the origin and a planet around it in the x-y plane, write a program that takes the planet's initial position and velocity as input to plot the planet' s orbit. Physics of the problem : The equations of motion are : xx'' H tt L =- GM rr 2 * xx rr ,yy ' ' H tt L =- GM rr 2 * yy rr , with rr = xx 2 + yy 2 ,M = mass of the Sun, G = the gravitational constant. Mathematica cannot solve this analytically : In[8]:= DSolve @8 xx'' @ tt D m− g m xx @ tt DêHH xx @ tt D ^2 + yy @ tt D ^2 L ^ H 3 ê 2 LL , yy'' @ tt D m− g m yy @ tt DêHH xx @ tt D ^2 + yy @ tt D ^2 L ^ H 3 ê 2 LL< , 8 xx, yy < ,tt D Out[8]= DSolve B: xx ±± @ tt D m− gmxx @ tt D I xx @ tt D 2 + yy @ tt D 2 M 3 ê 2 ,yy ±± @ tt D m− gmyy @ tt D I xx @ tt D 2 + yy @ tt D 2 M 3 ê 2 > , 8 xx, yy < ,tt F
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
symbols (G,M). 1 L Choose a length scale R i.e., xx = R * x, yy = R * y, rr = R * r, we then have : x'' H tt L =- GM R 3 * x K x 2 + y 2 O 3 , y'' H tt L =- GM R 3 * y K x 2 + y 2 O 3 . Choose 2 p R 3 GM as the unit of time, i.e., tt = 2 p R 3 GM * t : x'' H t L =- 4 p 2 x x 2 + y 2 3 ,y ' ' H t L =- 4 p 2 y x 2 + y 2 3 . Note : x, y, t are dimensionless. In Example2.1.9, we have plotted the orbit for a given initial condition, similar to the following: In[9]:= orbit = NDSolve @8 x'' @ t D m− 4 Pi^2 x @ t DêHH x @ t D ^2 + y @ t D ^2 L ^ H 3 ê 2 LL , y'' @ t D m− 4 Pi^2 y @ t DêHH x @ t D ^2 + y @ t D ^2 L ^ H 3 ê 2 LL , x @ 0 D == 1, y @ 0 D == 0, x' @ 0 D == − 3.1, y' @ 0 D == 6.3 < , 8 x, y < , 8 t, 0, 3 <D Out[9]= 88 x
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/25/2010 for the course PHYS 5900 taught by Professor Lin during the Fall '09 term at East Carolina University .

Page1 / 16

class33 - PHYS 5900 Class 33 (11/20/2009Fri) Zi-Wei Lin...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online