Reputation: 1521
I'm trying to understand how to specify the jacobian pattern in ode settings of MATLAB's ode solver.
I've started with a simple example,
syms x y z;
F = [x*y, cos(x*z), log(3*x*z*y)]
v = [x y z]
J = jacobian(F,v)
gives,
J =
[ y, x, 0]
[ -z*sin(x*z), 0, -x*sin(x*z)]
[ 1/x, 1/y, 1/z]
I would like to generate the jacobian pattern Jacobian sparsity pattern, specified as the comma-separated pair consisting of 'JPattern' and a sparse matrix. The sparse matrix contains 1s where there might be nonzero entries in the Jacobian to speed up the computation.
Therefore, from J I'd like to generate the jpattern
matrix,
jpattern =
[ 1, 1, 0]
[ 1, 0, 1]
[ 1, 1, 1]
Suggestions on how to generate the jpattern
from Jacobian
will be really helpful.
Upvotes: 0
Views: 430
Reputation: 19689
To create the desired sparse jpattern
matrix, initialize it by the size of J
and then find indices of possible non-zero entries of J
and update the corresponding entries in jpattern
to 1
.
[jr, jc] = size(J);
jpattern = sparse(jr, jc);
jpattern(find(J~=0)) = 1;
>> full(jpattern)
ans =
1 1 0
1 0 1
1 1 1
Upvotes: 1