How to write the linear equations in the form of matrix
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(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".
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!