Solving two second order ODEs

조회 수: 14 (최근 30일)
Jake Barlow
Jake Barlow 2021년 12월 25일
댓글: Star Strider 2021년 12월 25일
Hi there!
I am trying to solve for U(t) and V(t) for the two second order ODEs
and ,
where a and w are constants and with the initial conditions and .
Then I want to plot the solutions against for a given time interval.
syms U(t) V(t)
%Constants definition
a = 1;
w = 100;
dU=diff(U,t);
dV=diff(V,t);
%Initial Conditions
y0 = [0 0 1 1];
eq1 = diff(U, t, 2) == -a*w*dV;
eq2 = diff(V,t, 2) == a*w*dU;
vars = [U(t); V(t)]
OTV = odeToVectorField([eq1,eq2])
M = matlabFunction(OTV,'vars', {'t','Y'});
interval = [0 5]; %time interval
ySol = ode45(M,interval,y0);
tValues = linspace(interval(1),interval(2),1000);
yValues1 = deval(ySol,tValues,1); %U(t) solution
yValues2 = deval(ySol,tValues,2); %V(t) solution
plot(yValues1,yValues2)
Does the above code correctly solve the system of differential equations and initial conditions and plot V(t) against U(t)?
If not, then please let me know what is incorrect. Also plese let me know if there is another way to solve the system of ODEs.
Thank you for your help. Much appreciated.

답변 (2개)

Star Strider
Star Strider 2021년 12월 25일
Essentially, yes.
However it could be made a bit more efficient —
syms U(t) V(t)
%Constants definition
a = 1;
w = 100;
dU=diff(U,t);
dV=diff(V,t);
%Initial Conditions
y0 = [0 0 1 1];
eq1 = diff(U, t, 2) == -a*w*dV;
eq2 = diff(V,t, 2) == a*w*dU;
vars = [U(t); V(t)]
vars = 
[OTV,Subs] = odeToVectorField([eq1,eq2])
OTV = 
Subs = 
M = matlabFunction(OTV,'vars', {'t','Y'});
interval = [0 5]; %time interval
ySol = ode45(M,interval,y0);
tValues = linspace(interval(1),interval(2),1000);
yValues1 = deval(ySol,tValues,1); %U(t) solution
yValues2 = deval(ySol,tValues,2); %V(t) solution
plot(yValues1,yValues2)
% ---------- Slightly More Efficient: Solves Directly & Avoids The 'deval' Calls ----------
interval = [0 5]; %time interval
tValues = linspace(interval(1),interval(2),1000);
[t,y] = ode45(M,tValues,y0);
yValues1 = y(:,1); %U(t) solution
yValues2 = y(:,2); %V(t) solution
figure
plot(yValues1,yValues2)
xlabel('$V(t)$', 'Interpreter','latex')
ylabel('$\frac{dV(t)}{dt}$', 'Interpreter','latex')
.
  댓글 수: 4
Jake Barlow
Jake Barlow 2021년 12월 25일
Hi Star Strider, thank you very much for your comment. It is clearer now!
Star Strider
Star Strider 2021년 12월 25일
My pleasure!

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


Paul
Paul 2021년 12월 25일
I think there are a few mistakes in the code
syms U(t) V(t)
%Constants definition
a = 1;
w = 100;
dU=diff(U,t);
dV=diff(V,t);
%Initial Conditions
y0 = [0 0 1 1];
eq1 = diff(U, t, 2) == -a*w*dV;
eq2 = diff(V,t, 2) == a*w*dU;
vars = [U(t); V(t)]
vars = 
[OTV,S] = odeToVectorField([eq1,eq2]);
S
S = 
Note that S is ordered [V dV U dU], so that should be the ordering of the solution of ode45
M = matlabFunction(OTV,'vars', {'t','Y'});
interval = [0 5]; %time interval
% note the IC's are in the same order as S
ySol = ode45(M,interval,[0 1 0 1],odeset('MaxStep',0.0001,'InitialStep',0.0001));
tValues = linspace(interval(1),interval(2),10000);
yValues1 = deval(ySol,tValues,1); % V(t) solution
yValues2 = deval(ySol,tValues,2); % Vdot(t) solution
yValues3 = deval(ySol,tValues,3); % U(t) solution
yValues4 = deval(ySol,tValues,4); % Udot(t) solution
figure
plot(yValues3,yValues1) % plot U vs V
xlabel('U');ylabel('V')
An exact solution can be computed using dsolve()
sol = dsolve([eq1; eq2],[U(0)==0; V(0)==0; dU(0)==1; dV(0)==1])
sol = struct with fields:
V: sin(100*t)/100 - cos(100*t)/100 + 1/100 U: cos(100*t)/100 + sin(100*t)/100 - 1/100
Ufunc = matlabFunction(sol.U);
Vfunc = matlabFunction(sol.V);
plot(Ufunc(tValues),Vfunc(tValues))
xlabel('U');ylabel('V')
% compare numerical and exact solutions
figure
plot(tValues,yValues3-Ufunc(tValues),tValues,yValues1-Vfunc(tValues))
  댓글 수: 1
Jake Barlow
Jake Barlow 2021년 12월 25일
Hi Paul, thank you very much for your answer.

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by