expeuler.m

来自「ode routines in matlab」· M 代码 · 共 58 行

M
58
字号
function [x, y] = ExpEuler( x0, x_end, y0, h, rhs_fctn )%%  Purpose:%  Solve the initial value problem%               %                      y'(x) = f(x,y(x))%                      y(x0) = y0%%  on the interval [ x0, x_end ] using the explicit Euler method.%  The explicit Euler method is given by%%    y(:,i+1) = y(:,i) + h * f(x(i),y(:,i))%%%  Usage:%%       [x, y] = ExpEuler( x0, x_end, y0, h, rhs_fctn )%%  Parameters:%%  Input:%%  x0          initial x%%  x_end       final x%%  y0          initial values%%  h           step size%%  rhs_fctn    function evaluating f(x,y) for given x and y%              rhs_fctn(x, y, '') returns  f(x,y)%%%%  Output:%  %  x        vector of x values x(i) = x0 + (i-1)*h%%  y        y(:,i) contains approximation of solution at x(i)%% find number of time steps needed to reach final timemx = ceil((x_end-x0)/ h);% allocate workspacen = size(y0(:),1);x = (x0:h:x0+mx*h);y = zeros(n,mx+1);% Set initial valuesy(:,1) = y0(:);for i = 1:mx    y(:,i+1) = y(:,i) + h * feval(rhs_fctn,x(i),y(:,i),'');end

⌨️ 快捷键说明

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