How to write the linear equations in the form of matrix

조회 수: 3 (최근 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
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".

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by