Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab ode45循环内的问题积分_Matlab_Ode_Numerical Integration - Fatal编程技术网

Matlab ode45循环内的问题积分

Matlab ode45循环内的问题积分,matlab,ode,numerical-integration,Matlab,Ode,Numerical Integration,当我试着使用这个Matlab代码时,它会在一个无限循环中运行。我正在尝试在ode45内部执行集成: clear clc options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]); [T,Y] = ode45(@rigid,[0 12],[0 1 1],options); plot(T,Y(:,1),'+',T,Y(:,2),'*',T,Y(:,3),'.') function dy = rigid(t,y) dy = zeros(3

当我试着使用这个Matlab代码时,它会在一个无限循环中运行。我正在尝试在ode45内部执行集成:

clear
clc
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);

plot(T,Y(:,1),'+',T,Y(:,2),'*',T,Y(:,3),'.')


function dy = rigid(t,y)
dy = zeros(3,1);    % a column vector

dy(1) = y(2) ;

dy(2) = -y(1) * y(3);
fun = @(t) exp(-t.^2).*log(t).^2+y(1);
q = integral(fun,0,Inf);
dy(3) = y(2) * y(3) + q;
不存在“无限循环”。你的函数只需要很长时间来集成。尝试将
tspan
设置为
[0 1e-7]
。这似乎是一个高频振荡,但我不知道你的方程是否正确(这是一个数学问题,而不是编程问题)。这样的系统很难准确集成(ode15可能是更好的选择),更不用说快速集成了

您还没有提到一个重要事实,即调用
integral
会生成一条警告消息:

Warning: Minimum step size reached near x = 1.75484e+22. There may be a
singularity, or the tolerances may be too tight for this problem. 
> In funfun/private/integralCalc>checkSpacing at 457
  In funfun/private/integralCalc>iterateScalarValued at 320
  In funfun/private/integralCalc>vadapt at 133
  In funfun/private/integralCalc at 84
  In integral at 88
  In rtest1>rigid at 17
  In ode15s at 580
  In rtest1 at 5 
在每次迭代中打印警告消息会大大降低集成速度。这一警告有很好的理由。您确实意识到,您正在使用
integral
0
Inf
计算的函数等价于以下函数,对吗

sqrt(pi)*((eulergamma + log(4))^2/8 + pi^2/16) + Inf*y(1)
其中
eulergamma
psi(1)
double(sym('eulergamma'))
。你的被积函数不收敛

如果您愿意,可以尝试通过以下两种方式之一避免警告消息

1。关闭警告(确保随后重新启用)。您可以使用以下代码执行此操作:

...
warning('OFF','MATLAB:integral:MinStepSize');
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
warning('ON','MATLAB:integral:MinStepSize');
...
您可以通过
lastwarn
功能获取警告的ID

2.另一个选项可能是更改集成边界并完全避免警告,例如:

...
q = integral(fun,0,1e20);
...
这可能是可以接受的,也可能是不可以接受的,但是由于结果不收敛,
integral
也不能返回正确的解