how to solve error using integral
이전 댓글 표시
clear all
clc
qu=linspace(-2*pi,2*pi,126);
x=linspace(-2,2,126);
y=2*x;
psi=(-x./sqrt(qu/2)+sqrt(y)/sqrt(exp(-2*asinh(sin(qu/2)))+1));
ket=diff(psi);
bras=psi';
bras=bras(1:end-1);
for i=1:length(qu)-1
f(i)=bras(i)*ket(i);
end
phase=1i*integral(f,qu,-pi,pi)
% i am getting the following error:
Error using integral (line 82)
First input argument must be a function handle.
Error in test (line 15)
phase=1i*integral(f,qu,-pi,pi)
how can i solve this error?
thank you in advance
답변 (2개)
Not certain what the desired result is, however everything here are arrays or vectors, so use trapz instead, since there are no functions defined —
qu=linspace(-pi,pi,126);
x=linspace(-2,2,126);
y=2*x;
psi=(-x./sqrt(qu/2)+sqrt(y)/sqrt(exp(-2*asinh(sin(qu/2)))+1));
ket=gradient(psi,(x(2)-x(1))); % Use 'gradient' To Calculate The Numerical Derivative
bras=psi';
% bras=bras(1:end-1);
% for i=1:length(qu)-1
% f(i)=bras(i)*ket(i);
% end
f = bras .* ket;
% phase=1i*integral(f,qu,-pi,pi)
phase_columns = 1i*trapz(qu,f)
phase_rows = 1i*trapz(qu,f,2)
Experiment to get the desired result.
EDIT — (14 Oct 2021 at 12:42)
Originally forgot to multiply the trapz results by 1i, now included.
.
Mathieu NOE
2021년 10월 14일
hello
integral works on function (handles) not arrays
f is an array , not a function handle so use trapz to do numerical integration
clear all
clc
qu=linspace(-2*pi,2*pi,126);
x=linspace(-2,2,126);
y=2*x;
psi=(-x./sqrt(qu/2)+sqrt(y)./sqrt(exp(-2*asinh(sin(qu/2)))+1));
ket=diff(psi);
bras=psi;
bras=bras(1:end-1);
f = bras.*ket(1:length(qu)-1);
phase=1i*trapz(qu(1:length(qu)-1),f)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!