ODE for 2 variables

조회 수: 27 (최근 30일)
Nivedhitha S
Nivedhitha S 2019년 8월 6일
댓글: Torsten 2019년 8월 19일
Hello all,
Does matlab support DE of this type: d(xy)/dt (ie. y dx/dt + x dy/dt)? If so how to werite these type of equations? I am a new comer to matlab and any help would be great!
Thanks.
  댓글 수: 2
Torsten
Torsten 2019년 8월 6일
You will need two equations to determine x and y. What is your second equation ?
Nivedhitha S
Nivedhitha S 2019년 8월 6일
I have set of 4 interdependent eqns of the type d(xy)/dt, say something like this, where p1-p7 are parameters:
  1. dx/dt=A;
  2. d(xy)/dt=A*(p1-y)+p2*x*y
  3. d(xz)/dt= A*(p3-z)-p4*x*y
  4. d(xu)/dt= p5*x*(p6-u)-p7*x*y

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

채택된 답변

Torsten
Torsten 2019년 8월 6일
편집: Torsten 2019년 8월 6일
function main
p = [1 2 3 4 5 6 7];
A = 20;
x0 = 2;
y0 = 5;
z0 = 27;
u0 = -34;
a0 = [x0; x0*y0; x0*z0; x0*u0];
tspan = [0, 1];
[T a] = ode45(@(t,a)fun(t,a,A,p),tspan,a0)
end
function da = fun(t,a,A,p)
x = a(1);
y = a(2)/a(1);
z = a(3)/a(1);
u = a(4)/a(1);
da = zeros(4,1);
da(1) = A;
da(2) = A*(p(1)-y)+p(2)*a(2);
da(3) = A*(p(3)-z)-p(4)*a(2);
da(4) = p(5)*x*(p(6)-u)-p(7)*a(2);
end
  댓글 수: 2
Nivedhitha S
Nivedhitha S 2019년 8월 17일
That was very useful! Thanks a lot!
Can I creat few equantions which has to be integrated in a different time range within the same function?
say
da (5) = p(6)*x*y;
but this has to be active in a tspan of (t2:t3) whereas the other 4 has to be active in the tspan of (t1:t3). Will this be possible to do?
Torsten
Torsten 2019년 8월 19일
You will have to call the ODE integrator two times (from t1 to t2 and from t2 to t3) and adjust the equations to be integrated depending on the time span (4 active equations from t1 to t2 and 5 active equations from t2 to t3).

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

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 8월 6일
Write your equations in the form M*DV = RHS where:
,
and M is the mass matrix. Since your second equation expands to:
the second row of your mass matrix will be [y, x, 0, 0]. Multiply that vector times DV and you'll see that you've recreated the left side of the second differential equation. Generate the remaining rows of the mass matrix similarly.
Create an options structure that specifies the mass matrix using odeset and the 'Mass' name-value pair. The value for that pair will be a function handle that accepts t (time) and the vector V and returns the mass matrix M.
Then write the function that evaluates RHS as a function of t and V. Call the ODE solver specifying that function and the options structure (so the solver knows how to create the mass matrix.

카테고리

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