在matlab / mathematica中以数值方式评估不确定的积分,该积分无法进行符号运算

时间:2018-09-12 17:11:10

标签: matlab wolfram-mathematica

我正在尝试计算Matlab和Mathematica中该软件无法象征性完成的功能的积分。

到目前为止,这是我的MatLab代码,但我了解它可能并没有太大帮助。

f = @(t) asin(0.5*sin(t));
a = @(t) sin(t);
F = int(f,t)   % Matlab can't do this
F = 
int(asin(sin(t)/2), t)
A = int(a,t)   % This works
A =
-cos(t)

dt = 1/(N-1); % some small number
for i=1:N
    F(i) = integral(f,(i-1)*dt,i*dt);
    A(i) = integral(a,(i-1)*dt,i*dt);
end

for循环中的两个计算都给出了fa的近似值,而不是它们与dt相乘后的积分。

在数学堆栈交换上,我发现了一个question,它得出了一个有限差分,如积分方法。但是,当我在Matlab中进行计算时,它会输出f的缩小版本,这在绘制后很明显(请参阅上文,我的意思是缩小)。我认为这是因为对于较小的时间间隔,积分基本上将函数近似于不同程度的精度(再次参见上文)。

我正在尝试获取积分的符号方程或每个位置上函数的积分的近似值。

因此,我的问题是如果我具有MatLab和Mathematica无法轻易采用的积分的功能

  1. 除默认值之外,是否可以直接使用积分计算器来近似积分?intintegraltrapz

  1. 我可以先对具有有限差分的函数进行近似,然后以符号方式求积分吗?

2 个答案:

答案 0 :(得分:1)

您的代码几乎可以了

for i=1:N
    F(i) = integral(f,0,i*dt);
end

您也可以这样做

F(1)=integral(f,0,dt)
for i=2:N
    F(i) = F(i-1)+integral(f,(i-1)*dt,i*dt);
end

第二种选择肯定更有效

因为原语实际上是F(x)= int(f(x),0,x)(0定义了一个常数)并且对于足够小的dx,您已经证明f(x)= int(f(x ),x,x + dx)/ dx i。您已经证明MATLABintégral函数可以发挥作用。

例如,让我们假设enter image description here = enter image description here,如果您想计算enter image description here,则上面的函数将计算enter image description here,只需将上面的0替换为常量a你喜欢。

现在enter image description here,所以您应该得到F,其中包含离散化的enter image description here

答案 1 :(得分:0)

The accepted answer in general is by far the best method I would say but if certain restrictions on your functions are allowable then there is a second method.

For two functions f and g see below

T = 1;  % Period
NT = 1;  % Number of periods
dt = 0.01; % time interval
time = 0:dt:NT*T;  % time

syms t
x = K*sin(2*pi*t+B);   % edit as appropriate

% f = A/tanh(K)*tanh(K*sin(2*pi*t+p))
% g = A/asin(K)*asin(K*sin(2*pi*t+p))

formulas found here

f = A1/tanh(K1)*(2^(2*1)-1)*2^(2*1)*bernoulli(2*1)/factorial(2*1)*x^(2*1-1);
% |K1|<pi/2
g = A2/asin(K2)*factorial(2*0)/(2^(2*0)*factorial(0)^2*(2*0+1))*x^(2*0+1);
% |K2|<1

there are no such limitations in the accepted answer

N = 60;
for k=2:N
    a1 = (2^(2*k)-1)*2^(2*k)*bernoulli(2*k)/factorial(2*k);
    f = f + A1/tanh(K1)*a1*x^(2*k-1);

    a2 = factorial(2*k)/(2^(2*k)*factorial(k)^2*(2*k+1));
    g = g + A2/asin(K2)*a*x^(2*k+1);
end

MATLAB can calculate sin^n(t) for n being an integer.

F = int(f,t);
phi = double(subs(F,t,time));

G = int(g,t);
psi = double(subs(G,t,time));
相关问题