- #1

- 21

- 0

## Homework Statement

Write a function and solver for the chaotic motion of stars in a galaxy

Initial conditions:

x at time zero = 0

velocity of x at time zero = 0.3

y at time zero = 0

velocity of y at time zero = 0

Time period 2 to 200 with an interval od 0.02

## Homework Equations

U(x,y)=x^2y-y^3/3

This gives rise to the equations:

d^2x/dt^2= -x-2xy;

d^2y/dt^2= -y-x^2+y^2.

And this gives rise to the four first-order equations

dx/dt=velocity of x

dvelocity of y/dt = -x -2xy

dy/dt = velocity of y

dvelocity of y/dt = -y=x^2+y^2

## The Attempt at a Solution

This is my first time learning matlab and all the examples we were shown until now dealt with first-ODEs. I've made lots and lots of attempts at the solution, all coming up with all kinds of errors like undefined variables on Matlab. Basically my problem is that I can't figure out how to structure the function and solver for so many different terms.

One hopeless attempt was:

Denoting:

velocity of x to be Vx

x to be x

velocity of y to be Vy

y to be y

Is it supposed to look like:

function q=func(t,x,y)

q=[Vx; -x-2*x*y; Vy; -x^2+y^2]

And the solver is

clear

Vx0=0.0;

x0=0.3;

Vy0=0.0;

y0=0.0;

t=[0:0.02:200];

[x,y]=ode45(@func,Vx0,x0,Vy0,y0,[]);

plot(x,y)

I'm so confused on so many points. I don't think [x,y]=ode45(...) is right, but I don't know if it should be [t,x], [t.y] or whatever. Likewise, I know I'm meant to plot (x,y) for different time intervals, so do write plot(t,x,y)? I don't even know what [] is supposed to be for, only that it's in the notes and all the other examples I can find.

I tried so many different variations, tried to look it up in books and on the web for 7 hours straight yesterday and 2 more today and I'm feeling quite miserable about matlab. Our notes aren't helpful at all, using different variations of names for x and y for different problems which was so confusing. I'd be so appreciative for any general help on how to structure function and solver, or even direct me to a book or website that deals with two second-ODEs.

Is it just meant to look like:

function name = another function name(time, variable 1, variable 2)

name=[RHS equation 1;RHS equation 2;RHS equation 3;RHS equation 4];

And the solver:

clear

initial conditions 1 = [x];

initial conditions 2 = [y];

initial conditions 3 = [z];

constant = [a];

t = [...;...;...]

[?]=ode45(@function name,t, initial conditions 1, initial conditions2, initial conditions 3, [], constant);

plot (?)

Many thanks

Last edited: