How to properly solve a system of four first order ODEs

1 view (last 30 days)
(Just for some background) Using the equations for the velocity of point vortices in a fluid, you obtain 2*N first order ODEs that describe the position. I'm trying to get the solution of the positions for just 2 point vortices to start off with, but can't see my error, but the result is in fact wrong right now. Any help would be awesome!
code is below, as well as the formula I am using
if true
%gamma is the strength of the vortices
gamma1 = 2;
gamma2 = 1;
%y1 and y2 become x(1) and x(2)
%x1 and x2 become x(3) and x(4)
f = @(t,x) [gamma2/2/pi*(-(x(1)-x(2)) / ((x(3)-x(4))^2 + (x(1)-x(2))^2));
gamma1/2/pi*(-(x(2)-x(1)) / ((x(4)-x(3))^2 + (x(2)-x(1))^2));
gamma2/2/pi*((x(3)-x(4)) / ((x(3)-x(4))^2 + (x(1)-x(2))^2));
gamma1/2/pi*((x(4)-x(3)) / ((x(4)-x(3))^2 + (x(2)-x(1))^2))];
%Initial conditions are the initial positions
[t,x] = ode45(f, [0 10], [3 1 3 1]);
subplot(1,2,1)
plot(x(:,3),x(:,1))
xlabel("x1")
ylabel("y1")
subplot(1,2,2)
plot(x(:,4),x(:,2))
xlabel("x2")
ylabel("y2")
end
  7 Comments
Star Strider
Star Strider on 28 Jul 2018
Please copy and paste your entire ODE function code and your calling script with the ode45 call, including the initial conditions. Also include all constants you may use.
What should the figure look like?
2Lindsay2
2Lindsay2 on 28 Jul 2018
Here is the m file for you to look at. This is all the code that I am using. I'm not using a function script, just the function handle in the one main script ODESolutions.m.
In the figure the center of curvature should be the same.

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 28 Jul 2018
Hi 2L2,
I don't think you are doing yourself any favors with the notation
%y1 and y2 become x(1) and x(2)
%x1 and x2 become x(3) and x(4)
In your function, try swapping the first two rows with the second two rows, to obtain
f = @(t,x) [gamma2/2/pi*((x(3)-x(4)) / ((x(3)-x(4))^2 + (x(1)-x(2))^2));
gamma1/2/pi*((x(4)-x(3)) / ((x(4)-x(3))^2 + (x(2)-x(1))^2));
gamma2/2/pi*(-(x(1)-x(2)) / ((x(3)-x(4))^2 + (x(1)-x(2))^2));
gamma1/2/pi*(-(x(2)-x(1)) / ((x(4)-x(3))^2 + (x(2)-x(1))^2));];
%Initial conditions are the initial positions
[t,x] = ode45(f, [0 100], [3 1 3 1]);
Running the final time out to 100 shows each pair going almost all the way around a circle.
  2 Comments
2Lindsay2
2Lindsay2 on 29 Jul 2018
Wow!!! I never would have guessed that switching rows would make a difference. Do you know why that is?
Thank you so much!
David Goodmanson
David Goodmanson on 29 Jul 2018
That's because your code did not correctly represent the formulas, and the corrected code happens to be the same as swapping the rows in the code you had.
The formulas don't say so, but u is a vector of x velocities v is a vector y velocities. Your basic setup has a four element vector [y1; y2; x1; x2] so the corresponding velocity vector is [vel_y1; vel_y2; vel_x1; vel_x2]; in terms of the formulas that is [v1; v2; u1; u2]. If you write new code with this starting point, it's the same as swapping the rows in what you had.

Sign in to comment.

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!