找回密码
 注册
Simdroid-非首页
查看: 103|回复: 5

四阶龙格库塔算法(ode45)中如何令自变量取特定值

[复制链接]
发表于 2013-8-7 08:16:02 | 显示全部楼层 |阅读模式 来自 黑龙江哈尔滨
示例程序:
[t,y]=ode45(f,[0 12],y0);
其中, f为函数句柄,自变量为范围为[0 12],y0为y的初始值,计算时程序在自变量取值范围内自动取值,请教高手,能规
定自变量取一些特殊值么,如2、4、6、8、10、12,如果能,如何编程实现。
发表于 2013-8-7 14:01:27 | 显示全部楼层 来自 福建三明
Simdroid开发平台
记得matlab faq里有这个问题,答案是不能。如果一定要这样,试试缩小时间区间
回复 不支持

使用道具 举报

 楼主| 发表于 2013-8-7 14:25:23 | 显示全部楼层 来自 黑龙江哈尔滨
本帖最后由 MILAN3 于 2013-8-7 14:27 编辑

自问自答,将程序改为下式即可
[t,y]=ode45(f,[0:0.2:12],y0)
即设定自变量T以0.2的步长在[0 12]的范围内变化,这只是一个特例,因为特殊值刚好有规律,如果没有规律就不知如何解决了。这种设置的另一好处是确定了ODE45的迭代次数,这一设置中程序迭代了(12-0)/0.2=60 次。

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2013-8-8 11:44:48 | 显示全部楼层 来自 江苏徐州
MILAN3 发表于 2013-8-7 14:25
自问自答,将程序改为下式即可
[t,y]=ode45(f,[0:0.2:12],y0)
即设定自变量T以0.2的步长在[0 12]的范围内变 ...

定步长积分:
  1. function Y = ode4(odefun,tspan,y0,varargin)
  2. %ODE4 Solve differential equations with a non-adaptive method of order 4.
  3. % Y = ODE4(ODEFUN,TSPAN,Y0) with TSPAN = [T1, T2, T3, ... TN] integrates
  4. % the system of differential equations y' = f(t,y) by stepping from T0 to
  5. % T1 to TN. Function ODEFUN(T,Y) must return f(t,y) in a column vector.
  6. % The vector Y0 is the initial conditions at T0. Each row in the solution
  7. % array Y corresponds to a time specified in TSPAN.
  8. %
  9. % Y = ODE4(ODEFUN,TSPAN,Y0,P1,P2...) passes the additional parameters
  10. % P1,P2... to the derivative function as ODEFUN(T,Y,P1,P2...).
  11. %
  12. % This is a non-adaptive solver. The step sequence is determined by TSPAN
  13. % but the derivative function ODEFUN is evaluated multiple times per step.
  14. % The solver implements the classical Runge-Kutta method of order 4.
  15. %
  16. % Example
  17. % tspan = 0:0.1:20;
  18. % y = ode4(@vdp1,tspan,[2 0]);
  19. % plot(tspan,y(:,1));
  20. % solves the system y' = vdp1(t,y) with a constant step size of 0.1,
  21. % and plots the first component of the solution.
  22. %

  23. if ~isnumeric(tspan)
  24. error('TSPAN should be a vector of integration steps.');
  25. end

  26. if ~isnumeric(y0)
  27. error('Y0 should be a vector of initial conditions.');
  28. end

  29. h = diff(tspan);
  30. if any(sign(h(1))*h <= 0)
  31. error('Entries of TSPAN are not in order.')
  32. end

  33. try
  34. f0 = feval(odefun,tspan(1),y0,varargin{:});
  35. catch
  36. msg = ['Unable to evaluate the ODEFUN at t0,y0. ',lasterr];
  37. error(msg);
  38. end

  39. y0 = y0(:); % Make a column vector.
  40. if ~isequal(size(y0),size(f0))
  41. error('Inconsistent sizes of Y0 and f(t0,y0).');
  42. end

  43. neq = length(y0);
  44. N = length(tspan);
  45. Y = zeros(neq,N);
  46. F = zeros(neq,4);

  47. Y(:,1) = y0;
  48. for i = 2:N
  49. ti = tspan(i-1);
  50. hi = h(i-1);
  51. yi = Y(:,i-1);
  52. F(:,1) = feval(odefun,ti,yi,varargin{:});
  53. F(:,2) = feval(odefun,ti+0.5*hi,yi+0.5*hi*F(:,1),varargin{:});
  54. F(:,3) = feval(odefun,ti+0.5*hi,yi+0.5*hi*F(:,2),varargin{:});
  55. F(:,4) = feval(odefun,tspan(i),yi+hi*F(:,3),varargin{:});
  56. Y(:,i) = yi + (hi/6)*(F(:,1) + 2*F(:,2) + 2*F(:,3) + F(:,4));
  57. end
  58. Y = Y.';
复制代码

评分

2

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2013-9-2 16:29:03 | 显示全部楼层 来自 黑龙江哈尔滨
inndoor 发表于 2013-8-8 11:44
定步长积分:

非常感谢,计算发现,固定步长后,有时计算不收敛。
回复 不支持

使用道具 举报

发表于 2013-9-3 12:42:41 | 显示全部楼层 来自 江苏徐州
MILAN3 发表于 2013-9-2 16:29
非常感谢,计算发现,固定步长后,有时计算不收敛。

龙格库塔法收敛与否和步长有点关系。
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-5-21 20:13 , Processed in 0.045462 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表