How to write the linear equations in the form of matrix

1 ビュー (過去 30 日間)
Qiu Xu
Qiu Xu 2021 年 12 月 29 日
編集済み: Torsten 2021 年 12 月 29 日
I have a linear equations: -i*c*sqrt((nx+1)*ny)*u_{nx+1,ny-1}+i*c*sqrt(nx*(ny+1))*u_{nx-1,ny+1}=E*u_{nx,ny}, where nx,ny=0,1,2,3,...,N are integers, c is a constant. As well known, these linear equations can be written in the form of matrix that is A*u=E*u, with u=(u_{0,0},u_{0,1},u_{0,2},...,u_{1,0},u_{1,1},u_{1,2},...u_{nx,ny},...u_{N,N})^T, and A is a matrix with dimension (N+1)*(N+1). I want to know how to construct the matrix A fastly for a very large N in the Matlab. Thanks very much.

回答 (1 件)

Torsten 2021 年 12 月 29 日
編集済み: Torsten 2021 年 12 月 29 日
If you want it fast, you will have to define the matrix once by hand. Number the unknowns
U1 = u_{0,0}, U2 = u_{0,1},...,U(N+1)^2 = u_{N,N}
Proceed the same way with the equations EQN1,EQN2,...,EQN(N+1)^2 for U1,U2,...,U(N+1)^2.
Then your matrix will have an entry C in position (i,j) if the coefficient in front of Uj in EQNi equals C.
A slower, but more comfortable way is to write your equations from above as a function:
function res = fun(u)
and define
res(nx,ny) = E*u(nx,ny) - (-i*c*sqrt((nx+1)*ny)*u(nx+1,ny-1)+i*c*sqrt(nx*(ny+1))*u(nx-1,ny+1))
Then use "jacobianest" from the file exchange (or some faster routine) to calculate the Jacobian matrix A of the
function defined in "fun".


Community Treasure Hunt

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

Start Hunting!

Translated by