How to calculate jacobian matrix for R^n X R^n system in matlab

조회 수: 9 (최근 30일)
Chandan Kumawat
Chandan Kumawat 2022년 5월 11일
답변: Bjorn Gustavsson 2022년 5월 13일
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
Torsten
Torsten 2022년 5월 12일
편집: Torsten 2022년 5월 12일
What is your problem ?
Transforming your (NxN) matrix of functions into an (N^2,1) vector or writing code to calculate a numerical Jacobian for a system of n^2 equations or both ?
Chandan Kumawat
Chandan Kumawat 2022년 5월 13일
Both for 2-D burgers equations.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2022년 5월 13일
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 - x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^2 + 3*x1_2^2 + 3*x2_1^2 + 3*x2_2^2)]
[-2*x1_1*(tanh(x1_1^2 + x1_2^2 + x2_1^2 + x2_2^2)^2 - 1), -2*x2_1*(tanh(x1_1^2 + x1_2^2 + x2_1^2 + x2_2^2)^2 - 1), -2*x1_2*(tanh(x1_1^2 + x1_2^2 + x2_1^2 + x2_2^2)^2 - 1), -2*x2_2*(tanh(x1_1^2 + x1_2^2 + x2_1^2 + x2_2^2)^2 - 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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by