how to integrate a product with function handle
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to perform a numerical integration with function handle. Please find details below
Nz = 256;
L = 1;
dz = L/Nz;
zgrid = dz*(0:Nz-1)'-L/2;
gN = 20000;
U0 = 5*gN;
U1=1.9*gN;
sigma = 5*dz;
xb = L/4;
tau = 0.01;
h=0.2*gN;
n0=mean(dens(abs(zgrid)<L/10,:));
wp=2*pi*sqrt(gN*n0);
Ub = @(t) U1*(t<tau)+(t>=tau)*(U1+h*sin(wp*(t-tau)));
Vext = @(t) (U0*exp(-(zgrid+xb).^2/sigma^2)+Ub(t)*exp(-(zgrid-xb).^2/sigma^2));
Now I want to integrate
integrate (Vext *A, zgrid)
where A is a double array matrix of dimension (zgrid,t)
The problem is Vext is function handle and A is numerical values of the dimension (zgrid,t). please help how to do this integration?
댓글 수: 0
답변 (1개)
Steven Lord
2021년 5월 7일
You can't calculate the product of a number and a function handle. What you can do instead is calculate the product of a number and the result of evaluating that function handle.
fh = @(x) x.^2;
This works and returns a number. It evaluates the function handle with x = 5 then multiplies that result by 2.
y1 = 2*fh(5)
This works and returns another function handle that you can evaluate. Note that inside the function handle fh2 I evaluate fh at the value that was passed into fh2.
fh2 = @(x) 2*fh(x)
y2 = fh2(7) % Essentially the same as y1 (except with x = 7 instead of x = 5.)
This does not work which is why I put it last.
y3 = 2*fh
참고 항목
카테고리
Help Center 및 File Exchange에서 GPU Computing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!