Holooly Plus Logo

Question 10.3: Solving a first-order ODE using the modified Euler method. U......

Solving a first-order ODE using the modified Euler method.

Use the modified Euler method to solve the ODE \frac{d y}{d x}=-1.2 y+7 e^{-0.3 x} from x=0 to x=2.5 with the initial condition y(0)=3. Write a user-defined function that solves a first-order ODE using the modified Euler methos. Use the user-defined function in a script file to solve the ODE using h=0.5. Compare the results with the exact (analytical) solution: y=\frac{70}{9} e^{-0.3 x}-\frac{43}{9} e^{-1.2 x}.

The 'Blue Check Mark' means that either the MATLAB code/script/answer provided in the answer section has been tested by our team of experts; or the answer in general has be fact checked.

Learn more on how do we answer questions.

Script File

The following is a user-defined function named odeModEuler that solves a first-order ODE using the modified Euler methods.

Program 10-3: User-defined function. Solving first-order ODE using the modified Euler

function [x, y] = odeModEuler(ODE,a,b,h,yINI)

% odeModEuler solves a first order ODE using the

% modified Euler method.

% Input variables:

% ODE    Name for the function that calculates dy/dx.

% a           The first value of x.

% b           The last value of x.

% h           Step size.

% yINI     The value of the solution y at the first point (initial value).

% Output variables:

% x           A vector with the x coordinate of the solution points.

% y           A vector with the y coordinate of the solution points.

x(1) = a; y(1) = yINI;

N = (b - a)/h;

for i = 1:N

x(i+1) =x(i) +h;

SlopeEu =ODE (x (i) , y (i)) ;

yEu=y(i) +SlopeEu*h;

SlopeEnd= ODE (x (i + 1) , yEu) ;

y (i + 1) =y (i) + (SlopeEu + SlopeEnd) *h/2;

end

Assign the initial value to x ( 1) and y ( 1 ) .

Determine the number of steps.

ApplyEq.(10.14).

X_{i+1} = X_i + h     (10.14)

Determine the slope at the beginning of the interval, Eq. (10.53).

\left.\frac{d y}{d x}\right|_{x=x_i}=f\left(x_i, y_i\right)          (10.53)

ApplyEq.(10.54).

y_{i+1}^{E u}=y_i+f\left(x_i, y_i\right) h (10.54)

Determine the estimated slope at the end of the interval, Eq. (10.55).

\left.\frac{d y}{d x}\right|_{\substack{y=y^{E u} \\ x=x_{i+1}^{i+1}}}=f\left(x_{i+1}, y_{i+1}^{E u}\right) (10.55)

Calculate the numerical solution in end step i, Eq. (10.56).

y_{i+1}=y_i+\frac{f\left(x_i, y_i\right)+f\left(x_{i+1}, y_{i+1}^{E u}\right)}{2} h     (10.56)

The following program, written in a script file, uses the user-defined function odeModEuler for solving the problem. The function ODE in the input argument of odeModEuler is the same as in Example 10-1 (Chap10Exmp1ODE). The program also creates a plot that shows the numerical and the exact solutions.

clear

a = 0 ; b = 2 . 5 ; h = 0 . 5 ; yINI = 3 ;

[x, y] = odeModEuler(@Chap10Exmp1ODE,a,b,h,yINI);

xp=a:0.1:b;

yp=70/9*exp(-0.3*xp) -43/9*exp(-1.2*xp);

plot(x,y,'*r' ,xp,yp)

xlabel ( 'x' ) ; ylabel ( 'y' )

The plot produced by the program is shown in the figure on the right. The figure shows that the calculated points are much closer to the exact solution than in Example 10-1, part (a), where Euler's explicit method was employed using the same step size.

The numerical values of the exact and numerical solutions, and the error, which is the difference between the two, are:

\begin{array}{lllllll}i & 1 & 2 & 3 & 4 & 5 & 6 \\ x_{i} & 0.0 & 0.5 & 1.0 & 1.5 & 2.0 & 2.5 \\ y_{i} \text { (numerical) } & 3.0 & 3.946 & 4.188 & 4.063 & 3.764 & 3.394 \\ y_{i} \text { (exact) } & 3.0 & 4.072 & 4.323 & 4.170 & 3.835 & 3.436 \\ \text { Error } & 0.0 & 0.1261 & 0.1351 & 0.1063 & 0.0716 & 0.0425\end{array}

Comparing the error values here with those in Example 10-1, where the problem was solved with Euler's explicit method using the same size subintervals, shows that the error with the modified Euler method is much smaller.

10.3

Related Answered Questions