Docsity

Computational Physics using MATLAB, Notas de estudo de Engenharia Elétrica

Pré-visualização parcial do texto

Baixe Computational Physics using MATLAB e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

Computational

Physics using

MATLAB®

Table of Contents

Table of Figures

Figure 1. Uranium decay as a function of time .........................................................................

Figure 2. Simple Pendulum - Euler Method .............................................................................. 9

Figure 3. Simple Pendulum: Euler - Cromer method.............................................................. 10

Figure 4. Simple pendulum solution using Euler, Euler Cromer, Runge Kutta and Matlab

ODE45 solver. .......................................................................................................................... 15

Figure 5. The damped pendulum using the Euler-Cromer method ........................................ 17

Figure 6. Results from Physical pendulum, using the Euler-Cromer method, F_drive =0.5 19

Figure 7.Results from Physical pendulum, using the Euler-Cromer method, F_drive =1.2 ..

Figure 8. Results from Physical pendulum, using the Euler-Cromer method, F_drive =0.5 21

Figure 9. Results from Physical pendulum, using the Euler-Cromer method, F_Drive=1.2. 21

Figure 10. Increase resolution with npoints=15000.Results from Physical pendulum, using

the Euler-Cromer method, F_Drive=1.2 ................................................................................. 22

Figure 11. Poincare section (Strange attractor) Omega as a function of theta. F_Drive =1.2. 23

Figure 12. Bifurcation diagram for the pendulum ................................................................... 25

Figure 13. Variation of z as a function of time and corresponding strange attractor .............. 27

Figure 14. Simulation of Earth orbit around the Sun .............................................................. 29

Figure 15. Simulation of Earth orbit with time step of 0.05 .................................................... 31

Figure 16. Simulation of Earth orbit, initial y velocity of 4, time step is 0.002. ..................... 32

Figure 17.Simulation of Earth orbit, initial y velocity of 4, time step is 0.05 ......................... 33

Figure 18. Simulation of Earth orbit, initial y velocity of 8, time step is 0.002. 2500 points

and Runge Kutta method ......................................................................................................... 33

Figure 19.Plot for an initial y velocity of 8, dt is 0.05, npoints=2500. The Runge Kutta

Method is used here ................................................................................................................. 35

Figure 20. Orbit for a force law with β=2. The time step is 0.001 years. ................................ 37

Figure 21. Orbit for a force law with β=2.5. The time step is 0.001 years. .............................. 39

Figure 22. Orbit for a force law with β=3. .............................................................................. 40

Figure 23. Orbit orientation as a function of time ................................................................... 45

Figure 24. Calculated precession rate of Mercury ................................................................... 47

Figure 25. Simulation of solar system containing Jupiter and Earth ....................................

Figure 26. Simulation of solar system containing Jupiter and Earth. Jupiter mass is 10 X

actual value. ............................................................................................................................. 51

Figure 27.Simulation of solar system containing Jupiter and Earth. Jupiter mass is 1000 X

actual value, ignoring perturbation of the Sun. ....................................................................... 52

Figure 28.Motion of Hyperion. The initial velocity in the y direction was 1 HU/Hyperion

year. This gave a circular orbit. Note from the results that the tumbling is not chaotic under

these conditions. ...................................................................................................................... 56

Figure 29.Motion of Hyperion. The initial velocity in the y direction was 5 HU/Hyperion

year. This gave a circular orbit. Note from the results that the tumbling is chaotic under these

conditions. ............................................................................................................................... 59

Figure 30. Equipotential surface for geometry depicted in Figure 5.2 in the book ................ 62

Figure 31.Equipotential surface for hollow metallic prism with a solid metallic inner

conductor held at V=1. ............................................................................................................. 66

Figure 32.Equipotential surface for a finite sized capacitor. ................................................... 69

Figure 33. Equipotential contours near a finite sized capacitor. ............................................. 69

Figure 34.Equipotential surface in region of a simple capacitor as calculated using the SOR

code for a 60 X 60 grid. The convergence criterion was that the simulation was halted when

the difference in successively calculated surfaces was less than 10-5^ per site.......................... 73

Figure 35.Number of iterations required for Jacobi method vs L for a simple capacitor. The

convergence criterion was that the simulation was halted when the difference in successively

calculated surfaces was less than 10-5^ per site. ........................................................................ 74

Figure 36.Number of iterations required for SOR method vs L for a simple capacitor. The

convergence criterion was that the simulation was halted when the difference in successively

calculated surfaces was less than 10-5^ per site. ........................................................................ 74

Figure 37. Equipotential surface near a point charge at the center of a 20X20 metal box. The

Jacobi relaxation method was used. The plot on the right compares the numerical and

analytical (as obtained from Coulomb’s Law). ........................................................................ 77

Figure 38. Waves propagating on a string with fixed ends ..................................................... 79

Figure 39. Signal from a vibrating string and Power spectrum. Signal excited with Gaussian

pluck centred at the middle of the string and the displacement 5% from the end of the string

was recorded. ...........................................................................................................................

Figure 40. x^2 as a function of step number. Step length = 1. Average of 500 walks. Also

shown is a linear fit to the data. .............................................................................................. 88

Figure 41. x^2 as a function of step number. Step length = random value betwen +/-1.

Average of 500 walks. Also shown is a linear fit to the data................................................... 90

Figure 42. Calculated wavefunction using the shooting method. The wall(s) of the box are at

x=(-)1. The value of Vo used was 1000 giving ground-state energy of 1.23. Analytical value is

1.233. Wavefunctions are not normalised. .............................................................................. 93

Figure 43. Composition of wavepacket. ko = 500, x0=0.4, sigma^2=0.001. ......................... 94

Figure 44. Wavepacket reflection from potential cliff at x=0.6. The potential was V=0 for

x<0.6 and V=-1e6 for x>0.6. Values used for initial wavepacket were x_0=0.4,C=10,

sigma_squared=1e-3, k_0=500. Simulation used delta_x=1e-3, delta_t=5e-8. Time

progresses left to right. ............................................................................................................

Figure 45. Wavepacket reflection from potential wall at x=0.6. The potential was V=0 for

x<0.6 and V=1e6 for x>0.6. Values used for initial wavepacket were x_0=0.4,C=10,

sigma_squared=1e-3, k_0=500. Simulation used delta_x=1e-3, delta_t=5e-8. Time

progresses left to right. ............................................................................................................

Figure 46.Wavepacket reflection from potential cliff at x=0.5. The potential was V=0 for

x<0.5 and V=-1e3 for x>0.5. Values used for initial wavepacket were x_0=0.25,

y_0=0.5,C=10, sigma_squared=0.01, k_0=40. Simulation used delta_x=0.005,

delta_t=0.00001. ................................................................................................................... 102

Figure 47. Wavepacket reflection from potential wall at x=0.5. The potential was V=0 for

x<0.5 and V=1e3 for x>0.5. Values used for initial wavepacket were x_0=0.25,

y_0=0.5,C=10, sigma_squared=0.01, k_0=40. Simulation used delta_x=0.005,

delta_t=0.00001. ................................................................................................................... 103

1. Uranium Decay

% 1D radioactive decay % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi % Section 1.2 p

% Solve the Equation dN/dt = -N/tau

N_uranium_initial = 1000; %initial number of uranium atoms npoints = 100; %Discretize time into 100 intervals dt = 1e7; % time step in years tau=4.4e9; % mean lifetime of 238 U

N_uranium = zeros(npoints,1); % initializes N_uranium, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros

N_uranium(1) = N_uranium_initial; % the initial condition, first entry in the vector N_uranium is N_uranium_initial time(1) = 0; %Initialise time

for step=1:npoints-1 % loop over the timesteps and calculate the numerical solution N_uranium(step+1) = N_uranium(step) - (N_uranium(step)/tau)dt; time(step+1) = time(step) + dt; end % For comparison , calculate analytical solution t=0:1e8:10e9; N_analytical=N_uranium_initialexp(-t/tau); % Plot both numerical and analytical solution plot(time,N_uranium,'r',t,N_analytical,'b'); %plots the numerical solution in red and the analytical solution in blue xlabel('Time in years') ylabel('Number of atoms')

Figure 1. Uranium decay as a function of time

Note that the analytical and numerical solution are coincident in this diagram. It

uses real data on Uranium and so the scales are slightly different than those used in

the book.

0 1 2 3 4 5 6 7 8 9 10 x 10

9

100

200

300

400

500

600

700

800

900

1000

Time in years

Number of atoms

3.1.1 Solution using the Euler-Cromer method.

This problem with growing oscillations is addressed by performing the solution using

the Euler - Cromer method. The code is below

% Euler_cromer calculation of motion of simple pendulum % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3. %

clear; length= 1; %pendulum length in metres g=9.8; % acceleration due to gravity npoints = 250; %Discretize time into 250 intervals dt = 0.04; % time step in seconds omega = zeros(npoints,1); % initializes omega, a vector of dimension npoints X 1,to being all zeros theta = zeros(npoints,1); % initializes theta, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros theta(1)=0.2; % you need to have some initial displacement, otherwise the pendulum will not swing

for step = 1:npoints-1 % loop over the timesteps omega(step+1) = omega(step) - (g/length)theta(step)dt; theta(step+1) = theta(step)+omega(step+1)*dt; %note that % this line is the only change between % this program and the standard Euler method time(step+1) = time(step) + dt; end;

plot(time,theta,'r' ); %plots the numerical solution in red xlabel('time (seconds) '); ylabel('theta (radians)');

Figure 3. Simple Pendulum: Euler - Cromer method

0 1 2 3 4 5 6 7 8 9 10

-0.

-0.

-0.

-0.

-0.

0

time (seconds)

theta (radians)

3.1.2 Simple Harmonic motion example using a variety of numerical approaches

In this example I use a variety of approaches in order to solve the following, very simple,

equation of motion. It is based on Equation 3.9, with k and α =1.

I take 4 approaches to solving the equation, illustrating the use of the Euler, Euler

Cromer, Second order Runge-Kutta and finally the built in MATLAB®^ solver ODE23.

The solution using the built in MATLAB®^ solver ODE23 is somewhat less

straightforward than those using the other techniques. A discussion of the technique

follows.

The first step is to take the second order ODE equation and split it into 2 first order

ODE equations.

These are

Next you create a MATLAB®^ function that describes your system of differential

equations. You get back a vector of times, T, and a matrix Y that has the values of

each variable in your system of equations over the times in the time vector. Each

column of Y is a different variable.

MATLAB®^ has a very specific way to define a differential equation, as a function that

takes one vector of variables in the differential equation, plus a time vector, as an

argument and returns the derivative of that vector. The only way that MATLAB®

keeps track of which variable is which inside the vector is the order you choose to use

the variables in. You define your differential equations based on that ordering of

variables in the vector, you define your initial conditions in the same order, and the

columns of your answer are also in that order.

In order to do this, you create a state vector y. Let element 1 be the vertical

displacement, y1, and element 2 is the velocity,v. Next, we write down the state

equations, dy1 and dy2. These are

dy1=v;

dy2=-y

Next, we create a vector dy, with 2 elements, dy1 and dy2. Finally we call the

MATLAB®^ ODE solver ODE23. We take the output of the function called my_shm.

We perform the calculation for time values range from 0 to 100. The initial velocity is

0, the initial displacement is 10. The code to do this is here

[t,y]=ode45(@my_shm,[0,100],[0,10]);

Finally, we need to plot the second column of the y matrix, containing the

displacement against time. The code to do this is

Here are the functions to do the individual calculations

% Simple harmonic motion - Euler method % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3. % Equation is d2y/dt2 = -y

function [time,y] = SHM_Euler (initial_displacement);

npoints = 2500; %Discretize time into 250 intervals dt = 0.04; % time step in seconds

v = zeros(npoints,1); % initializes v, a vector of dimension npoints X 1,to being all zeros y = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros y(1)=initial_displacement; % need some initial displacement

% Euler solution for step = 1:npoints-1 % loop over the timesteps v(step+1) = v(step) - y(step)dt; y(step+1) = y(step)+v(step)dt; time(step+1) = time(step) + dt; end;

% Simple harmonic motion - Euler Cromer method % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3. % Equation is d2y/dt2 = -y

function [time,y] = SHM_Euler_Cromer (initial_displacement);

npoints = 2500; %Discretize time into 250 intervals dt = 0.04; % time step in seconds

v = zeros(npoints,1); % initializes v, a vector of dimension npoints X 1,to being all zeros y = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros y(1)=initial_displacement; % need some initial displacement

% Euler Cromer solution for step = 1:npoints-1 % loop over the timesteps v(step+1) = v(step) - y(step)dt; y(step+1) = y(step)+v(step+1)dt; time(step+1) = time(step) + dt; end;

% Simple harmonic motion - Second order Runge Kutta method % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3.

% Equation is d2y/dt2 = -y function [time,y] = SHM_Runge_Kutta(initial_displacement);

% 2nd order Runge Kutta solution

npoints = 2500; %Discretize time into 250 intervals dt = 0.04; % time step in seconds

v = zeros(npoints,1); % initializes v, a vector of dimension npoints X 1,to being all zeros y = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros y(1)=initial_displacement; % need some initial displacement

v = zeros(npoints,1); % initializes v, a vector of dimension npoints X 1,to being all zeros y = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros v_dash = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros y_dash = zeros(npoints,1); % initializes y, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros y(1)=10; % need some initial displacement

for step = 1:npoints-1 % loop over the timesteps

v_dash=v(step)-0.5y(step)dt; y_dash=y(step)+0.5v(step)dt;

v(step+1) = v(step)-y_dashdt; y(step+1) = y(step)+v_dashdt; time(step+1) = time(step)+dt; end;

% Simple harmonic motion - Built in MATLAB ODE45 method % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3. % Equation is d2y/dt2 = -y

function dy = SHM_ODE45_function(t,y);

% y is the state vector

y1 = y(1); % y1 is displacement v = y(2); % y2 is velocity

% write down the state equations

dy1=v; dy2=-y1;

% collect the equations into a column vector, velocity in column 1, % displacement in column 2

3.2 Solution for a damped pendulum using the Euler-Cromer method.

This solution uses q=

% Euler_cromer calculation of motion of simple pendulum with damping % by Kevin Berwick, % based on 'Computational Physics' book by N Giordano and H Nakanishi, % section 3. %

clear; length= 1; %pendulum length in metres g=9.8; % acceleration due to gravity q=1; % damping strength

npoints = 250; %Discretize time into 250 intervals dt = 0.04; % time step in seconds

omega = zeros(npoints,1); % initializes omega, a vector of dimension npoints X 1,to being all zeros theta = zeros(npoints,1); % initializes theta, a vector of dimension npoints X 1,to being all zeros time = zeros(npoints,1); % this initializes the vector time to being all zeros

theta(1)=0.2; % you need to have some initial displacement, otherwise the pendulum will not swing

for step = 1:npoints-1 % loop over the timesteps omega(step+1) = omega(step) - (g/length)theta(step)dt-qomega(step)dt; theta(step+1) = theta(step)+omega(step+1)*dt;

% In the Euler method, , the previous value of omega % and the previous value of theta are used to calculate the new values of omega and theta. % In the Euler Cromer method, the previous value of omega % and the previous value of theta are used to calculate the the new value % of omega. However, the NEW value of omega is used to calculate the new % theta % time(step+1) = time(step) + dt; end;

plot(time,theta,'r' ); %plots the numerical solution in red xlabel('time (seconds) '); ylabel('theta (radians)');

Figure 5. The damped pendulum using the Euler-Cromer method

time (seconds)

theta (radians)

% In the Euler Cromer method, the previous value of omega % and the previous value of theta are used to calculate the the new value % of omega. However, the NEW value of omega is used to calculate the new % theta % time(step+1) = time(step) + dt; end;

plot (theta,omega,'r' ); %plots the numerical solution

xlabel('theta (radians)'); ylabel('omega (seconds)');

Figure 6. Results from Physical pendulum, using the Euler-Cromer method, F_drive =0.

0 10 20 30 40 50 60

-0.

-0.

-0.

-0.

0

1

time (seconds)

theta (radians)

Figure 7.Results from Physical pendulum, using the Euler-Cromer method, F_drive =1.

time (seconds)

theta (radians)