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
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.
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.
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
In this use, the given function is Myfunction, and the point is defined by x0.
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 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.
As shown in the code, the values of the corresponding roots are converted from radians to degrees by simply multiplying these values by 180/π.
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.
Newton’s Method
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.
Note that the initial value plays an important role in finding the root more quickly.
Secant Method
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.
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).
- a)
g(x) = x2 − 4
- b)
- c)
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.
If we compare the results obtained by the different methods given, Newton’s method yields a slightly better result than the others.
Numerical Integration
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 |
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 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.
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.
using the ode45 solver, where the exact solution is . 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.
Euler’s Method
where h is the mesh size (in a uniform mesh) over the interval [a, b].
using Euler’s method, where the exact solution is . 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.
Runge–Kutta Method of Fourth Order
using RK4, where the exact solution is . 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.
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 and . 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
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.