Runge-Kutta-4th-order解3个ODE的MATLAB解决方案。

huangapple go评论97阅读模式
英文:

Runge-Kutta-4th-order solution of 3 ODEs MATLAB

问题

Lorenz方程如下:

参数值:sigma = 10,b = 8/3,r = 28。采用初始条件x = y = z = 5,从t = 0到20进行积分。对于这种情况,我们将使用四阶RK方法以恒定时间步长h = 0.03125来获得解。

需要在绘制x相对于t的图后获得的图形如下:

然而,获得的数值收敛到零,因此yl(end,1) = -6.49e-48。我们实际得到的图形是:

给出的代码如下。运行LorenzPlot.m以获得图形。你能提供代码的修正吗?谢谢。

LorenzPlot.m

  1. h = 0.03125; % 步长
  2. tspan = [0 20]; % 时间间隔
  3. y0 = [5 5 5]; % 初始条件
  4. a = 10;
  5. b = 8/3;
  6. r = 28;
  7. [tl, yl] = rk4sys(@lorenz,tspan,y0,h,a,b,r); % RK4
  8. % 使用RK4方法的时间序列图
  9. figure(1)
  10. plot(tl,yl(1:end,1))
  11. legend('x = y = z = 5')
  12. title('RK4时间图')
  13. xlabel('时间')
  14. ylabel('数量')

rk4sys.m

  1. function [tp,yp] = rk4sys(dydt,tspan,y0,h,varargin)
  2. % 参考: Chapra, S. C. (2018). 应用数值方法与 MATLAB® 工程师和科学家的第四版,pp.602-603
  3. % rk4sys: 用于一组ODE的四阶Runge-Kutta
  4. % [t,y] = rk4sys(dydt,tspan,y0,h,p1,p2,...): 用四阶RK方法积分ODE
  5. % 输入:
  6. % dydt = 评估ODEM文件的名称
  7. % tspan = [ti, tf]; 输出的初始和最终时间间隔
  8. % 使用间隔h,或者
  9. % = [t0 t1 ... tf]; 在特定时间生成解决方案输出
  10. % y0 = 依赖变量的初始值
  11. % h = 步长
  12. % p1,p2,... = dydt使用的附加参数
  13. % 输出:
  14. % tp = 自变量向量
  15. % yp = 依赖变量的解向量
  16. if nargin < 4,error('至少需要4个输入参数'), end
  17. if any(diff(tspan) <= 0),error('tspan不是升序的'), end
  18. n = length(tspan);
  19. ti = tspan(1);tf = tspan(n);
  20. if n == 2
  21. t = (ti:h:tf)'; n = length(t);
  22. if t(n) < tf
  23. t(n + 1) = tf;
  24. n = n + 1;
  25. end
  26. else
  27. t = tspan;
  28. end
  29. tt = ti; y(1,:) = y0;
  30. np = 1; tp(np) = tt; yp(np,:) = y(1,:);
  31. i = 1;
  32. while(1)
  33. tend = t(np + 1);
  34. hh = t(np + 1) - t(np);
  35. if hh > h,hh = h;end
  36. while(1)
  37. if tt+hh > tend,hh = tend-tt;end
  38. k1 = dydt(tt,y(i,:),varargin{:})';
  39. ymid = y(i,:) + k1*hh/2;
  40. k2 = dydt(tt + hh/2,ymid,varargin{:})';
  41. ymid = y(i,:) + k2*hh/2;
  42. k3 = dydt(tt + hh/2,ymid,varargin{:})';
  43. yend = y(i,:) + k3*hh;
  44. k4 = dydt(tt + hh,yend,varargin{:})';
  45. phi = (k1 + 2*(k2 + k3) + k4)/6;
  46. y(i + 1,:) = y(i,:) + phi*hh;
  47. tt = tt + hh;
  48. i = i + 1;
  49. if tt >= tend,break,end
  50. end
  51. np = np + 1; tp(np) = tt; yp(np,:) = y(i,:);
  52. if tt >= tf,break,end
  53. end

lorenz.m

  1. function yl = lorenz(t,y,a,b,r)
  2. yl = [-a*y(1)-a*y(2);
  3. r*y(1)-y(2)-y(1)*y(3);
  4. -b*y(3)+y(1)*y(2)];
  5. end
英文:

Lorenz equations are as follows:
Lorenz Equations

Parameter values: sigma = 10, b = 8/3, and r = 28. Employ initial conditions of x = y = z = 5 and integrate from t = 0 to 20. For this case, we will use the fourth-order RK method to obtain solutions with a constant time step of h = 0.03125.
Graph we need to obtain after plotting x, which is y(1), versus t is as follows:
t vs x actual

However obtained numerical values converge to zero, so that yl(end,1) = -6.49e-48. The graph we get instead is:

t vs x obtained

Codes are given below. LorenzPlot.m is run to obtain the graph.Can you please provide the corrections for the code? Thanks.

LorenzPlot.m

  1. h = 0.03125; % Stepsize
  2. tspan = [0 20]; % Time interval
  3. y0 = [5 5 5]; % Initial conditions
  4. a = 10;
  5. b = 8/3;
  6. r = 28;
  7. [tl, yl] = rk4sys(@lorenz,tspan,y0,h,a,b,r); % RK4 solution
  8. % Time-series plot using RK4 method
  9. figure(1)
  10. plot(tl,yl(1:end,1))
  11. legend(&#39;x = y = z = 5&#39;)
  12. title(&#39;RK4 time plot&#39;)
  13. xlabel(&quot;Time&quot;)
  14. ylabel(&quot;Quantity&quot;)

rk4sys.m

  1. function [tp,yp] = rk4sys(dydt,tspan,y0,h,varargin)
  2. % Reference: Chapra, S. C. (2018). Applied Numerical Methods
  3. % with MATLAB&#174; for Engineers and Scientists Fourth Edition, pp.602-603
  4. % rk4sys: fourth-order Runge-Kutta for a system of ODEs
  5. % [t,y] = rk4sys(dydt,tspan,y0,h,p1,p2,...): integrates
  6. % a system of ODEs with fourth-order RK method
  7. % input:
  8. % dydt = name of the M-file that evaluates the ODEs
  9. % tspan = [ti, tf]; initial and final times with output
  10. % generated at interval of h, or
  11. % = [t0 t1 ... tf]; specific times where solution output
  12. % y0 = initial values of dependent variables
  13. % h = step size
  14. % p1,p2,... = additional parameters used by dydt
  15. % output:
  16. % tp = vector of independent variable
  17. % yp = vector of solution for dependent variables
  18. if nargin &lt; 4,error(&#39;at least 4 input arguments required&#39;), end
  19. if any(diff(tspan)&lt;= 0),error(&#39;tspan not ascending order&#39;), end
  20. n = length(tspan);
  21. ti = tspan(1);tf = tspan(n);
  22. if n == 2
  23. t = (ti:h:tf)&#39;; n = length(t);
  24. if t(n)&lt;tf
  25. t(n + 1) = tf;
  26. n = n + 1;
  27. end
  28. else
  29. t = tspan;
  30. end
  31. tt = ti; y(1,:) = y0;
  32. np = 1; tp(np) = tt; yp(np,:) = y(1,:);
  33. i = 1;
  34. while(1)
  35. tend = t(np + 1);
  36. hh = t(np + 1) - t(np);
  37. if hh &gt; h,hh = h;end
  38. while(1)
  39. if tt+hh &gt; tend,hh = tend-tt;end
  40. k1 = dydt(tt,y(i,:),varargin{:})&#39;;
  41. ymid = y(i,:) + k1*hh/2;
  42. k2 = dydt(tt + hh/2,ymid,varargin{:})&#39;;
  43. ymid = y(i,:) + k2*hh/2;
  44. k3 = dydt(tt + hh/2,ymid,varargin{:})&#39;;
  45. yend = y(i,:) + k3*hh;
  46. k4 = dydt(tt + hh,yend,varargin{:})&#39;;
  47. phi = (k1 + 2*(k2 + k3) + k4)/6;
  48. y(i + 1,:) = y(i,:) + phi*hh;
  49. tt = tt + hh;
  50. i = i + 1;
  51. if tt &gt;= tend,break,end
  52. end
  53. np = np + 1; tp(np) = tt; yp(np,:) = y(i,:);
  54. if tt &gt;= tf,break,end
  55. end

lorenz.m

  1. function yl = lorenz(t,y,a,b,r)
  2. yl = [-a*y(1)-a*y(2);
  3. r*y(1)-y(2)-y(1)*y(3);
  4. -b*y(3)+y(1)*y(2)];
  5. end

答案1

得分: 1

你的第一个Lorenz方程似乎有误,应该如下:

Runge-Kutta-4th-order解3个ODE的MATLAB解决方案。

更改lorenz函数中的符号似乎可以纠正错误:

  1. function yl = lorenz(t,y,a,b,r)
  2. yl = [-a*y(1)+a*y(2);
  3. r*y(1)-y(2)-y(1)*y(3);
  4. -b*y(3)+y(1)*y(2)];
  5. end

Runge-Kutta-4th-order解3个ODE的MATLAB解决方案。

英文:

Your first Lorenz equation seems wrong: it should read:

Runge-Kutta-4th-order解3个ODE的MATLAB解决方案。

Changing the sign on your lorezn function seems to correct the mistake:

  1. function yl = lorenz(t,y,a,b,r)
  2. yl = [-a*y(1)+a*y(2);
  3. r*y(1)-y(2)-y(1)*y(3);
  4. -b*y(3)+y(1)*y(2)];
  5. end

Runge-Kutta-4th-order解3个ODE的MATLAB解决方案。

huangapple
  • 本文由 发表于 2023年6月6日 03:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定