How to properly solve a system of four first order ODEs

조회 수: 1 (최근 30일)
2Lindsay2
2Lindsay2 2018년 7월 26일
댓글: David Goodmanson 2018년 7월 29일
(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
Star Strider
Star Strider 2018년 7월 28일
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 2018년 7월 28일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 2018년 7월 28일
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
2Lindsay2
2Lindsay2 2018년 7월 29일
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 2018년 7월 29일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by