How to solve a system of n differential equations?

Hello everyone, I got the solution below from the help, I tried an example with a 3x3 system and it runs ok. However, when I try to apply it to my real system, I can not find an answer. Do you have any idea why?
Example:
syms x(t) y(t)
A = [1 2; -1 1];
B = [1; 1];
Y = [x; y];
odes = diff(Y) == A*Y + B;
C = Y(0) == [2; -1];
[xSol(t), ySol(t)] = dsolve(odes,C);
My problem:
clear; close 'all'; clc;
syms T1(t) T2(t) T3(t) T4(t) T5(t)
syms x(t) y(t) z(t) w(t) p(t)
A = [-81 1 0 0 0
0.5 -0.56 0.02 0.04 0
0 0.004 -0.008 0.004 0
0 0.267 0.13 -1.4 1
0 0 0.273 3 -82.1];
B = [24000;0;0;1.333;6300];
Y = [x; y; z; w; p];
odes = diff(Y) == A*Y + B;
C = Y(0) == [300; 300; 300; 300; 300];
[xSol(t), ySol(t), zSol(t), wSol(t), pSol(t)] = dsolve(odes,C);

2 Comments

R2018a finds a solution. It does, however, depend upon an arbitrary constant of integration, which would tend to suggest that dsolve thinks there should be one more initial condition.
Thanks Walter. I'll check monday if it may be updated to R2018a. Meanwhile, how to edit such constant of integration or do any other think to solve the problem?

Sign in to comment.

 Accepted Answer

You appear to have a dynamic system. The Control System Toolbox would be more appropriate.
One way to solve it would be to convert it to an anonymous function and solve it with ode45:
[VF,Subs] = odeToVectorField(odes)
Sys = matlabFunction(VF,'Vars',{'t','Y'})
Y0 = [300; 300; 300; 300; 300];
[T,Y] = ode45(Sys, [0 5], Y0);
figure(1)
plot(T,Y)
grid
lgnd = regexp(sprintf('%s\n',Subs), '\n', 'split');
legend(lgnd(1:end-1), 'Location','E')

5 Comments

Thanks very much Star Strider (and Walter Roberson), I'm not completly sure about what happened (odeToVectorField, matlabFunction), but it works correctly :)
For while it's not for a control; but I expected to use a control in the future.
My (our) pleasure.
The odeToVectorField function creates a system of first-order ODEs from one or more first-order differential equations (as here), or from one or more higher-order differential equations. It does this by substituting functions or derivatives to create the first-order equations. It outputs the substitutions in the optional second output.
The matlabFunction function takes a symbolic equaton or system of equations and creates an anonymous function or function file from them, so that function can be used with numeric (as opposed to symbolic) calculations.
Here, it becomes a system of differential equations that ode45 can use.
It is of course possible to do all the symbolic coding and conversion to an anonymous function by hand. As long as there are functions to do it, I see no reason not to use them.
Star Strider, may I add another question to this? We initializated T1, T2, ...T5(t) using the following code:
syms T1(t) T2(t) T3(t) T4(t) T5(t)
It's bad, because any time I run the code I have to declare variables. In this example, n=5; but it gets different values for different problems.
I tried T=sym('T',[n,1]); but, in this case, T was not considered as a t function and diff(T) was considered 0 instead of dT/dt.
Is there any way to declare T = [T1(t), T2(t), ..., Tn(t)]?
Not that I’m aware of.
You can try something like this, although you would then have to manually copy the output and paste it to your syms call (without the single quotes):
TV = sprintf('T%d(t) ', 1:10)
TV =
'T1(t) T2(t) T3(t) T4(t) T5(t) T6(t) T7(t) T8(t) T9(t) T10(t) '
I know of no way to do it programmatically.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!