Solving An Equation With One Variable: Lab Session 12
Solving An Equation With One Variable: Lab Session 12
1
• When a function is entered as an expression (string), it cannot include predefined variables.
For example, if the function to be entered is 𝑓(𝑥) = 𝑥𝑒−𝑥 − 0.2 , it is not possible to define
b=0. 2 and then enter 'x*exp (-x) -b'.
• x0 can be a scalar or a two-element vector. If it is entered as a scalar, it has to be a value of
x near the point where the function crosses (or touches) the x-axis. If x0 is entered as a
vector, the two elements have to be points on opposite sides of the solution. If f(x) crosses
the x-axis, then f(x0(1)) has a different sign than f(x0(2)). When a function has more than
one solution, each solution can be determined separately by using the fzero function and
entering values for x0 that are near each of the solutions.
• A good way to find approximately where a function has a solution is to make a plot of the
function. In many applications in science and engineering the domain of the solution can
be estimated. Often when a function has more than one solution only one of the solutions
will have a physical meaning.
Additional Comments
• The fzero command finds zeros of a function only where the function crosses the x-axis.
The command does not find a zero at points where the function touches but does not cross
the x-axis.
• If a solution cannot be determined, NaN is assigned to x.
• The fzero command has additional options. Two of the more important options are:
1. [x fval] =fzero (function, x0) assigns the value of the function at x to the variable
fval.
2. x=fzero (function, x0, optimset ('display',' iter')) displays the output of each iteration
during the process of finding the solution
• When the function can be written in the form of a polynomial, the solution, or the roots,
can be found with the roots command.
• The fzero command can also be used to find the value of x where the function has a specific
value. This is done by translating the function up or down. For example, in the following
function of the first value of x where the function is equal to 0.1 can be determined by
solving the equation 𝑥𝑒−𝑥 − 0.3 = 0. This is shown below:
2
Finding the Minima and Maxima of a Function
In many applications there is a need to determine the local minimum or maximum of a
function of the form 𝑦 = 𝑓(𝑥). In calculus the value of x that corresponds to a local minimum or
maximum is determined by finding the zero of the derivative of the function. The value of y is
determined by substituting the x into the function. In MATLAB the value of x where a one-variable
function f(x) within the interval x1 ≤ x ≤ x2 has a minimum can be determined with the fminbnd
command which has the form:
• The function can be entered as a string expression, or as a function handle, in the same way
as with the fzero command.
• The value of the function at the minimum can be added to the output by using the option
where
the value of the function at x is assigned to the variable fval
Within a given interval, the minimum of a function can either be at one of the end points of
the interval or at a point within the interval where the slope of the function is zero (local minimum).
When the fminbnd command is executed, MATLAB looks for a local minimum. If a local
minimum is found, its value is compared to the value of the function at the end points of the
interval. MATLAB returns the point with the actual minimum value for the interval.
For example, consider the function 𝑓(𝑥) = 𝑥3 − 12𝑥2 + 40.25𝑥 − 36.5 which is plotted in
the interval 0 ≤ x ≤ 8 in the figure.
3
It can be observed that there is a local minimum between 5 and 6, and that the absolute
minimum is at x = 0. Using the fminbnd command with the interval 3 ≤ x ≤ 8 to find the location
of the local minimum and the value of the function at this point gives:
Notice that the fminbnd command gives the local minimum. If the interval is changed to 0
≤ x ≤ 8, fminbnd gives:
For this interval the fminbnd command gives the absolute minimum which is at the end
point x= 0.
The fminbnd command can also be used to find the maximum of a function. This is done
by multiplying the function by -1 and finding the minimum. For example, the maximum of the
function 𝑓(𝑥) = 𝑥𝑒−𝑥 − 0.2 in the interval 0 ≤ x ≤ 8 can be determined by finding the minimum of
the function as shown below:
4
Numerical Integration
Integration is a common mathematical operation in science and engineering. Calculating
area and volume, velocity from acceleration, and work from force and displacement are just a few
examples where integrals are used. Integration of simple functions can be done analytically, but
more involved functions are frequently difficult or impossible to integrate analytically. In calculus
courses the integrand (the quantity to be integrated) is usually a function. In applications of science
and engineering the integrand can be a function or a set of data points. For example, data points
from discrete measurements of flow velocity can be used to calculate volume.
The MATLAB uses three built-in integration functions quad, quadl, and trapz. The quad and quadl
commands are used for integration when f(x) is a function, and trapz is used when f(x) is given by
data points.
The quad Command
The form of the quad command, which uses the adaptive Simpson method of integration,
is:
• The function can be entered as a string expression or as a function handle, in the same way
as with the fzero command.
• The function f(x) must be written for an argument x that is a vector (use element-by-element
operations) such that it calculates the value of the function for each element of x.
• The user has to make sure that the function does not have a vertical asymptote between a
and b.
• quad calculates the integral with an absolute error that is smaller than l×10 -6. This number
can be changed by adding an optional tol argument to the command:
5
tol is a number that
defines the maximum error. With larger tol the integral is calculated less accurately but faster.
The form of the quadl command is exactly the same as that of the quad command:
All of the comments that are listed for the quad command are valid for the quadl command.
The difference between the two commands is the numerical method used for calculating the
integration. The quadl command uses the adaptive Lobatto method, which can be more efficient
for high accuracies and smooth integrals .
The trapz Command
The trapz command can be used for integrating a function that is given as data points. It
uses the numerical trapezoidal method of integration. The form of the command is
where x and y are vectors with the x and y coordinates of the points, respectively. The two vectors
must be of the same length.
6
An ordinary differential equation (ODE) is an equation that contains an independent
variable, a dependent variable, and derivatives of the dependent variable. The equations that are
considered here are of first order with the form:
where x and y are the independent and dependent variables, respectively. A solution is a function
y = f(x) that satisfies the equation. In general, many functions can satisfy a given ODE, and more
information is required for determining the solution of a specific problem. The additional
information is the value of the function (the dependent variable) at some value of the independent
variable.
Steps for Solving a Single First Order ODE Step-I: Write the Problem in a Standard
Form
As shown above, three pieces of information are needed for solving a first order ODE: An
equation that gives an expression for the derivative of y with respect to t, the interval of the
independent variable, and the initial value of y. The solution is the value of y as a function of t
between t0 and t1.
Step-II: Create a User-Defined Function (in a Function File) or an Anonymous Function
The ODE to be solved has to be written as a user-defined function (in a function file) or as
an anonymous function. Both calculate for given values of t and y. For the example problem
above, the user-defined function (which is saved as a separate file) is:
7
Step-III: Select a Method of Solution
Select the numerical method that you would like MATLAB to use in the solution. Many
numerical methods have been developed to solve first-order ODEs, and several of the methods are
available as built-in functions in MATLAB. In a typical numerical method, the time interval is
divided into small time steps. The solution starts at the known point y0, and then by using one of
the integration methods the value of y is calculated at each time step. Following table lists seven
ODE solver commands, which are MATLAB built-in functions that can be used for solving a first
order ODE. A short description of each solver is included in the table.
In general, the solvers can be divided into two groups according to their ability to solve stiff
problems and according to whether they use on-step or multistep methods. Stiff problems are ones
that include fast and slowly changing components and require small time steps in their solution.
One-step solvers use information from one point to obtain a solution at the next point. Multistep
solvers use information from several previous points to find the solution at the next point.
8
It is impossible to know ahead of time which solver is the most appropriate for a specific
problem. A suggestion is to first try ode45, which gives good results for many problems. If a
solution is not obtained because the problem is stiff, trying the solver ode15 s is suggested.
Step-IV: Solve the ODE
The form of the command that is used to solve an initial value ODE problem is the same
for all the solvers and for all the equations that are solved. The form is:
Additional Information
solver_name Is the name of the solver (numerical method) that is used (e.g. ode45 or ode23s)
ODEfun
The function from Step 2 that calculates for given values of t and y. If it
was written as a user-defined function, the function handle is entered. If it was
written as an anonymous function, the name of the anonymous function is
entered.
tspan A vector that specifies the interval of the solution. The vector must have at
least two elements but can have more. If the vector has only two elements, the
elements must be [t0 tf] , which are the initial and final points of the solution
interval. The vector tspan can have, however, additional points between the
first and last points. The number of elements in tspan affects the output from
the command.
y0 The initial value of y (the value of y at the first point of the interval).
[t y] The output, which is the solution of the ODE. t and y are column vectors. The
first and the last points are the beginning and end points of the interval. The
spacing and number of points in between depends on the input vector tspan. If
tspan has two elements (the beginning and end points), the vectors t and y
9
contain the solution at every integration step calculated by the solver. If tspan
has more than two points (additional points between the first and the last), the
vectors t and y contain the solution only at these points. The number of points
in tspan does not affect the time steps used for the solution by the program.
Practice Exercise
1. Determine the positive roots of the equation
𝑥2 − 5𝑥𝑠𝑖𝑛(3𝑥) + 3 = 0
10
3. Use MATLAB to calculate the following integral:
i.
ii.
11
12
4. Use a MATLAB built-in function to numerically solve:
In one figure plot the numerical solution as a solid line and the exact solution as discrete points.
13