Error with simultaneous differential equations

Trying to solve:
"syms x(t) y(t) t
ode1 = diff(x,t,2)+2*x==y;
ode2 = diff(y,t,2)+2*y==x;
odes = [ode1; ode2]
cond1 = y(0)==2;
cond2 = x(0)==4;
cond3 = t(0)==0;
cond4 = diff(x,t)==0;
cond5 = diff(y,t)==0;
conds = [cond1; cond2; cond3; cond4; cond5]
[xSol(t), ySol(t)] = dsolve(odes, conds)"
BUT!
Array indices must be positive integers or logical values.
Error in indexing (line 1079)
R_tilde = builtin('subsref',L_tilde,Idx);
How to solve the error with y(0) and x(0) if it is initial condition for system of ODEs?

 Accepted Answer

The problem is that ‘t’ is not a function (and shouldn’t be one), so ‘t(0)’ is interpreted as an array reference. Compounding that is that MATLAB subscript references are integers greater than 0, so that threw the error.
Assigning ‘Dx’ and the rest made referencing the derivatives easier, allowing this to now run without error —
syms x(t) y(t) t
Dx = diff(x); % Add These
D2x = diff(Dx);
Dy = diff(y);
D2y = diff(Dy);
ode1 = diff(x,t,2)+2*x==y;
ode2 = diff(y,t,2)+2*y==x;
odes = [ode1; ode2]
odes(t) = 
cond1 = y(0)==2;
cond2 = x(0)==4;
% cond3 = t(0)==0;
cond4 = Dx(0)==0;
cond5 = Dy(0)==0;
% conds = [cond1; cond2; cond3; cond4; cond5]
conds = [cond1; cond2; cond4; cond5]
conds = 
[xSol(t), ySol(t)] = dsolve(odes, conds)
xSol(t) = 
ySol(t) = 
figure % Just For Fun!
fplot(xSol, [0 10])
hold on
fplot(ySol, [0 10])
hold off
grid
EDIT — Fixed typos.
.

2 Comments

Hello, Star Strider. You are the best, very thanks!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Asked:

on 10 Dec 2022

Commented:

on 10 Dec 2022

Community Treasure Hunt

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

Start Hunting!