Problems with a function of matrix

조회 수: 4 (최근 30일)
Tsz Tsun
Tsz Tsun 2023년 4월 3일
댓글: Tsz Tsun 2023년 4월 3일
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?

채택된 답변

VBBV
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)
nonHermitianHamiltonian = 
w = 
ans = 
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
Tsz Tsun
Tsz Tsun 2023년 4월 3일
Thanks a lot for pointing this out!
Tsz Tsun
Tsz Tsun 2023년 4월 3일
Hi VBBV sorry, but I find that for the first code I post
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
I get into an error as follows:
Not enough input arguments.
Error in windingNumberCalculation (line 4)
nonHermitianHamiltonian(1,1) =2*kappa*cos(m*k) + 2*C*cos(2*m*k) + i*2*Delta*sin(2*m*k) - i*Gamma ;
I don't know why it says not enough input arugments.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by