符号函数:输入参数必须是 'double'

Symbolic functions: Input arguments must be 'double'

我想在 MATLAB 中使用 int 计算以下函数的积分。我尝试了以下方式,但它总是显示错误 Input arguments must be 'double':

syms x
int(exp(x) .* 1000 .* square(1000 .* (x -1)))

syms x
int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000, 1000)

syms x
int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000.0, 1000.0)

我的函数定义是:

function y = step(t)
y = (1 + sign(t))/2;
end

function y = square(t)
y = step(t + 0.5) - step(t - 0.5);
end

您需要将您的函数声明为符号函数:

syms square
syms step2

为什么step2?因为step是一个常用工具箱的已经定义好的函数。


MWE

function test

syms x z
syms square
syms step2

z1 = int(exp(x) .* 1000 .* square(1000 .* (x -1)))
z2 = int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000, 1000)
z3 = int(exp(x) * 1000 * square(1000 * (x -1)), x, -1000.0, 1000.0)

end

function y=step2(t)
y = (1 + sign(t))/2;
end

function y = square(t)
y = step2(t + 0.5) - step2(t - 0.5);
end

z1 = 500*exp(x)*(sign(1000*x - 1999/2) - sign(1000*x - 2001/2))
z2 = 1000*exp(1999/2000)*(exp(1/1000) - 1)
z3 = 1000*exp(1999/2000)*(exp(1/1000) - 1)