⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demoode45args.m

📁 这是《Numerical Methods with MATLAB: Implementation and Application》一书的配书程序(Matlab)
💻 M
字号:
function demoODE45args(alpha)
% demoODE45args  Integrate dy/dt = -alpha*y;  y(0) = 1 with variable alpha
%
% Synopsis:  demoODE45
%            demoODE45(alpha)
%
% Input      alpha = (optional) decay rate coefficient.  Default:  alpha = 5
%                   Default: rtol = 1e-3 (internal default used by ode45)
%
% Output:    A table and plot comparing the numerical and exact solutions

if nargin<1, alpha = 2;  end

tn = 1;  y0 = 1;
[t,y] = ode45('rhsDecay',tn,y0,[],alpha);
yex = y0*exp(-alpha*t);
fprintf('      t     y_ode45   y_exact    error\n');
for k=1:length(t)
  fprintf('  %7.4f  %8.6f  %8.6f  %9.2e\n',t(k),y(k),yex(k),y(k)-yex(k));
end
fprintf('\nMax error = %10.2e for alpha = %9.2e\n',norm(y-yex,inf),alpha);
plot(t,yex,'-',t,y,'o');
title(sprintf('Solution to dy/dt = -%g*y',alpha));  xlabel('t');  ylabel('y');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -