Problems with a function of matrix
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi I am new to matlab and I have an error when I write a function of a matrix, here is my code
function w = windingNumberCalculation(C, Delta, g, Gamma , kappa, m)
syms k;
%Define an Hamiltonian matrix
nonHermitianHamiltonian(1,1) = 2*kappa*cos(k) + 2*C*cos(2*m*k) + i*2*Delta*sin(2*m*k) - i*Gamma ;
nonHermitianHamiltonian(1,2) = g;
nonHermitianHamiltonian(2,1) = g;
nonHermitianHamiltonian(2,2) = 0;
nonHermitianHamiltonian
w= int(diff(log(det(nonHermitianHamiltonian - 0.5*trace(nonHermitianHamiltonian)*eye(2)))) ,k , 0 , 2*pi) /(2*pi*i)
end
Then I want to excute my function, for example.
windingNumberCalculation(0.052, -0.025, 0.087, 0.134,0,1)
However, I receive messages like "This statement is not inside any function.
(It follows the END that terminates the definition of the function "windingNumberCalculation".)"
But however, I don't receive any error messages if I don't use the function method for calculation, i.e . the below code runs perfectly fine
Omega= 1;
C= 0.052*Omega;
Delta = -0.025*Omega;
g= 0.087*Omega;
Gamma = 0.134*Omega;
kappa = 0*Omega;
m=1;
syms k;
%Define an Hamiltonian matrix
nonHermitianHamiltonian(1,1) = 2*kappa*cos(k) + 2*C*cos(2*m*k) + i*2*Delta*sin(2*m*k) - i*Gamma ;
nonHermitianHamiltonian(1,2) = g;
nonHermitianHamiltonian(2,1) = g;
nonHermitianHamiltonian(2,2) = 0;
nonHermitianHamiltonian;
w= int(diff(log(det(nonHermitianHamiltonian - 0.5*trace(nonHermitianHamiltonian)*eye(2)))) ,k , 0 , 2*pi) /(2*pi*i)
May I know what is the problem when using the function?
댓글 수: 0
채택된 답변
VBBV
2023년 4월 3일
편집: VBBV
2023년 4월 3일
Call the function from command window and not from inside the script file where you have the code
% call the function from command window
>> windingNumberCalculation(0.052, -0.025, 0.087, 0.134,0,1)
function w = windingNumberCalculation(C, Delta, g, Gamma , kappa, m)
syms k;
%Define an Hamiltonian matrix
nonHermitianHamiltonian(1,1) = 2*kappa*cos(k) + 2*C*cos(2*m*k) + i*2*Delta*sin(2*m*k) - i*Gamma ;
nonHermitianHamiltonian(1,2) = g;
nonHermitianHamiltonian(2,1) = g;
nonHermitianHamiltonian(2,2) = 0;
nonHermitianHamiltonian
w= int(diff(log(det(nonHermitianHamiltonian - 0.5*trace(nonHermitianHamiltonian)*eye(2)))) ,k , 0 , 2*pi) /(2*pi*i)
end
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!