user1792403
user1792403

Reputation: 187

Computing the Jacobian of vectorized ODEs in MATLAB

This works fine:

syms a b x
jacobian ([a*x - a*b, a*b],[a, b])

but this:

syms a b(i) x
i = 1:6
jacobian ([a*x - a*b(i), a*b(i)],[a, b(i)])

returns the error:

Error using sym/jacobian (line 37)
The second argument must be a vector of variables.

In my opinion the second argument is a vector of variables so I don't understand the error.

Is it possible to differentiate with respect to a vector of ODEs e.g. b(i)? How would I go about it?

Upvotes: 0

Views: 457

Answers (1)

TroyHaskin
TroyHaskin

Reputation: 8401

The declaration syms b(i) creates a symbolic function b of i. So, if a vector of doubles is passed to b(i), the output will be a vector of function values:

>> syms b(i)
>> b(1:6)
ans =
[ b(1), b(2), b(3), b(4), b(5), b(6)]

>> b(i) = i^2;  % Defining the actual function will generate actual values
>> b(1:6)
ans =
[ 1, 4, 9, 16, 25, 36]

So the error is correct: you have a list of values. To create a vector of variables, use the sym function

>> b = sym('b',[1,6])
b =
[ b1, b2, b3, b4, b5, b6]

Upvotes: 1

Related Questions