Calculus 4.20
  1. Solve the following problem using the forward Eulerโ€™s method with stepsizes of h = 0.2 and 0.1 using Matlab programs. Compute the error and relative error using the true solution Y(x)Y\left(x\right). For selected values of xx, observe the ratio by which the error decreases when hh is halved. Plot the numerical solution and true solution on the same figure for each step size.

    Yโ€ฒ(x)=xeโˆ’xโˆ’Y(x),โ€…โ€Š0โ‰คxโ‰ค10,โ€…โ€ŠY(0)=1;Y'(x)=xe^{-x}-Y(x),\;0\leq x\leq10,\;Y(0)=1;

    The true solution is

    Y(x)=(1+x22)eโˆ’xY(x)=(1+\frac{x^2}2)e^{-x}

    Solution:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    clear
    clc
    h = 0.2;
    x = 0:h:10;
    f = (1 + (x.^2) / 2) .* exp(-1 .* x);
    df = x .* exp(-1 .* x) - (1 + (x.^2) / 2) .* exp(-1 .* x);
    df_fd(1) = NaN;

    for i = 2:length(x)
    df_fd(i) = (f(i) - f(i - 1)) / h;
    end

    plot(x, df, 'r', x, df_fd, 'bo');
    hold on

    h = 0.1;

    x = 0:h:10;
    f = (1 + (x.^2) / 2) .* exp(-1 .* x);
    df = x .* exp(-1 .* x) - (1 + (x.^2) / 2) .* exp(-1 .* x);
    df_fd(1) = NaN;

    for i = 2:length(x)
    df_fd(i) = (f(i) - f(i - 1)) / h;
    end

    plot(x, df_fd, 'k*');
  2. Use the backward Eulerโ€™s method to solve the following problem: Consider the linear equation

    Yโ€ฒ(x)=ฮปY(x)+(1โˆ’ฮป)cosโก(x)โˆ’(1+x)sinโก(x),โ€…โ€ŠY(0)=1;Y'(x)=\lambda Y(x)+(1-\lambda)\cos(x)-(1+x)\sin(x),\;Y(0)=1;

    The true solution is Y(x)=sinโก(x)+cos(x)Y(x)=\sin(x)+cos(x). Solve this problem with several values of ฮป\lambda and hh, for 0โ€…โ€Šโ‰คโ€…โ€Šxโ€…โ€Šโ‰คโ€…โ€Š100\;\leq\;x\;\leq\;10. Plot the numerical solution and true solution on the same figure for each stepsize and comment on the results.
    (a) ฮปโ€…โ€Š=โ€…โ€Šโˆ’1;โ€…โ€Šhโ€…โ€Š=โ€…โ€Š0.25โ€…โ€Šandโ€…โ€Š0.125.\lambda\;=\;-1;\;h\;=\;0.25\;and\;0.125.

    Solution:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    clear
    clc

    lamada = -1;

    h = 0.25;
    x = 0:h:10;

    df = lamada .* (sin(x) + cos(x)) + (1 - lamada) .* cos(x) - (1 + lamada) .* sin(x);
    f = sin(x) + cos(x);
    df_fd(length(x)) = NaN;


    for i = 1:length(x) - 1
    df_fd(i) = (f(i + 1) - f(i)) / h;
    end

    plot(x, df, 'r', x, df_fd, 'bo');
    hold on

    h = 0.125;
    x = 0:h:10;
    df = lamada .* (sin(x) + cos(x)) + (1 - lamada) .* cos(x) - (1 + lamada) .* sin(x);
    f = sin(x) + cos(x);
    df_fd(length(x)) = NaN;

    for i = 1:length(x) - 1
    df_fd(i) = (f(i + 1) - f(i)) / h;
    end

    plot(x, df_fd, 'k*');

    (b) ฮปโ€…โ€Š=โ€…โ€Š1;โ€…โ€Šhโ€…โ€Š=โ€…โ€Š0.25โ€…โ€Šandโ€…โ€Š0.125.\lambda\;=\;1;\;h\;=\;0.25\;and\;0.125.

    Solution:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    clear
    clc

    lamada = 1;

    h = 0.25;
    x = 0:h:10;

    df = lamada .* (sin(x) + cos(x)) + (1 - lamada) .* cos(x) - (1 + lamada) .* sin(x);
    f = sin(x) + cos(x);
    df_fd(length(x)) = NaN;


    for i = 1:length(x) - 1
    df_fd(i) = (f(i + 1) - f(i)) / h;
    end

    plot(x, df, 'r', x, df_fd, 'bo');
    hold on

    h = 0.125;
    x = 0:h:10;
    df = lamada .* (sin(x) + cos(x)) + (1 - lamada) .* cos(x) - (1 + lamada) .* sin(x);
    f = sin(x) + cos(x);
    df_fd(length(x)) = NaN;

    for i = 1:length(x) - 1
    df_fd(i) = (f(i + 1) - f(i)) / h;
    end

    plot(x, df_fd, 'k*');
Author: Little Twain
Link: https://8.9.6.8/2022/04/21/Calculus-4-20/
Copyright Notice: All articles in this blog are licensed under GNU General Public License v3.0 unless stating additionally.