© Irfan Turk 2019
I. TurkPractical MATLABhttps://doi.org/10.1007/978-1-4842-5281-9_4

4. Numerical Methods

Irfan Turk1 
(1)
Nilufer, Bursa, Turkey
 

In this chapter, I present how to solve problems using numerical techniques. First, interpolation and extrapolation methods are introduced. Next, curve fitting and root finding are explained. Finally, numerical integration and numerical differentiation are covered.

Interpolation and Extrapolation

It is possible to find the corresponding value of a point within a given domain using the function interp1 when dealing with interpolation in one-dimensional applications. It is also possible to find a value that is out of a given domain via the same function. This method is called extrapolation. Various methods such as linear or spline are available in the interp1 function (Table 4-1). Some of the methods, which can be used for interpolation, are not available for extrapolation within the interp1 function. If an outer point is picked for extrapolation using a method in which extrapolation does not work, MATLAB returns NaN which stands for “not a number.” Therefore, the method of choice is important for performing interpolation or extrapolation.
Table 4-1

Methods Used with interp1

Option

Explanation

linear

Uses linear interpolation (default value)

nearest

Uses nearest neighbor interpolation

next

Uses next neighbor interpolation

previous

Uses previous neighbor interpolation

spline

Uses piecewise cubic spline interpolation and extrapolation

Example 4-1. For the given values of x = [1, 2, 5, 8] and y = [20, 30, 50, 80], calculate the value of 7 using the default method of the interp1 function, and calculate the value of 10 with the methods linear and spline.

Solution 4-1. The following code can be used to accomplish the given task.

Example4p1.m
%Example4p1
%This code uses interp1 function
x=[1, 2, 5, 8];
y=[20, 30, 50, 80];
ValX1 = interp1(x,y,7);
ValX2 = interp1(x,y,10,"linear");
ValX3 = interp1(x,y,10,"spline");
fprintf("1st value is %f ",ValX1)
fprintf("2nd value is %f ",ValX2)
fprintf("3rd value is %f ",ValX3)
Once the code is executed, the following output will be obtained.
> Example4p1
1st value is 70.000000
2nd value is NaN
3rd value is 121.428571
>

As shown in the preceding output, the computer prints NaN for the 2nd value. It indicates the fact that extrapolation is not available with the linear method in the interp1 function.

Curve Fitting

Using a data set, one can obtain a polynomial function using the function polyfit.

Example 4-2. For the given values of x = [4, 6, 6.5, 8, 10] and y = [4, 8, 6, 7, 9], obtain four different curves from the first degree to the fourth. Then, with these four polynomials, evaluate the values of x, where x = 3:0.5:20. The code should plot all the curves in a single figure.

Solution 4-2. The following code can be used to accomplish the given task.

Example4p2.m
%Example4p2
%This code uses polyfit function
x=[4, 6, 6.5, 8, 10];y=[4, 8, 6, 7, 9];
P1 = polyfit(x,y,1)%1st degree
P2 = polyfit(x,y,2)%2nd degree
P3 = polyfit(x,y,3)%3rd degree
P4 = polyfit(x,y,4)%4th degree
%Using obtained curves
xx = 3:0.5:20;
y1 = polyval(P1,xx);
y2 = polyval(P2,xx);
y3 = polyval(P3,xx);
y4 = polyval(P4,xx);
subplot(221)
plot(x,y,'s',xx,y1);title('1st degree')
subplot(222)
plot(x,y,'s',xx,y2);title('2nd degree')
subplot(223)
plot(x,y,'s',xx,y3);title('3rd degree')
subplot(224)
plot(x,y,'s',xx,y4);title('4th degree')
Once the code is executed, the following output will be printed on the screen.
> Example4p2
P1 =
    0.7129    1.8812
P2 =
   -0.0678    1.6691   -1.2124
P3 =
    0.1291   -2.7559   19.2697  -37.1885
P4 =
   -0.2905    8.3000  -86.1202  383.4500 -608.7143
>
As shown in this output, the coefficients of each polynomial are calculated and assigned to the corresponding variables. Figure 4-1 will also be displayed along with the output.
../images/483991_1_En_4_Chapter/483991_1_En_4_Fig1_HTML.jpg
Figure 4-1

Output of Example4p2

Root Finding

It is possible to find the roots of nonlinear equations with MATLAB. For this purpose, the built-in function fzero can be used. Other popular methods, which we overview in this section, include the bisection method, Newton’s method, the secant method, and the fixed-point iteration method.

fzero Function

fzero is a built-in function that finds the roots of a nonlinear function in MATLAB. This function is very easy to code and it has two different uses. The first use consists of the calculation of the root around a point for the defined function.
> fzero(Myfunction,x0)

In this use, the given function is Myfunction, and the point is defined by x0.

In the second use, the function finds the root within a given interval.
>fzero(Myfunction, [x0, x1])

In this use, the given function is Myfunction and the root is searched between the points x0 and x1. The function can be defined using an anonymous function as in the following example.

Example 4-3. Write code to find the root of 2 cos (x). The code should separately find the root around $$ frac{pi }{4} $$ and the root within the interval of π and 2π. The code should print the values in terms of degrees on the screen.

Solution 4-3. The following code can be used to find the roots.

Example4p3.m
%Example4p3
%This code uses fzero function
Myfunc = @(x) 2*cos(x); % function
x0 = pi/4; % around this point
FirstKind = fzero(Myfunc,x0); % the usage
CorrespondingDegree1 = (FirstKind)*(180/pi);
fprintf("1st root is %d degree ",CorrespondingDegree1)
interval = [pi, 2*pi];% interval for 2nd usage
SecondKind = fzero(Myfunc,interval);
CorrespondingDegree2 = (SecondKind)*(180/pi);
fprintf("2nd root is %d degree ",CorrespondingDegree2)

As shown in the code, the values of the corresponding roots are converted from radians to degrees by simply multiplying these values by 180/π.

Once the code is executed, the following output will be printed on the screen.
> Example4p3
1st root is 90 degree
2nd root is 270 degree
>

Bisection Method

The bisection method can be implemented by creating a user-defined function. There is no built-in function like fzero in MATLAB for the bisection method. The algorithm in the following example can be used for the implementation of this method. In the bisection method, the boundary points should be picked carefully. Due to the nature of the method, the defined function must have different signs at the boundaries (at a and b as presented in the code).

Example 4-4. Write a code to find the root of f(x) = x3 − 3x + 1 between 0 and 1. The code should calculate the value up to a tolerance given by 10−10 using the bisection method.

Solution 4-4. The following code can be used to find this root.

Example4p4.m
%Example4p4
%This code uses bisection algorithm
format long
tolerance=1e-10;
a = 0.0; b = 1.0;
f=@(x) x^3-3*x+1;% function is defined here
Result = MyBisect (f,a,b,tolerance);
fprintf('Root is = f ( %d ) = %d ',Result,f(Result));
function [c] = MyBisect (f,a,b,tolerance)
c = (a + b)/2;
while (abs(f(c)) > tolerance)
    c = (a + b)/2;
    if ( f(a)*f(c) < 0 )
       b = c;
    else
       a = c;
    end
end
end
Once the code is executed, the following output will be printed on the screen.
> Example4p4
Root is = f ( 3.472964e-01 ) = -4.365619e-11
>

Newton’s Method

In some textbooks, Newton’s method is referred to as the Newton–Raphson method. There is no built-in function in MATLAB to calculate a root by using this method, but its algorithm is quite easy to implement.
$$ {x}_{n+1}={x}_n-frac{fleft({x}_n
ight)}{f^{prime}left({x}_n
ight)} $$

The function must be defined by the user to calculate the root using this algorithm.

Example 4-5. Write code to find the root of f(x) = x3 − 3x + 1 with an initial value of 0.5. The code should calculate the value up to a tolerance given by 10−10 by using Newton’s method.

Solution 4-5. The following code can be used to illustrate Newton’s method.

Example4p5.m
%Example4p5
%This code uses Newton's algorithm
format long
tolerans=1e-10;
x0 = 0.5;
F = @(x) x^3-3*x+1; %Function written here
Derivat = @(x) 3*x^2-3; % Its derivative should be here
%Make sure Derivat(x0) is NOT equal to zero
x0 = MyNewton (F,Derivat ,x0 ,tolerans);
fprintf('Root = f ( %d ) = %d ',x0,F(x0));
function x0 = MyNewton (F,Derivat,x0,tolerans)
while (abs(F(x0)) > tolerans)
    Result = x0 - (F(x0) / Derivat (x0));
    x0=Result;
end
end
Once we run the code, the following output will be displayed on the screen.
> Example4p5
Root = f ( 3.472964e-01 ) = -2.220446e-16
>

Note that the initial value plays an important role in finding the root more quickly.

Secant Method

The secant method is yet another method for finding the root of an equation. The algorithm for this method is
$$ {x}_{n+1}={x}_n-frac{left({x}_n-{x}_{n-1}
ight)}{left(fleft({x}_n
ight)-fleft({x}_{n-1}
ight)
ight)}fleft({x}_n
ight) $$

The function must be defined by the user to calculate the root using this algorithm. The following example can be used to illustrate the secant method.

Example 4-6. Write a code to find the root of f(x) = x3 − 3x + 1 with the first two initial values of 0 and 1. The code should calculate the value up to a tolerance given by 10−10 by using the secant method.

Solution 4-6. The following code can be used to find the root via the secant method.

Example4p6.m
%Example4p6
%This code uses the secant method
format long
tolerance=1e-10;
x0 = 0.0;
x1 = 1.0;
f = @(x) x^3-3*x+1; % function comes here
x1 = MySecant (f,x0,x1,tolerance);
fprintf('Root = f ( %d ) = %d ',x1,f(x1));
function x1 = MySecant (f,x0,x1,tolerance)
while (abs(f(x1)) > tolerance)
    Result = x1 - ( (x1 - x0) / (f(x1)-f(x0)) ) * f(x1);
    x0 = x1;
    x1 = Result;
end
end
Once the code is executed , the following output will be shown on the screen.
> Example4p6
Root = f ( 3.472964e-01 ) = -4.440892e-16
>

Fixed-Point Iteration

The fixed-point iteration method is generally used for finding the roots of equations in the form f(x) = 0. From the given equation, the iteration’s function must be derived. In this method, the given equation is rewritten in the form x = g(x), where g(x) is the iteration function. In the algorithm, an initial point x0 is selected. Then, the iteration function is recursively processed by xn + 1 = g(xn). If the function f is continuous and xn converges to a number, say M, then M is a fixed point of the iteration function.

For a certain function, more than one equation can be picked as the iteration function. Converging to a number plays an important role in picking the iteration function. To grasp the idea of how to select the iteration function, take a look at the following example.

Example 4-7. Considering the function given by f(x) = x2 − x − 4, find possible iteration functions g(x) to find the roots of f(x).

Solution 4-7. The iteration function g(x) can be selected as follows.
  1. a)

    g(x) = x2 − 4

     
  2. b)

    $$ g(x)=1+frac{4}{x} $$

     
  3. c)

    $$ g(x)=sqrt{x+4} $$

     

Example 4-8. Write code to find the root of f(x) = x3 − 3x + 1 with an initial value of 0.5. The code should calculate the value up to a tolerance given by 10−10 using the fixed-point iteration method.

Solution 4-8. The following code can be used to find the root using this method.

Example4p8.m
%Example4p8
%This code uses fixed-point iteration method
format long
tolerans=1e-10;
x0 = 0.5;
f = @(x) x^3-3*x+1;% function comes here
while (abs(f(x0)) > tolerans)
    Result =  (x0 ^ 3 + 1) / 3; %fixed point function
    x0 = Result;
end
fprintf('Root = f ( %d ) = %d ',x0,f(x0));
Once the code is executed, the following output will be displayed on the screen.
> Example4p8
Root = f ( 3.472964e-01 ) = -5.213918e-11
>

If we compare the results obtained by the different methods given, Newton’s method yields a slightly better result than the others.

Numerical Integration

MATLAB provides various built-in functions, shown in Table 4-2, to calculate integrals numerically.
Table 4-2

Functions Used in Numerical Integration

Function

Explanation

integral

Performs integration by using the global adaptive quadrature

integral2

Evaluates double integrals numerically

integral3

Evaluates triple integrals numerically

cumtrapz

Calculates cumulative integrals using the trapezoidal method

trapz

Calculates integrals using the trapezoidal method

Table 4-3

Common ODE Solvers in MATLAB

Solver

Accuracy

Explanation

ode45

Medium

Most of the time, this solver should be the first one tried; used for nonstiff problems

ode113

Low to high

Used for nonstiff problems, and when ODE is expensive to evaluate

ode15s

Low to medium

Recommended when ode45 fails, and when solving differential algebraic equations for stiff problems

ode15i

Low

Solves fully implicit differential equations for stiff problems

Example 4-9. Write code to numerically calculate the integral of $$ 2x{e}^{x^2} $$ from 0 to 1 by using the function integral. Then, compare the numerical solution with the analytical solution. The code should display the numerical result, analytical result, and error on the screen.

Solution 4-9. The following code can be used to accomplish the given task.

Example4p9.m
%Example4p9
%This code uses integral function
format long
Myfun = @(x) 2*exp(x.^2).*x;
NumericSol = integral(Myfun,0,1);
AnalyticSol = exp(1)-1;
Error = abs(NumericSol - AnalyticSol);
fprintf('Numerical solution :%e ',NumericSol)
fprintf('Analytical solution:%e ',AnalyticSol)
fprintf('Error is           :%e ',Error)
Once the code is executed, the following output will be printed on the screen.
> Example4p9
Numerical solution :1.718282e+00
Analytical solution:1.718282e+00
Error is           :4.440892e-16
>

Numerical Differentiation

In this section, numerical methods for solving ordinary differential equations are discussed.

Ordinary Differential Equations

Numerical solution of ordinary differential equations has important applications in a wide variety of areas in engineering and applied sciences. MATLAB provides numerous built-in functions that solve ordinary differential equations with an initial condition.

Ordinary differential equations (ODEs) with initial values can also be solved using Euler’s method and the Runge–Kutta method. This section covers how to solve initial value problems of ODEs using the ODE solvers, Euler’s method, and the fourth-order Runge–Kutta method.

ODE Solver Functions

MATLAB has numerous functions for solving ODEs. Although ode45 is the first one to come to mind, the user should consider the type of the problem (stiff or nonstiff) when selecting the solver.

Example 4-10. Write code to solve the following initial value problem given by
$$ frac{dy}{dx}=y-x,y(0)=frac{2}{3},0le xle 5 $$

using the ode45 solver, where the exact solution is $$ y=left(x+1
ight)-frac{1}{3}{e}^x $$. The code should print the solution at x = 5 (y(5)) to the screen.

Solution 4-10. The following code can be used to accomplish the given tasks.

Example4p10.m
%Example4p10
%This code uses ode45 function
format long
y = 2.0/3.0; %initial condition
x0=0; xF =5;
f = @MyFunc;
xval = [0,5];% 0<=x<=5
[t,Result]=ode45(f,xval,y);
Lengt = length(Result);
fprintf("ode45 Solution of y(5): %.10f ", Result(Lengt));
y_exact = 5.0 + 1.0 - (1.0/3.0)*exp(5.0);
fprintf("Exact Solution of y(5): %.10f ",y_exact);
function Res = MyFunc(x,y)
Res = y-x; %defined function
end
In the code, the precision is set to show 10 decimal digits and the function ode45 is called by an anonymous function. Once we run the code, the following output is displayed on the screen.
> Example4p10
ode45 Solution of y(5): -43.4717851406
Exact Solution of y(5): -43.4710530342
>

Euler’s Method

Euler’s method is yet another way to find approximate solutions to initial value problems of ODEs of the following type:
$$ Big{frac{dy}{dx}=fleft(x,y
ight) y(a)={y}_a $$
$$ {y}_{i+1}={y}_i+ hfleft({x}_i,{y}_i
ight) $$

where h is the mesh size (in a uniform mesh) over the interval [a, b].

Example 4-11. Write code to solve the initial value problem given by
$$ frac{dy}{dx}=y-x,y(0)=frac{2}{3},0le xle 5 $$

using Euler’s method, where the exact solution is $$ y=left(x+1
ight)-frac{1}{3}{e}^x $$. The code should print the exact solution and the numerical solution of y(5) on the screen. The mesh size (h) should be set to h = 0.0005.

Solution 4-11. The following code can be used to accomplish the given tasks.

Example4p11.m
%Example4p11
%This code uses Euler's method
format long
f = @(x,y) y-x; %defined function
y = 2.0/3.0; %initial condition
xval = linspace(0,5,10001);% 0<=x<=5
h = xval(2)-xval(1); %h is mesh size and it is uniform
for i=1:length(xval)
    y = y + f(xval(i),y)*h; %Euler formula
end
fprintf("Numerical Solution of y(5): %.10f ", y)
y_exact = 5.0 + 1.0 - (1.0/3.0)*exp(5.0);
fprintf("Exact Solution of y(5)    : %.10f ",y_exact)
In the code, the precision is set to show 10 decimal digits . Once the code is executed, the following output will be shown on the screen.
> Example4p11
Numerical Solution of y(5): -43.4334780673
Exact Solution of y(5)    : -43.4710530342
>

Runge–Kutta Method of Fourth Order

Runge–Kutta methods are another group of methods for solving initial value problems of ODEs. In this section, we consider the fourth-order (RK4) formula, which has the following form:
$$ Big{frac{dy}{dx}=fleft(x,y
ight) y(a)={y}_a,ale xle b $$
$$ Big{{K}_1= hfleft({x}_i,{y}_i
ight) {K}_2= hfleft({x}_i+frac{1}{2}h,{y}_i+frac{1}{2}{K}_1h
ight) {K}_3= hfleft({x}_i+frac{1}{2}h,{y}_i+frac{1}{2}{K}_2h
ight) {K}_4= hfleft({x}_i+h,{y}_i+{K}_3h
ight) {y}_{i+1}={y}_i+frac{1}{6}left({K}_1+2{K}_2+2{K}_3+{K}_4
ight) $$
Example 4-12. Write code to solve the initial value problem given by
$$ frac{dy}{dx}=y-x,y(0)=frac{2}{3},0le xle 5 $$

using RK4, where the exact solution is $$ y=left(x+1
ight)-frac{1}{3}{e}^x $$. The code should print the exact solution and the numerical solution of y(5) on the screen. The mesh size (h) should be set to h = 0.0005.

Solution 4-12. We can write the following code for the given tasks.

Example14p12.m
%Example 4p12
%This example solves a first-order ODE
%by using Runge-Kutta Method of fourth order
format long
f = @(x,y) y-x; %defined function
y = 2.0/3.0; %initial condition
xval = linspace(0,5,10001);% 0<=x<=5
h = xval(2)-xval(1); %h is mesh size and it is uniform
for i=1:length(xval)
    K1 = h*f(xval(i),y);
    K2 = h*f(xval(i) + 0.5*h, y + 0.5*K1*h);
    K3 = h*f(xval(i) + 0.5*h, y + 0.5*K2*h);
    K4 = h*f(xval(i) + h, y + K3*h);
    y = y + (1.0/6.0)*(K1 + 2.0*K2 + 2.0*K3 + K4);%RK4
end
fprintf("Numerical Solution of y(5): %.10f ", y)
y_exact = 5.0 + 1.0 - (1.0/3.0)*exp(5.0);
fprintf("Exact Solution of y(5)    : %.10f ",y_exact)
In this code, the precision is set to show 10 decimal digits. Once the code is executed, the following output will be displayed on the screen.
> Example4p12
Numerical Solution of y(5): -43.4703160436
Exact Solution of y(5)    : -43.4710530342
>

Problems

  • 4.1. Given the values x = [1, 4, 8, 9] and y = [15, 30, 50, 60], calculate the value of 6 using the default method of the interp1 function.

  • 4.2. Write code to find the root of 3 sin (x). The code should find the root within the interval $$ frac{pi }{2} $$ and $$ frac{3pi }{2} $$. The obtained value should be displayed on the screen in degrees.

  • 4.3. Write code to find the root of f(x) = x4 + 4x + 2 between -1 and 1. The code should calculate the value within the tolerance given by 10−6 using the bisection method.

  • 4.4. Write code to find the root of f(x) = x4 + 4x + 2 with an initial value of -1. The code should calculate the value within the tolerance given by 10−6 using Newton’s method.

  • 4.5. Write code to find the root of f(x) = x4 + 4x + 2 with an initial value of -0.5. The code should calculate the value within the tolerance given by 10−6 using the fixed-point iteration method.

  • 4.6. Write code to calculate the integral of sin(2x) from 0 to π numerically using the integral function. Then, compare the numerical solution with the analytical solution. The code should print the numerical result, analytical result, and the discrepancy on the screen.

  • 4.7. Write code to solve the initial value problem given by

$$ frac{dy}{dx}=2{x}^2+7x+6-y,y(0)=3,0le xle 5 $$
  • using Euler’s method. The code should print the numerical solution of y(5) on the screen. The mesh size (h) should be set to h = 0.0005.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.94.79