Solving two differential equations using ode45

I have two differential equations in the form
-y"-5y'-y=3x"+4x'^2+2x+6t
-y"-2.3y'-2y=2x"+3x'^2+x
I would like to solve these equations using ODE's
the inital conditions are x(0), y(0), x'(0) and y'(0) are known
Thanks in advance

댓글 수: 5

Stephan
Stephan 2019년 6월 3일
What have you tried so far?
I have tried to convert the first order ODE referring the examples in the mathworks website. I am confused since, the second derivatives of both the variables x, y are available in both the equations
I need asssitance in converting the above equations into the ODE solver form
for Ex:
fun=@(t,y)[y(2);......];
Stephan
Stephan 2019년 6월 3일
Do you have access to the Symbolic Math Toolbox?
Yeah i do have access
I need assistance while converting the variables
fun=@(t,y)[.....]; % change of variables..
Y0=[x0; x0prime; y0; y0prime]; % initial conditions
tspan=[0 2];
[T,Y]=ode45(fun,tspan,Y0);

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

 채택된 답변

Stephan
Stephan 2019년 6월 3일
편집: Stephan 2019년 6월 3일

1 개 추천

This will help:
Follow the given examples and adapt them to your needs - for example:
syms y(t) x(t) t
ode(1) = -(diff(y,t,2))-5*diff(y,t)-y == 3*diff(x,t,2)+4*diff(x,t)^2+2*x+6*t;
ode(2) = -(diff(y,t,2))-2.3*diff(y,t)-2*y == 2*diff(x,t,2)+3*diff(x,t)^2+x;
pretty(ode)
[V,S] = odeToVectorField(ode)
fun = matlabFunction(V,'Vars',{'t','Y'})
x0 = [2 0 3 0.25];
[t, sol] = ode45(fun,[0 2], x0);
subplot(2,2,1)
plot(t,sol(:,1),'-r')
subplot(2,2,2)
plot(t,sol(:,2),'-b')
subplot(2,2,3)
plot(t,sol(:,1),'-g')
subplot(2,2,4)
plot(t,sol(:,2),'-k')

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 6월 3일

1 개 추천

Let v = [x; x'; y; y']. This means v' = [x'; x''; y'; y''].
Can you express x' in terms of some of the elements of v? Sure, it's just v(2). Expressing y' in terms of elements of v is also easy, it's v(4). So we have half the right-hand side of our system of ODEs. We still have two pieces to fill in; I'll represent them by placeholders in the (pseudo)code below.
dvdt = [v(2);
something;
v(4);
somethingElse];
Now things get a bit trickier. You can't isolate y'' or x'' as a function solely of x, x', y, y', and t. So you're going to need to use a Mass matrix. The ODE you're going to solve is not v' = f(t, y) but M*v' = f(t, y). If we rewrite your first ODE to put the second derivatives together on the left side of the equals sign:
-y''-3*x'' = 5*y'+y+4*x'^2+2*x+6*t
The left side is [0 -3 0 -1]*v (to show this, expand it out: 0*x'-3*x''+0*y'-y''). Let's put that in the second row of our mass matrix. The first and third rows are just the corresponding rows of the identity matrix, since [1 0 0 0]*v == x' and v(2) == x' and similarly for [0 0 1 0]*v and v(4) both representing y'.
M = [1 0 0 0;
0 -3 0 -1;
0 0 1 0;
aDifferentSomethingElse];
dvdt = [v(2);
something;
v(4);
somethingElse];
Fill in the something part of dvdt using the right side of that rewritten first ODE. Do the same type of manipulation (pull the second derivates to the left side of the equals sign) for the second ODE to fill in aDifferentSomethingElse and somethingElse.
Now write a function to evaluate dvdt as a function of t and v. You will need to use odeset to create an options structure containing your Mass matrix M. Pass the function and the options structure into ode45.
Technically you could have pulled both the first and second derivatives to the left side of the equals sign, and had all the elements of that row of the mass matrix be non-zero. But I wanted to keep this a little simple (too late, I know) so I pulled just the second derivatives into the Mass matrix.

댓글 수: 1

Thank you steven, Your answer gave a clear explanation in solving the ODE's

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

카테고리

질문:

2019년 6월 3일

댓글:

2019년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by