How do I use of "trapz" for numerical integration?

1 次查看(过去 30 天)
Hi everyone. I tried to use of "Tarpz" command for numerical integration and I use of "Meshgrid" command for make a matrix.in final, when script running, I get this error: X must be a vector. so How I can use of the "Tarpz" for my work? and I do not get the mentioned error.
close all
clear all
clc
z=0.42;
wvl=1030e-9; % is wavelength k=(2*pi)/wvl;
w0=0.1;
zr=pi*w0.^2/wvl;
w=w0*sqrt(1+(z/zr));
R=z*(1+(zr/z).^2);
x_1=linspace(-1,1,1000);
x_2=linspace(-1,1,1000);
[r1, r2]=meshgrid(x_1,x_2); [r3, r4]=meshgrid(x_1,x_2);
u1=w0/w*exp(-(r1.^2+r2.^2).^2/w.^2)*exp(-1i*k*z).*exp(-1i*k*(r1.^2+r2.^2).^2/(2*R))*exp(atan(z/zr)); % guassian beam fomula
A=(1/sqrt(1i))*sqrt(2*z./(pi*k*r1.*r2))*exp(-(1i*k*r1.*r2)/z); % bessel function formula that converted to expotential form
u2=trapz(r1,A.*u1);

回答(1 个)

Roger Stafford
Roger Stafford 2014-11-25
编辑:Roger Stafford 2014-11-25
The quantity which you have designated as 'r1' in the call to 'trapz' is meant to represent the variable of integration, and as such it must be either a scalar giving constant increments of this variable or a vector giving its successive values. It cannot be a two-dimensional array as you have produced from 'meshgrid'. You need to carefully read the documentation for 'trapz' at:
http://www.mathworks.com/help/matlab/ref/trapz.html
and then rethink your implementation so as to comply with this requirement.
[Added comment] It looks as though you are attempting to perform a double integration wherein there are two variables of integration. If so, you can only do that using 'trapz' by making two successive calls on it in an appropriate manner.

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by