我是靠谱客的博主 强健大侠,最近开发中收集的这篇文章主要介绍关于Matlab的ODE函数模拟十分缓慢的一些方案一 使用变步长二 邪恶的方法:使用非内置的ODE方法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一 使用变步长

https://ww2.mathworks.cn/matlabcentral/answers/92961-how-do-i-use-a-fixed-step-size-with-ode23-and-ode45-in-matlab


1.1 问题

I would like to use the ODE23 and ODE45 ordinary differential equation solver functions with a fixed step size.
How do I do this in MATLAB?


1.2 解释以及思路

1 缓慢的原因是因为不满足tol,导致步长十分小
2 解决方法之一是提供雅可比矩阵(这个很多时候都做不到)
3 解决方法之二 是使用fix step,但是可能会导致精度不够,或者可能会出错

ODE23 and ODE45 are MATLAB’s ordinary differential equation solver functions. ODE23 is based on the integration method, Runge Kutta23, and ODE45 is based on the integration method, Runge Kutta45. The way that ODE23 and ODE45 utilize these methods is by selecting a point, taking the derivative of the function at that point, checking to see if the value is greater than or less than the tolerance, and altering the step size accordingly. These integration methods do not lend themselves to a fixed step size. Using an algorithm that uses a fixed step size is dangerous since you may miss points where your signal’s frequency is greater than the solver’s frequency. Using a variable step ensures that a large step size is used for low frequencies and a small step size is used for high frequencies. ODE23/ODE45 are optimized for a variable step, run faster with a variable step size, and clearly the results are more accurate. If you wish to obtain only those values at a certain fixed increment, do the following:

  • Use ODE23/ODE45 to solve the differential equation.
  • Use INTERP1 to extract only the desired points.
    For example:
% the fixed step vector for desired
% output:
t0 = 0:.01:10;
[t,y] = ode23('filename',0,10);
y0 = interp1(t0,t,y);

Now, t0 and y0 are the outputs at a fixed interval.
Note that, as of MATLAB 5, you can also obtain solutions at specific time points by specifying tspan as a vector of the desired times. The time values must be in order, either increasing or decreasing.
For example:

tspan = 0:.01:10;
[t,y] = ode23('filename',tspan);

For the suggested interpolation
y0 = interp1(t0,t,y);
Correct format for interp1 should be to put new interval as last parameter, so use:
y0 = interp1(t,y,t0);

When the numerical algorithm for interpolating data on a fixed grid computes the value based on a stepsize there will be some interpolation error that depends on the rate of change of the original data and grid stepsize.


Using fixed step size on ode 45
dy/dt = F(t)

t = initial_value:step_size:final_value;
y = [y0]
for i = 2:length(t);
[t,y] = ode45(Func_name,[t_initial t(i)],y0);
y = [y;y(end)];
%Add the final variable value from ode45 to the soln vec
end

Using different step sizes in
tspan = 0:.01:10;
will result in different outputs. For example, if we use 0.01 or 0.02 as step size, the output at t=10 will be different for each case. Could you please explain it?


Using fixed step size on ode 45
dy/dt = F(t)
less computations

t = initial_value:step_size:final_value;
y = [y0];
for i = 2:length(t);
[t,y_ode] = ode45(Func_name,[0 step_size],y(end));
y = [y;y_ode(end)];
%Add the final variable value from ode45 to the soln vec
end

二 邪恶的方法:使用非内置的ODE方法

https://ww2.mathworks.cn/matlabcentral/answers/98293-is-there-a-fixed-step-ordinary-differential-equation-ode-solver-in-matlab-8-0-r2012b?#answer_107643

这个方法就没办法利用matlab的事件,以及精度方面的功能

最后

以上就是强健大侠为你收集整理的关于Matlab的ODE函数模拟十分缓慢的一些方案一 使用变步长二 邪恶的方法:使用非内置的ODE方法的全部内容,希望文章能够帮你解决关于Matlab的ODE函数模拟十分缓慢的一些方案一 使用变步长二 邪恶的方法:使用非内置的ODE方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部