Numerically solve a (nonlinear) system of 64 differential equations

2 views (last 30 days)
Hi all,
I'm trying to numerically solve a system of 64 equations with Matlab2014a and ode45 (see picture below for the equation, where Erf is the error function and Q is 64 by 64 matrix). But typing every single equation seems very tedious. So I was wondering (more like hoping) if there is some way to implement this formula in matlab without explicitly typing all the equations (and sum terms asf) i.e. using the notion below somehow with ode45?

Accepted Answer

Walter Roberson
Walter Roberson on 18 Nov 2016
N = 64;
X = sym(zeros(1, N));
lhs = sym(zeros(1,N));
rhs = sym(zeros(1,N));
for i = 1 : N
X(i) = symfun( sprintf('x%d(t)', i), t );
lhs(i) = diff(X(i), t);
end
for i = 1 : N
rhs(i) = erf(sum( (Q(i,:) .* X) .* (1 + mu * X(i)) ) - lambda*X(i));
end
Now you can go for
dsolve(lhs == rhs)
However, dsolve() is not able to solve the system symbolically. dsolve() is also not going to be able to solve the system numerically unless you also create boundary conditions.
You could proceed to
syms y
odefun = matlabFunction( rhs(:), 'vars', {t y});
and then odefun would be a function handle returning a column vector of differential values.
However... each of your xi and xj is not a variable but is instead a function xi(t) xj(t), so the odefun that was generated in this way would contain calls to functions x1, x2, x3, .... x64 . And that is a problem because you do not know what the functions are.
Perhaps, then:
N = 64;
X = sym('x', [1, N]);
rhs = sym(zeros(1,N));
for i = 1 : N
rhs(i) = erf(sum( (Q(i,:) .* X) .* (1 + mu * X(i)) ) - lambda*X(i));
end
odefun = matlabFunction( rhs(:), 'vars', {t X});
and then you could pass odefun as the first parameter to ode45
  7 Comments
Walter Roberson
Walter Roberson on 20 Nov 2016
It might have been the random Q that I choose for testing purposes. All of the results were above 250 before the erf(), and erf() of that is 1 to within working position...
Yes, a different (much smaller) Q made a notable difference.
holistic
holistic on 20 Nov 2016
Thank you once again Walter!
This works now. With regard to the initial conditions in my case, I just choose some arbitrary ones. But normally, the initial conditions won't be the same for all "units".

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!