Matlab: Solving two second order differential equations

In summary, the function q=func(t,x,y) gives the velocities of x, y, and z at time t. The solver clears the screen and plots (x,y) for different time intervals.
  • #1
snkk197
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:
Physics news on Phys.org
  • #2
Never mind, figured out where I went wrong.

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

Learning physics by trial and error...hmrph.

thats pretty much how you learn MATLAB =\
 
  • #4
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!
 
  • #5
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,
 
  • #6
Got it...
 

1. What is Matlab and how is it used in solving two second order differential equations?

Matlab is a programming language and software platform commonly used by scientists and engineers for numerical computation and data analysis. It has built-in functions and tools for solving mathematical problems, including differential equations. In order to solve two second order differential equations in Matlab, you will need to use the "ode45" function, which is specifically designed for solving systems of ordinary differential equations.

2. Can Matlab be used to solve any type of second order differential equations?

Yes, Matlab can be used to solve a wide range of second order differential equations, including linear and non-linear equations with constant or variable coefficients. However, the accuracy of the solutions may depend on the complexity of the equations and the chosen numerical methods.

3. How do I input my differential equations into Matlab for solving?

In Matlab, differential equations are typically represented as a system of first order differential equations. This means that if you have two second order differential equations, you will need to rewrite them as a system of four first order equations. You can then use the "ode45" function to solve this system of equations.

4. What are some common errors that may occur when using Matlab to solve differential equations?

Some common errors that may occur when solving differential equations in Matlab include incorrect input format, insufficient or incorrect initial conditions, and numerical instability. It is important to double check your equations and initial conditions, and to choose appropriate numerical methods for your specific problem.

5. Is it possible to visualize the solutions of the differential equations in Matlab?

Yes, Matlab has various plotting functions that can be used to visualize the solutions of differential equations. You can plot the solutions as a function of time, or create phase portraits to see the behavior of the system. This can be helpful in understanding the dynamics of the system and identifying any potential issues with the solutions.

Similar threads

  • Calculus and Beyond Homework Help
Replies
8
Views
763
  • Calculus and Beyond Homework Help
Replies
21
Views
843
  • Calculus and Beyond Homework Help
Replies
1
Views
105
  • Calculus and Beyond Homework Help
Replies
3
Views
572
  • Calculus and Beyond Homework Help
Replies
7
Views
799
  • Calculus and Beyond Homework Help
Replies
14
Views
395
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
326
Back
Top