How to calculate jacobian matrix for R^n X R^n system in matlab
3 views (last 30 days)
Show older comments
How to find the jacobian for R^n X R^n system
Function F(i,j) is a system nonliear functions, which constitutes n^2 equation.
i=1,2,....,n and j=1,2,......,n
Then how to find d F(i,j) / d(x(i,j))
where x(i,j) is matrix elements
4 Comments
Answers (1)
Bjorn Gustavsson
on 13 May 2022
Maybe this illustration of how to reshape your variable x and function f from n-by-n to n^2-by-1:
syms x [2,2] % a small n-by-n set of independent variables
% and a 2-by-2 array of functions:
f = [sin(2*x(:).'*x(:)) cos(3*x(:).'*x(:));exp(-x(:).'*x(:)),tanh(x(:).'*x(:))];
% Make these into (2*2)-by-1 arrays
y = x(:)
x1_1
x2_1
x1_2
x2_2
F = f(:);
dfdx = jacobian(f(:),x(:))
dfdx =
[ 4*x1_1*cos(2*x1_1^2 + 2*x1_2^2 + 2*x2_1^2 + 2*x2_2^2), 4*x2_1*cos(2*x1_1^2 + 2*x1_2^2 + 2*x2_1^2 + 2*x2_2^2), 4*x1_2*cos(2*x1_1^2 + 2*x1_2^2 + 2*x2_1^2 + 2*x2_2^2), 4*x2_2*cos(2*x1_1^2 + 2*x1_2^2 + 2*x2_1^2 + 2*x2_2^2)]
[ -2*x1_1*exp(- x1_1^2 - x1_2^2 - x2_1^2 - x2_2^2), -2*x2_1*exp(- x1_1^2 - x1_2^2 - x2_1^2 - x2_2^2), -2*x1_2*exp(- x1_1^2 - x1_2^2 - x2_1^2 - x2_2^2), -2*x2_2*exp(- x1_1^2 - x1_2^2 - x2_1^2 - x2_2^2)]
[ -6*x1_1*sin(3*x1_1^2 + 3*x1_2^2 + 3*x2_1^2 + 3*x2_2^2), -6*x2_1*sin(3*x1_1^2 + 3*x1_2^2 + 3*x2_1^2 + 3*x2_2^2), -6*x1_2*sin(3*x1_1^2 + 3*x1_2^2 + 3*x2_1^2 + 3*x2_2^2), -6*x2_2*sin(3*x1_1^2 + 3*x1_2^2 + 3*x2_1^2 + 3*x2_2^2)]
[-2*x1_1*(tanh(x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- 1)]
You can do very much the same with arbitrary numerical functions too, just keep track of where the different elements should go, then after integrating the solution you will just reshape the solution back into your n-by-n shape for each step of the solution. It is admittedly bit fidgety to get these things right - I have found it very helpful to check that I get this right by looking at small enough systems that I can manually inspect every matrix in the process. Even if I want it at 100-by-100 or more I check that the procedures work for 4-by-4, 10-by-10 etc.
HTH
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!