求出x与y的值再积分
基本格式:trapz(x,y)
例.
clear,clc;
format long %15字长定点数
fun = @(x)sin(x)./x
x1 = pi/4:pi/50:pi/2;
y1 = fun(x1);
s1 = trapz(x1,y1)
x2 = pi/4:pi/100:pi/2; %剖分越细密,积分越精密
y2 = fun(x2);
s2 = trapz(x2,y2)
根据上下限直接求积分
格式:integral(fun,xmin,xmax),其中fun为句柄函数
例1.
clear,clc;
s = integral(@fun,-1,1)
%函数定义要加在脚本最后
function f = fun(x)
f = exp(-x.^2);
end
例2.
clear,clc;
fun = @(x)sin(x)./x
x = linspace(1,20,20);
for i = 1:20
y(i) = integral(fun,0,x(i));
end
plot(x,y)
例.
% integral(fun,a,b,'ArrayValued',true) 表示被积函数是数组值函数
clear,clc;
t = 0:0.1:1;
fun = @(x)sin(x-t).^2;
y = integral(fun,0,pi,'Arrayvalued',true);
plot(t,y)
格式:integral2(fun,xmin,xmax,ymin,ymax)
例1.
clc;
fun = @(x,y)exp(-x.^2/2).*sin(x.^2+y);
s = integral2(fun,-2,2,-1,1)
例2.
xl = -1;xu = 1;
yl = @(x)-sqrt(1-x.^2);yu=@(x)sqrt(1-x.^2);
fun = @(x,y) sqrt(1-x.^2-y.^2);
s = integral2(fun,xl,xu,yl,yu)
格式:integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)
例.
xl = -1;xu = 1;
yl = @(x)-sqrt(1-x.^2);yu=@(x)sqrt(1-x.^2);
zl = @(x,y)sqrt(x.^2+y.^2);zu=1;
fun = @(x,y,z)abs(sqrt(x.^2+y.^2+z.^2)-1);
s = integral3(fun,xl,xu,yl,yu,zl,zu)