Matlab: Solving two second order differential equations

Click For Summary

Homework Help Overview

The discussion revolves around solving two second-order differential equations related to the chaotic motion of stars in a galaxy using MATLAB. The original poster outlines initial conditions and the equations derived from a potential function.

Discussion Character

  • Exploratory, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • The original poster attempts to structure a MATLAB function and solver for the equations but encounters confusion regarding variable definitions and the correct syntax for the ode45 function. They express uncertainty about how to handle multiple initial conditions and the overall structure of the function.

Discussion Status

Some participants have shared their own experiences with learning MATLAB, indicating a trial-and-error approach. The original poster has indicated progress in understanding their mistakes, but no explicit consensus or resolution has been reached regarding the correct implementation of the solver.

Contextual Notes

The original poster mentions that their course notes are not helpful, leading to confusion with variable naming conventions across different problems. There is also a request for guidance on the correct structure for solving two second-order ODEs.

snkk197
Messages
21
Reaction score
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:
Physics news on Phys.org
Never mind, figured out where I went wrong.

Learning physics by trial and error...hmrph.
 
snkk197 said:
Never mind, figured out where I went wrong.

Learning physics by trial and error...hmrph.

thats pretty much how you learn MATLAB =\
 
swraman said:
thats pretty much how you learn MATLAB =\

Feels like how I've learned the whole of my classical mechanics module - it's not very efficient!
 
Hi,
I'm also trying to solve two second ODE's for a different physical quantity. Could you please let me know the right ode structure.
Thanks a lot in advance.

Many thanks,
 
Got it...
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
4K