# Using ODE45

• Mar 27th 2009, 04:09 PM
Twig
Using ODE45
Hi

I have a problem that I dont really understand.

An electron with mass m and charge Q moves in an electric and magnetic forcefield with strengths E(x,y,z) and B(x,y,z). If the electrons position is described in cartesian coordinates r(t)=(x(t),y(t),z(t)) and velocity vector
dr/dt = v(t)=(u(t),v(t),w(t)) then:

$\displaystyle m\frac{d\vec{v}}{dt}=Q(E(r)-B(r) \, \times \, v)$

The X is the cross product. I am supposed to write these equations as a first order system, that is, write out the equations for all the six components x,y,z,u,v,w.

I dont get what I am supposed to do.

I do realize to be able to use MATLAB to solve differential equations of second order, I need to re-write to first order, but this I have done for very simple diff. equations.

Thanks!

PS: If anyone has a good link or something about using ODE45, feel free to post it.
• Mar 28th 2009, 04:04 AM
CaptainBlack
Quote:

Originally Posted by Twig
Hi

I have a problem that I dont really understand.

An electron with mass m and charge Q moves in an electric and magnetic forcefield with strengths E(x,y,z) and B(x,y,z). If the electrons position is described in cartesian coordinates r(t)=(x(t),y(t),z(t)) and velocity vector
dr/dt = v(t)=(u(t),v(t),w(t)) then:

$\displaystyle m\frac{d\vec{v}}{dt}=Q(E(r)-B(r) \, \times \, v)$

The X is the cross product. I am supposed to write these equations as a first order system, that is, write out the equations for all the six components x,y,z,u,v,w.

I dont get what I am supposed to do.

I do realize to be able to use MATLAB to solve differential equations of second order, I need to re-write to first order, but this I have done for very simple diff. equations.

Thanks!

PS: If anyone has a good link or something about using ODE45, feel free to post it.

I don't have the time to answer this in detail at present, and will get back to it. But in the mean time look at this thread.

CB
• Mar 28th 2009, 12:01 PM
Twig
Hi
Hi

thanks, I looked at the link, but didnt really understand much though :(

But I can wait, thanks again!
• Mar 28th 2009, 02:26 PM
CaptainBlack
Quote:

Originally Posted by Twig
Hi

I have a problem that I dont really understand.

An electron with mass m and charge Q moves in an electric and magnetic forcefield with strengths E(x,y,z) and B(x,y,z). If the electrons position is described in cartesian coordinates r(t)=(x(t),y(t),z(t)) and velocity vector
dr/dt = v(t)=(u(t),v(t),w(t)) then:

$\displaystyle m\frac{d\vec{v}}{dt}=Q(E(r)-B(r) \, \times \, v)$

The X is the cross product. I am supposed to write these equations as a first order system, that is, write out the equations for all the six components x,y,z,u,v,w.

I dont get what I am supposed to do.

I do realize to be able to use MATLAB to solve differential equations of second order, I need to re-write to first order, but this I have done for very simple diff. equations.

Thanks!

PS: If anyone has a good link or something about using ODE45, feel free to post it.

The state vector is:

$\displaystyle {\bf{X}}=\left[ \begin{array}{c}x \\ y \\ z \\ x' \\ y' \\ z'\end{array}\right]$

The the first order vector ODE is:

$\displaystyle {\bf{X}}'=\left[ \begin{array}{c}x' \\ y' \\ z' \\ x'' \\ y'' \\ z''\end{array}\right]$

where the first three components of the derivative are the last three components of the state vector, and the second three components are obtained from the equation for the derivative of velocity writen out in terms of position and velocity.

CB
• Mar 28th 2009, 02:52 PM
Twig
Hi
Hi CB

Ok, let me see if I get this right.
I am just going to ramble out my thoughts here, correct me where I am wrong.

[t,Y]=ode45(@function,[0 tfinal],[ state vector?]) ?

then

function dYdt=function(t,Y)

m=9.11e-31; %Electron mass
Q=1.609e-18; %Charge

dYdt=zeros(6,1); %Create empty column vector
dYdt(1)=Y(4);
dYdt(2)=Y(5);
dYdt(3)=Y(6);
dYdt(4)=derivative of equation
dydt(5)=derivative of equation
dYdt(6)=derivative of equation

Now, how do I differentiate that function?

Will the

dYdt(4)=derivative of equation
dydt(5)=derivative of equation
dYdt(6)=derivative of equation

be partial derivatives?
Do I even know how E(r) looks like?

Thanks again!
• Mar 28th 2009, 11:00 PM
CaptainBlack
Quote:

Originally Posted by Twig
Hi CB

Ok, let me see if I get this right.
I am just going to ramble out my thoughts here, correct me where I am wrong.

[t,Y]=ode45(@function,[0 tfinal],[ state vector?]) ?

then

function dYdt=function(t,Y)

m=9.11e-31; %Electron mass
Q=1.609e-18; %Charge

dYdt=zeros(6,1); %Create empty column vector
dYdt(1)=Y(4);
dYdt(2)=Y(5);
dYdt(3)=Y(6);
dYdt(4)=derivative of equation
dydt(5)=derivative of equation
dYdt(6)=derivative of equation

Now, how do I differentiate that function?

Will the

dYdt(4)=derivative of equation
dydt(5)=derivative of equation
dYdt(6)=derivative of equation

be partial derivatives?
Do I even know how E(r) looks like?

Thanks again!

They are derivatives with respect to time.

If you dont know what either E(r) of B(r) are you cannot proceed further.

CB
• Mar 28th 2009, 11:56 PM
Twig
hi

Ok, so the first derivative will be, if we let $\displaystyle C(r) = B(r) \times V(r)$

$\displaystyle \frac{Q}{m}(E'_{x}(r) - C'_{x}(r))$ ?

Or don't I need to differentiate at all? And just put for example in
dYdt(4)= $\displaystyle \frac{Q}{m}(E(r)-C(r))$ , where I just you the x-components of the vector fields?

Btw, it says a bit down: "Let the fields be homogenous, that is E=(e,0,0) and B=(0,0,b). What do they mean by "e" and "b" ?

thanks
• Mar 29th 2009, 08:11 AM
CaptainBlack
Quote:

Originally Posted by Twig
hi

Ok, so the first derivative will be, if we let $\displaystyle C(r) = B(r) \times V(r)$

$\displaystyle \frac{Q}{m}(E'_{x}(r) - C'_{x}(r))$ ?

Or don't I need to differentiate at all? And just put for example in
dYdt(4)= $\displaystyle \frac{Q}{m}(E(r)-C(r))$ , where I just you the x-components of the vector fields?

Btw, it says a bit down: "Let the fields be homogenous, that is E=(e,0,0) and B=(0,0,b). What do they mean by "e" and "b" ?

thanks

I guess they mean that the electric and magnetic fields are constant and in the (1,0,0) and (0,0,1) directions respectivly. That should greatly simplify everything.

CB
• Mar 29th 2009, 10:15 AM
Twig
hi
Hi CB

I tried this:

E = (e,0,0) and B=(0,0,b).

$\displaystyle \frac{d \vec{v}}{dt} = \frac{Q}{m}((e,0,0)-(-bv,bu,0))$

This was after doing a cross product, just letting B=(0,0,b) and v(t) = (u,v,w).

After simplifying: $\displaystyle \frac{d \vec{v}}{dt} = \frac{Q}{m}((e+bv,-bu,0))$

Is this what they mean?

thanks!
• Mar 29th 2009, 12:26 PM
CaptainBlack
Quote:

Originally Posted by Twig
Hi CB

I tried this:

E = (e,0,0) and B=(0,0,b).

$\displaystyle \frac{d \vec{v}}{dt} = \frac{Q}{m}((e,0,0)-(-bv,bu,0))$

This was after doing a cross product, just letting B=(0,0,b) and v(t) = (u,v,w).

After simplifying: $\displaystyle \frac{d \vec{v}}{dt} = \frac{Q}{m}((e+bv,-bu,0))$

Is this what they mean?

thanks!

more or less (though I don't recall what u and v denote, components of the velocity?)

CB
• Mar 29th 2009, 01:14 PM
Twig
hi

Yes, u,v,w are compontents of velocity.

function dYdt=labtest(t,Y)
dYdt=zeros(4,1);

if(Y(1).^2 + Y(3).^2 <= 1)
dYdt(1)=Y(2);
dYdt(2)=1+Y(4);
dYdt(3)=Y(4);
dYdt(4)=-Y(2);
else
dYdt(1)=Y(2);
dYdt(2)=0;
dYdt(3)=Y(4);
dYdt(4)=0;
end

function testalabtest()
clc

x0 = 1;
vx0=-1.8; %Uppgiften säger börja med -2, är detta typ hastigheten som skall has
%för att elektronen lämnar vid (0,1) ? Se plot nedan. -1.8
y0=0;
vy0=0;

[t,Y]=ode45(@labtest,[0 3],[x0 vx0 y0 vy0])

x = Y(:,1); y=Y(:,3);

ezplot('sin(t)','cos(t)',[0,2*pi]);grid,hold on
plot(x,y,'-r');axis([-2 2 -2 2])

Forgive my swedish comments here and there, but does this seem reasonable?