Natrix’s Weblog

March 29, 2009

Differential Equations: Epilogue

Filed under: Uncategorized — natrix @ 1:44 am

This is one of the few college level classes I have taken where I was able to both enjoy the structure of this course and learn far more than just the core mathematics goals. Although vector fields, Euler’s method, phase space, initial conditions, and the concept of domain transforms were the main objective of this course, there was much more offered. Over the course of the semester I was able to increase my proficiency with MATLAB in addition to an introduction to the LaTeX typesetting system. This course also provided an excellent HTML refresher course. I would recommend this class to anyone with an interest in calculus, engineering, or technical writing.

December 2, 2008

Fourth 3-week block task

Filed under: Uncategorized — natrix @ 4:59 pm

The Laplace Transform and The Big Picture

The Laplace Transform is a method discovered by Pierre-Simon Laplace (1749 – 1827), a French mathematician and astronomer. The transform converts an ordinary differential equation in one domain into a much easier to solve algebraic equation in a different domain, then the solved algebraic equation is then converted back into the original domain.

The definition of the Laplace Transform is

F(s) = \mathscr{L}[f(t)] = \int_{-\infty}^{0}e^{-st}f(t)dt

for t \ge 0, real numbers only.

Example 1, solve the function

\frac{dx}{dt} + x = e^{-t}

for x(t)

the Laplace Transform is applied on it yeilding \mathscr{L}[x] = \frac{1}{(s+1)^2} + \frac{a}{s+1} where a = x(0)

Using a table, each sum is converted back into the t domain, yeilding x(t) = e^{-t}(t + a)

Original function

\frac{dx}{dt} + x = e^{-t}

using formula \mathscr{L}[\frac{dx}{dt}] = -x(0) + s\mathscr{L}[x]

through substitution

-x(0) + s\mathscr{L}[x] + \mathscr{L}[x] = \mathscr{L}[e^{-t}]

s\mathscr{L}[x] + \mathscr{L}[x] = \mathscr{L}[e^{-t}] + x(0)

factoring \mathscr{L}[x](s + 1) = \mathscr{L}[e^{-t}] + x(0)

using \mathscr{L}[e^{-t}] = \frac{1}{s+1}

\mathscr{L}[x](s + 1) = \frac{1}{s+1} + x(0)

\mathscr{L}[x] = \frac{\frac{1}{s+1}}{s+1} + \frac{x(0)}{s+1}

\mathscr{L}[x] = \frac{1}{(s+1)^2} + \frac{x(0)}{s+1}

After we solve for \mathscr{L}[x] we plug the solution into Matlab using the “ilaplace” function which solves for the inverse Laplace transform

>> syms a s t; ilaplace(a/(s+1)+1/((s+1)^2),s,t)

ans =

t/exp(t) + a/exp(t)

Simplifying the above answer

x(t) = \frac{t}{e^t} + \frac{a}{e^t} = te^{-t} + ae^{-t}

after factoring

x(t) = e^{-t}(t + a)

image001

t x(t), a = -1 x(t), a = 0 x(t), a = 1
0 =(2.7818^-A2)*(A2-1) =(2.7818^-A2)*(A2) =(2.7818^-A2)*(A2+1)
0.1 =(2.7818^-A3)*(A3-1) =(2.7818^-A3)*(A3) =(2.7818^-A3)*(A3+1)
0.2 -0.651965788 0.162991447 0.977948681
0.3 -0.514991747 0.220710749 0.956413244
0.4 … … …

Example 2, solve the equation

\frac{d^2x}{dt^2} -2\frac{dx}{dt} +5x = 0

Where \mathscr{L}[x(t)] = \frac{-3a}{-s^2+2s-5} - \frac{b}{-s^2+2s-5} where a = x(0) and b = \frac{dx}{dt}(0)

October 23, 2008

Third 3-week block task

Filed under: Uncategorized — natrix @ 7:12 pm

Overview:

In this third 3 week block, systems of linear differential equations will be the focus. The definition, possible solution forms, and some graphical examples will be presented and discussed.

Part 1: Systems of linear differential equations

A system of linear differential equations is a set of linked formulas in the form

\frac{dx}{dt}=ax+by

\frac{dy}{dt}=cx+dy

Where a, b, c, d are constants. In other systems of linear DE’s, these constants can be variables with respect to t but for this project they will be constants.

Part 2: The linear transformation, and eigenvalues

A linear transformation is a function between two vector spaces where linear interpolation is preserved. In other words, by knowing the characteristics of a vector (the magnitude and direction) at any two points, it is possible to interpolate any vector on a line connecting the two points. For example, on the line AB, the vector originating at point C can be accurately modeled by knowing the vectors at points A and B.

linearstuff

Part 3: The three possible general vector field cases, and invariant lines

There are three possible general shapes for the vector fields for a system of two linked linear DE’s – eigenvalues with real roots (1, -2), complex roots (1\pm i), and imaginary roots (\pm i).

Case 1a, two real roots:

Original function:

T[(x,y)] = (x+y, x)

Solving for eigenvalues by hand:

x+y = \lambda x

x = \lambda y, y = \frac{1}{\lambda} x

\lambda y + y = \lambda^2 y

\lambda = 1 + \frac{1}{\lambda}, \frac{1}{\lambda} = \lambda - 1

\lambda^2 - \lambda -1 = 0

…after solving for the roots using the quadratic formula…

\frac{1 \pm \sqrt{5}}{2}

Therefore, the equation of the “creases” is

y = ({\frac{1 \pm \sqrt{5}}{2}})^{-1} x

y = 0.618 x and y = -1.618 x

Solving for eigenvalues using Matlab (the easy way):

Eiguenvalues can be solved in Matlab by entering a, b, c, and d into a matrix, then performing the eig() function upon the matrix. The matrix is entered with the form

A = \begin{vmatrix}1&1\\ 1&0 \end{vmatrix}

>> A = [1 1; 1 0]

A =

1 1
1 0

>> eig(A)

ans =

-0.6180
1.6180

-0.6180 and 1.6180 match the values calculated by hand, which is a good thing.

Mathametica code:

Needs["VectorFieldPlots`"];

A1 = VectorFieldPlot[{x + y, x}, {x, -1, 1}, {y, -1, 1}, PlotPoints -> 20, ScaleFactor > 0.2, Axes -> True];
A2 = Plot[-x*(1 + Sqrt[5])/2, {x, -1, 1}];
A3 = Plot[x*(1 - Sqrt[5])/2, {x, -1.5, 1.5}];
Show[A1, A2, A3]

Here, the invariant lines at y = -0.618x and $\latex y = 1.618x$ are the “creases” where vectors lying on the on these lines are exactly parallel to them.

It is possible to have two real roots which are both positive or negative – a solution with two positive roots

Case 1b, one real root

Using the equation T[(x,y)] = (x, y)

we can solve for the eigenvalues using the following Matlab code

>> a = [1 0; 0 1]

a =

1 0
0 1

>> eig(a)

ans =

1
1

Needs["VectorFieldPlots`"];
VectorFieldPlot[{x, y}, {x, -1, 1}, {y, -1, 1}, PlotPoints -> 20,
ScaleFactor -> 0.2, Axes -> True]

onerealroot

Similarly, if the eigenvalue was -1 instead of 1, the vectors would be pointing inward instead of outward.

Case 1c, two positive real roots
T[(x,y)] = (3x+3y, x+3y)

>> a = [3 3; 1 3]; eig(a)

ans =

4.7321
1.2679

>>

Needs["VectorFieldPlots`"];
A1 = VectorFieldPlot[{3*x + 3*y, x + 3*y}, {x, -1, 1}, {y, -1, 1},
PlotPoints -> 20, ScaleFactor -> 0.2, Axes -> True]
A2 = Plot[x*4.7321, {x, -1/4.7321, 1/4.7321}];
A3 = Plot[x*1.2679, {x, -1/1.2679, 1/1.2679}];
Show[A1, A2, A3]

twopositiveroots1

Something isn’t quite right here… the graph above needs work

Case 2, two complex roots:

\frac{dx}{dt} = -x+y

\frac{dy}{dt} = -x-y

T[(x,y)] = (-x+y, -x-y)

Eigen values in Matlab

A, C, D = -1

B = 1

>> A = [-1 1; -1 -1]; eig(A)

ans =

-1.0000 + 1.0000i
-1.0000 – 1.0000i

So the eiguen values for this system of linear DE’s are -1 \pm i
Graphing the vector field in Mathematica…

Needs["VectorFieldPlots`"];
VectorFieldPlot[{-x + y, -x - y}, {x, -1, 1}, {y, -1, 1},
PlotPoints -> 20, ScaleFactor -> 0.2, Axes -> True]

Case 3, two imaginary roots:

Calculating eiguenvalues in Matlab:

>> A = [0 1; -1 0]

A =

0 1
-1 0

>> eig(A)

ans =

0 + 1.0000i
0 – 1.0000i

Needs["VectorFieldPlots`"];
VectorFieldPlot[{y, -x}, {x, -1, 1}, {y, -1, 1}, PlotPoints -> 20,
ScaleFactor -> 0.2, Axes -> True]

case3

October 7, 2008

Second 3-week block task

Filed under: Uncategorized — natrix @ 7:34 pm

DRAFT

PART ONE: Excel for Euler’s method for several linked DE’s


The Lorenz Attractor

The first part of the second 3-week block task I worked on was the Lorenz attractor. In this case, the attractor is an entity in 3-dimensional space about which a particle “orbits” a chaotic pattern depending on what the time is set to. The exact path a particle travels with respect to time is extremely sensitive to initial conditions. For example, consider the following initial conditions:

Point A = (1,2,3.0000)

Point B = (1,2,3.0001)

Even with a tiny difference in initial conditions, the two particles only follow a similar path for a very short period of time.

Although the exact pattern is chaotic and difficult to predict, the behavior of the particle follows certain boundaries defined by the following equations:

\frac{dx}{dt} = \sigma (y - x)

\frac{dy}{dt} = x (\rho - z) - y

\frac{dz}{dt} = xy - \beta z

One of the easiest and quickest ways to obtain a rough understanding of how these three equations interact is to implement a Euler approximation in Excel.

In the table below, the values of \sigma, \rho, and \beta, have an enormous influence on the general shape of the attractor where \Delta t is the step used for the Euler approximation. The smaller the value used in this approximation, the more accurate the solution. Ideally \Delta t should be infinitely small to achieve the best possible accuracy, but this would require an infinite amount of computing power, so a compromise between simplicity and accuracy is needed. Using the “RAND()” function, we are able to setup “random” initial conditions which will later prove that the general shape of the attractor is not dependent on specific initial conditions. The values used in Table 1 were given in order to create an attractor which looks similar to the one at the top of this page.
x(t_1) = x(t_0) + \Delta t (\sigma y(t_0) - x(t_0))
y(t_1) = y(t_0) + \Delta t (x(t_0) (\rho - z(t_0)) - y(t_0))
z(t_1) = z(t_0) + \Delta t (x(t_0) y(t_0) - \beta z(t_0))

sigma	10	t		x(t)			y(t)				z(t)
rho	28	0		=RAND()			=RAND()				=RAND()
beta	=8/3	=C2+$B$4	=D2+$B$4*($B$1*(E2-D2))	=E2+$B$4*(D2*($B$2-F2)-E2)	=F2+$B$4*(D2*E2-$B$3*F2)
delta t	0.02	=C3+$B$4	=D3+$B$4*($B$1*(E3-D3))	=E3+$B$4*(D3*($B$2-F3)-E3)	=F3+$B$4*(D3*E3-$B$3*F3)
		...		...			...				...

Table 1

Graphing our 3 differential equations in Excel yields the following graph:

Figure 1

In Figure 1 our 3 differential equations are graphed with respect to t but this doesn’t give us a very good picture of what is really going on. Unfortunately, the closest we can get to a real 3D rendering in Excel is a “squashed” 2D image of only 2 out of the 3 data series in a simple X-Y plot. Graphing x(t) vs y(t) gives us a rough approximation of the Lorenz attractor.


Figure 2

As stated before, the exact initial values generated by the RAND() function do not affect the general shape of the attractor, since it does not change very much at all when the spreadsheet is modified, triggering the RAND() function to generate new values. At this point, this is about as far as we can explore differential equations using Euler’s method in Excel without having to do some serious Excel programming. Instead of using Excel for the rest of this project, MATLAB is used which is much more powerful and can generate 3D plots with ease.

2. MATLAB – Euler’s method for single DE

In MATLAB we created a new “m-file” using the example code provided to us. This code calculates a solution to a single differential equation defined in another m-file. Using the same equation I used in the first 3-week block task, I modified the “test_example.m” file to evaluate a solution for \frac {dy}{dx} = \frac{x-y}{x+y} The “test_example.m” file defines the equation where “euler.m” defines how a Euler’s method solution is calculated from the given equation.

“euler.m”, courtesy of Simple ODE Methods
1 % Euler’s method for a single differential equation.
2 % The differential equation is dy/dt = f(t,y).
3 function [ t, y ] = euler ( f, t_range, y_initial, nstep )
4 % We set a range of time values, from t(1) to t(2).
5 t(1) = t_range(1);
6 % We define dt by dividing the time range into an equal number of specified steps.
7 dt = ( t_range(2) – t_range(1) ) / nstep;
8 % We set the initial value of y at the beginning time t(1).
9 y(1) = y_initial;
10 % We use Euler’s method to update the value of y at new time steps.
11 % “feval” is used instead of “eval” because we are passing the name of f to the program.
12 for i = 1 : nstep
13 t(i+1) = t(i) + dt;
14 y(i+1) = y(i) + dt * feval ( f, t(i), y(i) );
15 end
16 % The following command plots y as a function of t.
17 plot(t,y)

“test_example.m”
1 function yprime = test_example(t,y)
2 yprime = (t-y)./(t+y);

>> y_init = 3;
Here, the initial y value is 3, just as in the First 3-week block task.
>> [ t, y ] = euler ( ‘test_example’, [ 0.0, 8.0 ], y_init, 80 );
The value 80 was calculated from the span, 8.0 – 0.0, divided by the number of steps from the previous project, 0.1.


Figure 3

As expected, this graph is nearly identical to the one produced in the previous project using ezplot and dsolve, proving that our system for finding solutions to differential equations using MATLAB is functioning.

3. MATLAB – Euler’s method for several DE’s

In order to execute Euler’s method for multiple DE’s, and to plot the Lorenz Attractor in 3D, the example m-file “euler.m” is modified on the following lines and saved as “euler_system.m”

9 y(:,1) = y_initial;
14 y(:,i+1) = y(:,i) + dt * feval ( f, t(i), y(:,i) );
18 % We also want a 3D plot of the vector components as they vary with t.
19 plot3(y(1,:),y(2,:),y(3,:))

Also, instead of using “test_example.m” we use a slightly modified version, which I called “lorenz.m” which includes the 3 DE’s for the Lorenz Attractor.

1 function yprime = lorenz(t,y)
2 yprime = [10.0*(y(2)-y(1));y(1)*(28-y(3))-y(2);y(1)*y(2)-8*y(3)/3];

To call the new function, the following commands are entered into MATLAB
>> y_init=[rand();rand();rand()];
Here, the initial values are set by the rand() function, which generates “random” numbers somewhere between 0 and 1.
>> [ t, y ] = euler_system ( ‘lorenz’, [ 0.0, 20.0 ], y_init, 2000);
Here, 0.0 is the initial value for t, where 20.0 is the final value. The initial values for x, y, and z are passed in through y_init, and the number of steps is set to 2000.

4. ode45 for numerical systems

5. Predator-Prey differential equations

The Lotka–Volterra equations is a basic model for the rise and fall of the population for a population consisting of predators and prey, foxes and rabbits for example.

\frac{dx}{dt} = x(\alpha - \beta y)

\frac{dy}{dt} = -y(\gamma - \sigma x)

where

x = number of prey

y = number of predators

t = time

\alpha \beta \gamma \sigma represent the interaction of the two species.

The easiest way to understand these two differential equations is to model them in Excel using Euler’s method.

alpha	1	time		 x(t) 				y(t)
beta	2	0		=RAND() 			=RAND()
gamma	3	=C2+$B$5	=D2+$B$5*(D2*($B$1-$B$2*E2))	=E2+$B$5*(-E2*($B$3-$B$4*D2))
sigma	4	=C3+$B$5	=D3+$B$5*(D3*($B$1-$B$2*E3))	=E3+$B$5*(-E3*($B$3-$B$4*D3))
delta t	0.02 	...		...				...

lotka-volterra

The next step is to solve this system in Matlab, using tools which are much more accurate than Euler’s method.

Using the following Matlab code I adapted and commented from Jay Votta’s project, we are able to use the “ode23″ function to find solutions for the predator/prey differential equations using the same initial conditions used in Excel. In theory, the image should look very similar.

M-file created:

function yprime = lotka(t,y)
yprime = diag([1 - 2*y(2), -3 + 4*y(1)])*y;
% where 1, 2, 3, and 4 = alpha, beta, gamma, sigma

Then, the following commands are entered in order to generate the graph below

>> y0 = [0.4 0.9]; % initial conditions from Excel
>> tfinal = 10; % time at final conditions from Excel
>> [t,y] = ode23(‘lotka’,[t0 tfinal],y0); % creates an array using the M-file, solved with the ode23 function
>> subplot(1,2,1) % initializes the left graph
>> plot(t,y) % populates the graph on the left, predators and prey vs. time
>> subplot(1,2,2) % initializes the right graph
>> plot(y(:,1),y(:,2)) % populates the graph on the right, predators vs. prey

predatorprey1

The solution for the given initial conditions are very similar between Matlab and Excel. Also, one interesting thing about the predators vs. prey graph illustrates that the population changes are stable and cyclical, coming full circle. Perhaps it is possible to model an unstable system using different initial conditions and different values for \alpha \beta \gamma \sigma where general shape of the population in the graph above and to the right either spirals out of control, or it spirals inward to 0. Another interesting direction to explore would be to run a simulation in realtime using this model and manually perturb the amount of predators and/or prey at a given time at different spots in the cycle to see where the system is most vunerable to the smallest change. Assuming this model is somewhat accurate, I could this being extremely useful for a conservation group to predict when it would be safe to allow hunting and when hunting would cause a serious ecological problem.

September 16, 2008

First 3-week block task

Filed under: Uncategorized — natrix @ 4:16 pm

For my first 3-week block task, I have chosen the following differential equation:

\frac{dy}{dx} = \frac{x-y}{x+y}
Equation 1

Our first step in understanding Equation 1 was to graph the solution to the differential equation for the initial condition f(0) = 3, initially using Excel and later by means of using MATLAB.

In Excel, we use Euler’s method to graph the solution to a differential equation. Since Euler’s method involves using a small step to find the next point recursevley I chose a step of 0.1 in order to minimize error. The X and Y points are as follows:

x y
0.0 3.00
0.1 2.90
0.2 2.81
0.3 2.72
0.4 2.64
0.5 2.57
0.6 2.50


 


7.4 3.46
7.5 3.50
7.6 3.53
7.7 3.57
7.8 3.61
7.9 3.64
8.0 3.68

 
Graphing the data above, yeilds the following curve:

Figure 0

Although the curve in Figure 0 is a quick and dirty approximation of what the actual curve for f(0) = 3 looks like, in order to get an exact answer we need to use an application slightly more powerful for math than Excel.

After opening up MATLAB, we enter the following code:

>> sol = dsolve(‘Dy = (x-y)/(x+y)’, ‘y(0) = 3′, ‘x’)
>> ezplot(sol, [0 8])

Which yields the following result:


Figure 1

Knowing the result for f(0) = 3 we have a rough idea what the vector field should look like. At the very least, the vectors which originate very close to the curve in Figure 1 should be nearly parallel.

In order to graph the vector field for Equation 1, my teammate and I entered the following code:

>> [x, y] = meshgrid(1:0.5:8, 1:0.5:8);
>> S = (x-y)/(x+y);
Warning: Matrix is singular to working precision.

However, this yielded a warning informing us that there was a singularity. In the Fall 2007 semester I took ECE 250, a course which is an introduction into MATLAB. In that course I learned that the simple 4-function operators of addition, subtraction, multiplication, and division sometimes need to have a period preceeding them in order to tell MATLAB that you are performing a calculation on an element-by-element basis. Element-by-element means that we evaluate a certian equation with respect to each value in the 2-dimensional array defined by the “meshgrid” function. Using our improved code below, it produced a graph we expected.

>> [x, y] = meshgrid(0:0.5:8, 2:0.5:4);
>> S = (x-y)./(x+y);
>> quiver(x, y, ones(size(S)), S), axis tight

Figure 2

Here we can see that the vector field is overwhelming negative for values less than x(2), leveling off, and then overwhelming positive for values greater than x(5) for the domain and range which is visible in Figure 2. This corresponds to the result we obtained in Figure 1.

September 9, 2008

Exploring MATLAB

Filed under: Uncategorized — natrix @ 5:58 pm

In the first few days of class, MATLAB was introduced for solving and graphing differential equations. Here is the first problem we completed:

\frac{dy}{dt} = e^{-t} -2 \cdot y

Equation 1

Using the MATLAB function “meshgrid”, we are able to generate a basic 2-dimensional array of values from -2 to 3, with steps of 0.2 for the T axis and -1 to 2 with steps of 0.2 for the Y axis. These are the points where we will evaluate the function. Using the “quiver” function we are able to generate a 2-dimensional plot of the data generating a vector field.

>> [T, Y] = meshgrid(-2:0.2:3, -1:0.2:2);

>> S = exp(-T) – 2*Y;

>> quiver(T, Y, ones(size(S)), S), axis tight

Figure 1

Unfortunately, most of the vector lines are difficult to see since they haven’t been normalized yet. To do that, we divide each vector by the length of itself, forcing them all to the same size in order to see a better representation of the direction of the vector field.

>> [T, Y] = meshgrid(-2:0.2:3, -1:0.2:2);

>> S = exp(-T) – 2*Y;

>> L = sqrt(1 + S.^2);

>> quiver(T, Y, 1./L, S./L, 0.5), axis tight

Figure 2

In Figure 2, we have traded off magnitude information for a better visualization of directional information. In order to fully understand what is happening with in Equation 1 it is best to have both graphs.

First LaTeX post

Filed under: Uncategorized — natrix @ 5:20 pm

I am working on the differential equation \nabla \times \mathbf{H} = \mathbf{J}_f + \frac{\partial\mathbf{D}}{\partial t}

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.