solving a differential equation coupling together in matrix and vector form with ode45

I have two problem:
1) I used two functions for solving a set of equation not in matrix or vector form but like
[x(2);3+x(4)+x(3);x(4);....................]
then [t,x] = ode45(@(t,x) lili(t,x),tspan,x0);
now I need the vector of dx=[x(2) x(4) x(6)]
but it doesnt return this vector.
2) i want to solve equation like this D2y=d2x+cross(m,n)
its clear cross product is in vector form
how could i solve matrix form of differential equation?
especially initial values is a vector

1 Comment

1) Unclear what you have done and what your problem is. Please provide the code you are using and possibly the error message you get.
2) ODE45 expects the differential equations to be given in vector format. So convert your equations from matrix form to vector form.
Best wishes
Torsten.

Sign in to comment.

 Accepted Answer

clc
clear
x_1=[1 8 9];
x_2=[2 3 4];
x0=[x_1 x_2].';
tspan = [0 20];
one = 1:3;
two = 4:6;
n=[0 1 2]';
f = @(t,x) [x(two);x(one)+n]
[t, xR] = ode45(f, tspan, x0);
x1 = xR(:,one)
x2 = xR(:,two)
plot(x1,t)
%plot(x2,t)
I have found a solution could any make me sure?

2 Comments

You solve
x1'' = x1
x2'' = x2 + 1
x3'' = x3 + 2
with initial conditions
x1(0)=1, x2(0)=8, x3(0)=9, x1'(0)=2, x2'(0)=3, x3'(0)=4
and plot x1, x2 and x3 in plot 1 and x1', x2' and x3' in plot 2.
Is it that what you wanted to achieve ?

Sign in to comment.

More Answers (1)

I have solved 3 second order of differential equations by ode45 and there is no error. as a example:
D2x=x+z+Dx+Dy
D2Y=Dx+z
D2z=Dz+Dx+y
more complicated than above and its nonlinear. and my codes:
function dy=roya(t,y)
dy=[y(2);y(1)+y(5)+y(2)+y(4);y(4);y(2)+y(5);y(6):y(6)+y(2)+y(3)];
end
y0=[0 0 0 0 0 0];
tspan = [0 5];
[t,y] = ode45(@(t,y) roya(t,y),tspan,y0);
now I need vector[y(1) y(3) y(5)] , [y(2) y(4) y(6)]
.........................................................
after solving above I wand to use [y(1) y(3) y(5)] this vector in another differential equation like:
D2m=D2y+cross(a,b)+D1y;
how could I define initial value for this equation
output is a 3by1 vecor?and a,b are 3by3 matrix

3 Comments

And why don't you solve them all together in one step ?
And what is the outcome you expect from D2y+cross(a,b)+D1y ? D2y and D1y are vectors, cross(a,b) is a 3x3 matrix.
the problem is initial value of Dm and m. how could I define initial value for them as a vector? if I solve all of them together I have to define initial value like this?y0=[0 0 0 0 0 0 [1 2 3] [2 3 4]];
a is a 3by1
b 1by3 vector
but if I solve them together and there were no problem still I need to call some of states as an input of kalman filter.
clc
clear
y0=[10 5 3];
y1=[1 2 3];
tspan = [0 5];
[t,y] = ode45(@(t,y) bhr(t,y),tspan,[y0 y1]);
function dy = bhr(t,y)
n=[0 1 2];
dy =[y(2);n+y(1)];
end
simple example of what i am looking for is above.
I am trying solve second order deffirential equation in vector form.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!