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?

采纳的回答

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