How to vectorize analytical derivative functions for bvp4c?

2 Ansichten (letzte 30 Tage)
Nick
Nick am 12 Mär. 2025
Beantwortet: Walter Roberson am 12 Mär. 2025
I want to use the vectorization option to speed up bvp4c while also providing analytical derivatives, but it is unclear to me how to vectorize these derivative functions which output matrices. Does vectorization of matrices extend into the higher dimensions? I could not find an example of this after some searching

Akzeptierte Antwort

Torsten
Torsten am 12 Mär. 2025
Verschoben: Torsten am 12 Mär. 2025
Only the computation of the vector-valued derivatives function f can be vectorized, not the computation of its analytical derivatives matrix (df/dy) and/or of the boundary conditions functions (dbc/dya , dbc/dyb).
See
for more details.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 12 Mär. 2025
In order to calculate analytic derivatives, you need to be using the Symbolic Toolbox.
If you use a scalar symbolic formula (an expression that does not use arrays), then the trick is that when you subs() vectors or arrays into the formula, all calculations will be done element-wise, and what you substitute in must consist of scalars or arrays of the same size. Symbolic expressions do not support implicit expansion. For example,
subs(a+b, {a,b}, {1:2, (3:4)'})
will not work, but
subs(a+b, {a,b}, {1, (3:4)'})
will work, and
subs(a+b, {a,b}, {magic(2), [1 2; 3 4]})
will work.
To get 3D arrays out, you need to subs() in 3D arrays.
If you use a scalar symbolic formula and you matlabFunction() the scalar formula, then the resulting expression will do element-wise operations, and will support implicit expansion.
If you use a vector or array-valued symbolic formula, then again all calculations will be done element-wise, and what you substitute in must consist of scalars or arrays of the same size, and implicit expansion is not supported. Whatever the vector or array formula you used needs to be compatible with the array sizes you subs() in. For example,
subs([a, 0], {a}, {1:5})
is compatible because row vector a will be concatenated with scalar 0, but
subs([a, 0], {a}, {(1:5).'})
would not be compatible because column vector a would be concatenated with scalar 0. There is no automatic expansion of sizes (other than scalars combining with arrays in a single component of the expression.)
If you use a vector-valued (not array valued!) symbolic formula, and you matlabFunction() it, then the MATLAB code that comes out will use [] in the expression. Within any one component, implicit expansion will occur, but the component sizes are determined independently of each other, and the overall expression sizes need to be compatible.
If you use an array-valued expression and matlabFunction() it, then matlabFunction() will generate code that uses reshape() with fixed component sizes. For example,
matlabFunction([a, b; b, a])
generates
@(a,b)reshape([a,b,b,a],[2,2])
This pretty much only permits passing in scalars; with the fixed [2,2] size on the reshape(), passing in vectors would result in a [a,b,b,a] that is the wrong size to fit the reshape().
The one exception is that if you were to pass in something like [1 2] and [] then that would be [[1 2],[],[],[1 2]] in the formula which would collapse to [1 2, 1, 2] which could be successfully reshape(,[2,2]) -- you could potentially mix vectors and empty vectors, if you are very careful.
So... if you have a symbolic formula in array form, then you can potentially get out 3D arrays if you subs() in reshape(VECTOR,1,1,[]), but if you have a symbolic vector in array form, then if you already matlabFunction() it, then getting out 3D arrays is nearly hopeless

Community Treasure Hunt

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

Start Hunting!

Translated by