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 Center および File ExchangeNumerical 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