0% found this document useful (0 votes)
10 views30 pages

Lab 8 Polynomials, Curve Fitting, and Interpolation

The document discusses the use of polynomials in MATLAB for problem solving, curve fitting, and interpolation. It covers polynomial representation, value calculation, roots, operations, derivatives, and the curve fitting process using the polyfit function. Additionally, it explores fitting non-polynomial functions to data and determining the best-fit coefficients through various methods.

Uploaded by

Darren O'Brien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

Lab 8 Polynomials, Curve Fitting, and Interpolation

The document discusses the use of polynomials in MATLAB for problem solving, curve fitting, and interpolation. It covers polynomial representation, value calculation, roots, operations, derivatives, and the curve fitting process using the polyfit function. Additionally, it explores fitting non-polynomial functions to data and determining the best-fit coefficients through various methods.

Uploaded by

Darren O'Brien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

LAB 8 POLYNOMIALS, CURVE FITTING, AND

INTERPOLATION
Polynomials are mathematical expressions that are frequently used for problem solving and
modeling in science and engineering. In many cases an equation that is written in the
process ofsolving a problem is a polynomial, and the solution of the problem is the zero of
the polynomial. MATLAB has a wide selection of functions that are specifically designed for
handling polynomials.How to use polynomials in MATLAB is described in Section 8.1.

Curve fitting is a process of finding a function that can be used to model data. The function
does notnecessarily pass through any of the points, but models the data with the smallest
possible error. There are no limitations to the type of the equations that can be used for
curve fitting. Often, however, polynomial, exponential, and power functions are used. In
MATLAB curve fitting can be done by writing a program or by interactively analyzing data
that is displayed in the Figure Window. Section 8.2 describes how to use MATLAB
programming for curve fitting with polynomials and other functions. Section 8.4 describes
the basic fitting interface that is used for interactive curve fitting and interpolation.

Interpolation is the process of estimating values between data points. The simplest kind of
interpolation is done by drawing a straight line between the points. In a more sophisticated
interpolation, data from additional points is used. How to interpolate with MATLAB is
discussed in Sections 8.3 and 8.4.

8.1 POLYNOMIALS
Polynomials are functions that have the form:

The coefficients an, an−1, ..., a1, a0 are real numbers, and n which is a nonnegative integer,
is the degree, or order, of the polynomial.

Examples of polynomials are:

f(x) = 5x5 + 6x2 + 7x + 3 polynomial of degree 5.


f(x) = 2x2 – 4x + 10 polynomial of degree 2.
f(x) = 11x – 5 polynomial of degree 1.

A constant (e.g., f(x) = 6) is a polynomial of degree 0.

In MATLAB, polynomials are represented by a row vector in which the elements are the
coefficients an, an−1, ..., a1, a0 . The first element is the coefficient of the x with the highest
power. The vector has to include all the coefficients, including the ones that are equal to 0.
For example:

8.1.1 Value of a Polynomial


The value of a polynomial at a point x can be calculated with the function polyval that has the
form:

x can also be a vector or a matrix. In such a case the polynomial is calculated for each
element (element-by-element), and the answer is a vector, or a matrix, with the
corresponding values of the polynomial.

Sample Problem 8-1: Calculating polynomials with MATLAB


For the polynomial f (x) = x5 − 12.1x4 + 40.59x3 − 17.015x2 − 71.95x + 35.88 :
Calculate f(9).

Plot the polynomial for −1.5 ≤ x ≤ 6.7 .

Solution
The problem is solved in the Command Window.
The coefficients of the polynomials are assigned to vector p. The function
polyval is then used to calculate the value at x = 9.
To plot the polynomial, a vector x is first defined with elements ranging from –1.5
to 6.7. Then a vector y is created with the values of the polynomial for every
element of x. Finally, aplot of y vs. x is made.

The plot created by MATLAB is presented below (axis labels were added with the Plot
Editor).

8.1.2 Roots of a Polynomial


The roots of a polynomial are the values of the argument for which the value of the
polynomial is equal to zero. For example, the roots of the polynomial f(x) = x2 − 2x − 3
are the values of x for which x2 − 2x − 3 = 0 , which are x = – 1 and x = 3.

MATLAB has a function, called roots, that determines the root, or roots, of a polynomial. The
form of the function is:
For example, the roots of the polynomial in Sample Problem 8-1 can be determined by:

The roots command is very useful for finding the roots of a quadratic equation. For example,
to find the roots of f(x) = 4x2 + 10x − 8 , type:

When the roots of a polynomial are known, the poly command can be used for determining
the coefficients of the polynomial. The form of the poly command is:

For example, the coefficients of the polynomial in Sample Problem 8-1 can be obtained from
the roots of the polynomial (see above) by:
8.1.3 Addition, Multiplication, and Division of Polynomials

Addition:
Two polynomials can be added (or subtracted) by adding (subtracting) the vectors of the
coefficients.If the polynomials are not of the same order (which means that the vectors of the
coefficients are notof the same length), the shorter vector has to be modified to be of the
same length as the longer vector by adding zeros (called padding) in front. For example, the
polynomials

f1 (x) = 3x6 + 15x5 − 10x3 − 3x2 + 15x − 40 and f2 (x) = 3x3 − 2x − 6 can be
added by:

Multiplication:
Two polynomials can be multiplied using the MATLAB built-in function conv, which has the
form:

• The two polynomials do not have to be of the same order.


• Multiplication of three or more polynomials is done by using the conv function
repeatedly.

For example, multiplication of the polynomials f1(x) and f2(x) above gives:

which means that the answer is:


Division:
A polynomial can be divided by another polynomial with the MATLAB built-in function deconv,
whichhas the form:

For example, dividing 2x3 + 9x2 + 7x − 6 by x + 3 is done by:

An example of division that gives a remainder is 2x6 − 13x5 + 75x3 + 2x2 − 60


divided by x2– 5:

The answer is:


8.1.4 Derivatives of Polynomials
The built-in function polyder can be used to calculate the derivative of a single polynomial, a
productof two polynomials, or a quotient of two polynomials, as shown in the following three
commands.
k = Derivative of a single polynomial. p is a vector with the coefficients of the
polyder(p) polynomial. k is a vector with the coefficients of the polynomial that is the
derivative.
k = Derivative of a product of two polynomials. a and b are vectors with the
polyder(a,b) coefficients of the polynomials that are multiplied. k is a vector with the
coefficients of the polynomial that is the derivative of the product.
[n d] = Derivative of a quotient of two polynomials. u and v are vectors with the
polyder(u,v) coefficients of the numerator and denominator polynomials. n and d are
vectors with the coefficients of the numerator and denominator polynomials
in the quotient that is the derivative.

The only difference between the last two commands is the number of output arguments.
With two output arguments MATLAB calculates the derivative of the quotient of two
polynomials. With one output argument, the derivative is of the product.

For example, if , and the derivatives of


, and can be determined by:

8.2 CURVE FITTING


Curve fitting, also called regression analysis, is a process of fitting a function to a set of data
points. The function can then be used as a mathematical model of the data. Since there are
many types offunctions (linear, polynomial, power, exponential, etc.), curve fitting can be a
complicated process. Many times one has some idea of the type of function that might fit the
given data and will need onlyto determine the coefficients of the function. In other situations,
where nothing is known about the data, it is possible to make different types of plots that
provide information about possible forms of functions that might fit the data well. This
section describes some of the basic techniques for curve fitting and the tools that MATLAB
has for this purpose.

8.2.1 Curve Fitting with Polynomials; The polyfit Function


Polynomials can be used to fit data points in two ways. In one the polynomial passes
through all thedata points, and in the other the polynomial does not necessarily pass through
any of the points butoverall gives a good approximation of the data. The two options are
described below.

Polynomials that pass through all the points:


When n points (xi, yi) are given, it is possible to write a polynomial of degree n – 1 that
passes through all the points. For example, if two points are given it is possible to write a
linear equation in the form of y = mx + b that passes through the points. With three points,
the equation has the form of y = ax2 + bx + c . With n points the polynomial has the

form an−1xn−1 + an−2xn−2 + … + a1x + a0 . The coefficients of the polynomial are


determined by substituting each point in the polynomial and then solving the n equations for
the coefficients. As will be shown later in this section, polynomials of high degree might give
a large error if they areused to estimate values between data points.

Polynomials that do not necessarily pass through any of the points:


When n points are given, it is possible to write a polynomial of degree less than n – 1 that
does not necessarily pass through any of the points but that overall approximates the data.
The most commonmethod of finding the best fit to data points is the method of least
squares. In this method, the coefficients of the polynomial are determined by minimizing the
sum of the squares of the residuals at all the data points. The residual at each point is
defined as the difference between the value of thepolynomial and the value of the data. For
example, consider the case of finding the equation of a straight line that best fits four data
points as shown in Figure 8-1. The points are (x1,y1), (x2,y2), (x3,y3), and (x4,y4), and the
polynomial of the first degree can be written as f(x) = a1x + a0. The residual, Ri, at each
point is the difference between the value of the function at xi and yi , Ri = f(xi) − yi . An
equation for the sum of the squares of the residuals Ri of all the points is given by:

or, after substituting the equation of the polynomial at each point, by:
Figure 8-1: Least squares fitting of first-degree polynomial to four points.

At this stage R is a function of a1 and a0. The minimum of R can be determined by taking the
partial derivative of R with respect to a1 and a0 (two equations) and equating them to zero:

This results in a system of two equations with two unknowns, a1 and a0. The solution of
these equations gives the values of the coefficients of the polynomial that best fits the data.
The same procedure can be followed with more points and higher-order polynomials. More
details on the least squares method can be found in books on numerical analysis.

Curve fitting with polynomials is done in MATLAB with the polyfit function, which uses the
least squares method. The basic form of the polyfit function is:
For the same set of m points, the polyfit function can be used to fit polynomials of any order
up to m– 1. If n = 1 the polynomial is a straight line, if n = 2 the polynomial is a parabola,
and so on. The polynomial passes through all the points if n=m – 1 (the order of the
polynomial is one less than the number of points). It should be pointed out here that a
polynomial that passes through all the points,or polynomials with higher order, do not
necessarily give a better fit overall. High-order polynomialscan deviate significantly between
the data points.

Figure 8-2 shows how polynomials of different degrees fit the same set of data points. A set
of sevenpoints is given by (0.9, 0.9), (1.5, 1.5), (3, 2.5), (4, 5.1), (6, 4.5), (8, 4.9), and (9.5,
6.3). The points are fitted using the polyfit function with polynomials of degrees 1 through 6.
Each plot in Figure 8-2 shows the same data points, marked with circles, and a curve-fitted
line that corresponds to a polynomial of the specified degree. It can be seen that the
polynomial with n = 1 is a straight line, and that with n = 2 is a slightly curved line. As the
degree of the polynomial increases, the line develops more bends such that it passes closer
to more points. When n = 6, which is one less than the number of points, the line passes
through all the points. However, between some of the points, the line deviates significantly
from the trend of the data.
Figure 8-2: Fitting data with polynomials of different order.

The script file used to generate one of the plots in Figure 8-2 (the polynomial with n = 3) is
shown below. Note that in order to plot the polynomial (the line), a new vector xp with small
spacing is created. This vector is then used with the function polyval to create a vector yp with
the value of thepolynomial for each element of xp.

When the script file is executed, the following vector p is displayed in the Command Window.
This means that the polynomial of the third degree in Figure 8-2 has the form
0.022x3 − 0.4005x2 + 2.6138x − 1.4148 .

8.2.2 Curve Fitting with Functions Other than Polynomials


Many situations in science and engineering require fitting functions that are not polynomials
to given data. Theoretically, any function can be used to model data within some range. For
a particular dataset, however, some functions provide a better fit than others. In addition,
determining the best-fitting coefficients can be more difficult for some functions than for
others. This section covers curve fitting with power, exponential, logarithmic, and reciprocal
functions, which are commonly used. The forms of these functions are:

All of these functions can easily be fitted to given data with the polyfit function. This is done
byrewriting the functions in a form that can be fitted with a linear polynomial (n = 1), which is

The logarithmic function is already in this form, and the power, exponential, and reciprocal
equations can be rewritten as:

These equations describe a linear relationship between ln(y) and ln(x) for the power
function, between ln(y) and x for the exponential function, between y and ln(x) or log(x) for
the logarithmic function, and between 1/y and x for the reciprocal function. This means that
the polyfit(x,y,1) function can be used to determine the best-fit constants m and b for best
fit if, instead of x and y, the following arguments are used.
The result of the polyfit function is assigned to p, which is a two-element vector. The first
element, p(1), is the constant m, and the second element, p(2), is b for the logarithmic and
reciprocal functions, ln(b) or log(b) for the exponential function, and ln(b) for the power
function (b = ep(2) or b = 10p(2) forthe exponential function, and b = ep(2) for the power
function).

For given data it is possible to estimate, to some extent, which of the functions has the
potential forproviding a good fit. This is done by plotting the data using different
combinations of linear and logarithmic axes. If the data points in one of the plots appear to fit
a straight line, the correspondingfunction can provide a good fit according to the list below.
x axis y axis Function
linear linear linear y = mx + b
logarithmic logarithmic power y = bxm
linear logarithmic exponential y = bemx or y = b10mx
logarithmic linear logarithmic y = mln(x) + b or y = mlog(x) + b
linear linear (plot 1/ y) reciprocal y = 1
mx+b

Other considerations in choosing a function:


• Exponential functions cannot pass through the origin.
• Exponential functions can fit only data with all positive y’s or all negative y’s.
• Logarithmic functions cannot model x = 0 or negative values of x.
• For the power function y = 0 when x = 0.
• The reciprocal equation cannot model y = 0.

The following example illustrates the process of fitting a function to a set of data points.

Sample Problem 8-2: Fitting an equation to data points


The following data points are given. Determine a function w = f(t) (t is the independent
variable, w is the dependent variable) with a form discussed in this section that best fits the
data.
t 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
w 6.00 4.83 3.70 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64

Solution
The data is first plotted with linear scales on both axes. The figure indicates that a linear
function will not give the best fit since the points do not appear to line up along a straight
line. From the other possible functions, the logarithmic function is excluded since for the first
point t =0, and the power function is excluded since at t = 0, w ≠ 0. To check if the other two
functions (exponential and reciprocal) might give a better fit, two additional plots, shown
below, are made.The plot on the left has a log scale on the vertical axis and linear
horizontal axis. In the plot on the right, both axes have linear scales, and the quantity 1/ w is
plotted on the vertical axis.

In the left figure, the data points appear to line up along a straight line. This indicates that an
exponential function of the form y = bemx can give a good fit to the data. A program in a
scriptfile that determines the constants b and m, and that plots the data points and the
function is given below.
When the program is executed, the values of the constants m and b are displayed in the
Command Window.

The plot generated by the program, which shows the data points and the function (with axis
labels added with the Plot Editor) is

It should be pointed out here that in addition to the power, exponential, logarithmic, and
reciprocal functions that are discussed in this section, many other functions can be written in
a form suitable forcurve fitting with the polyfit function. One example where a function of
the form y = e(a2x + a1x + a0)is fitted to data points using the
2
polyfit function with a third-
order polynomial is described in Sample Problem 8-7.

8.3 INTERPOLATION
Interpolation is the estimation of values between data points. MATLAB has interpolation
functionsthat are based on polynomials, which are described in this section, and on Fourier
transformation,which is outside the scope of this course. In one-dimensional interpolation,
each point has one independent variable (x) and one dependent variable (y). In two-
dimensional interpolation, each point has two independent variables (x and y) and one
dependent variable (z).

One-dimensional interpolation:
If only two data points exist, the points can be connected with a straight line and a linear
equation (polynomial of first order) can be used to estimate values between the points. As
was discussed in the previous section, if three (or four) data points exist, a second- (or a
third-) order polynomial that passes through the points can be determined and then be used
to estimate values between the points. As the number of points increases, a higher-order
polynomial is required for the polynomial topass through all the points. Such a polynomial,
however, will not necessarily give a good approximation of the values between the points.
This is illustrated in Figure 8-2 with n = 6.
A more accurate interpolation can be obtained if instead of considering all the points in the
data set(by using one polynomial that passes through all the points), only a few data points
in the neighborhood where the interpolation is needed are considered. In this method,
called spline interpolation, many low-order polynomials are used, where each is valid only in
a small domain of the data set.
The simplest method of spline interpolation is called linear spline interpolation. In this
method, shown below, every two adjacent points are connected with a straight line (a
polynomial of first degree). The equation of a straight line that passes through two adjacent
points (xi , yj ) and (xi+1, yj+1) and that can be used to calculate the value of y for any x
between the points is given by:

In a linear interpolation, the line between two data points has a constant slope, and there is a
changein the slope at every point. A smoother interpolation curve can be obtained by using
quadratic or cubic polynomials. In these methods, called quadratic splines and cubic
splines, a second-, or third- order polynomial is used to interpolate between every two
points. The coefficients of the polynomial are determined by using data from points that are
adjacent to the two data points. The theoretical background for the determination of the
constants of the polynomials is beyond the scope of this course and can be found in books
on numerical analysis.

One-dimensional interpolation in MATLAB is done with the interp1 (the last character is the
numeral one) function, which has the form:

• The vector x must be monotonic (with elements in ascending or descending order).


• xi can be a scalar (interpolation of one point) or a vector (interpolation of many
points). yi is ascalar or a vector with the corresponding interpolated values.
• MATLAB can do the interpolation using one of several methods that can be
specified. These methods include:
‘nearest’ returns the value of the data point that is nearest to the interpolated point.
‘linear’ uses linear spline interpolation.
‘spline’ uses cubic spline interpolation.
‘pchip’ uses piecewise cubic Hermite interpolation, also called ‘cubic’

• When the ‘nearest’ and the ‘linear’ methods are used, the value(s) of xi must be
within thedomain of x. If the ‘spline’ or the ‘pchip’ methods are used, xi can have
values outside the domain of x and the function interpl performs extrapolation.
• The ‘spline’ method can give large errors if the input data points are nonuniform
such that somepoints are much closer together than others.
• Specification of the method is optional. If no method is specified, the default is
‘linear’.

Sample Problem 8-3: Interpolation


The following data points, which are points of the function f(x) = 1.5x cos(2x), are given. Use
linear, spline, and pchip interpolation methods to calculate the value of y between the
points.Make a figure for each of the interpolation methods. In the figure show the points, a
plot of thefunction, and a curve that corresponds to the interpolation method.
x 0 1 2 3 4 5
y 1.0 –0.6242 –1.4707 3.2406 –0.7366 –6.3717

Solution
The following is a program written in a script file that solves the problem:

The three figures generated by the program are shown below (axes labels were added with
thePlot Editor). The data points are marked with circles, the interpolation curves are plotted
with dashed lines, and the function is shown with a solid line. The left figure shows the
linear interpolation, the middle is the spline, and the figure on the right shows the pchip
interpolation.
8.4 THE BASIC FITTING INTERFACE
The basic fitting interface is a tool that can be used to perform curve fitting and interpolation
interactively. By using the interface the user can:
• Curve-fit the data points with polynomials of various degrees up to 10, and with
spline andHermite interpolation methods.
• Plot the various fits on the same graph so that they can be compared.
• Plot the residuals of the various polynomial fits and compare the norms of the
residuals.
• Calculate the values of specific points with the various fits.
• Add the equations of the polynomials to the plot.
To activate the basic fitting interface, the user first has to make a plot of the data points. Then
the interface is activated by selecting Basic Fitting in the Tools menu, as shown above.
This opens the Basic Fitting Window, shown in Figure 8-3. When the window first opens,
only one panel (the Plot fits panel) is visible. The window can be extended to show a
second panel (the Numericalresults panel) by clicking on the button. One click adds
the first section of the panel, and a second click makes the window look as shown in Figure
8-3. The window can be reduced back by clicking on the button. The first two items in
the Basic Fitting Window are related to the selectionof the data points:

Select data: Used to select a specific set of data points for curve fitting in a figure that has
more than one set of data points. Only one set of data points can be curve-fitted at a time,
but multiple fitscan be performed simultaneously on the same set.

Center and scale x data: When this box is checked, the data is centered at zero mean and
scaledto unit standard deviation. This might be needed in order to improve the accuracy of
numerical computation.

The next four items are in the Plot fits panel and are related to the display of the fit.

Check to display fits on figure: The user selects the fits to be displayed in the figure. The
selections include interpolation with spline interpolant (interpolation method) that uses the
splinefunction, interpolation with Hermite interpolant that uses the pchip function, and
polynomials of various degrees that use the polyfit function. Several fits can be selected
and displayed simultaneously.
Figure 8-3: The Basic Fitting Window.

Show equations: When this box is checked, the equations of the polynomials that were
selected forthe fit are displayed in the figure. The equations are displayed with the number
of significant digits selected in the adjacent sign menu.

Plot residuals: When this box is checked, a plot that shows the residual at each data point
is created (residuals are defined in Section 8.2.1). Choices in the menus include a bar plot,
a scatter plot, and a line plot that can be displayed as a subplot in the same Figure Window
that has the plotof the data points or as a separate plot in a different Figure Window.

Show norm of residuals: When this box is checked, the norm of the residuals is displayed
in theplot of the residuals. The norm of the residual is a measure of the quality of the fit. A
smaller norm corresponds to a better fit.

The next three items are in the Numerical results panel. They provide the numerical
information forone fit, independently of the fits that are displayed:

Fit: The user selects the fit to be examined numerically. The fit is shown on the plot only if it
isselected in the Plot fit panel.

Coefficients and norm of residuals: Displays the numerical results for the polynomial fit
that is selected in the Fit menu. It includes the coefficients of the polynomial and the norm of
the residuals.The results can be saved by clicking on the Save to workspace button.

Find y = f(x): Provides a means for obtaining interpolated (or extrapolated) numerical
values for specified values of the independent variable. Enter the value of the independent
variable in the box,and click on the Evaluate button. When the Plot evaluated results box
is checked, the point is displayed on the plot.

As an example, the basic fitting interface is used for fitting the data points from Sample
Problem 8-3. The Basic Fitting Window is the one shown in Figure 8-3, and the
corresponding Figure Window is shown in Figure 8-4. The Figure Window includes a plot of
the points, one interpolation fit (spline), two polynomial fits (linear and cubic), a display of the
equations of the polynomial fits, and a mark ofthe point x = 1.5 that is entered in the Find y
= f(x) box of the Basic Fitting Window. The Figure Window also includes a plot of the
residuals of the polynomial fits and a display of their norm.

Figure 8-4: A Figure Window modified by the Basic Fitting Interface.


8.5 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 8-4: Determining wall thickness of a box

The outside dimensions of a rectangular box (bottom and four sides, no top), made of
aluminum, are 24 by 12 by 4 inches. The wall thickness of the bottom and the sides is x.
Derivean expression that relates the weight of the box and the wall thickness x. Determine
the thickness x for a box that weighs 15 lb. The specific weight of aluminum is 0.101 lb/in.3.

Solution
The volume of the aluminum VAl is calculated from the weight W of the box by:

where γ is the specific weight. The volume of the aluminum based on the dimensions of the
boxis given by

where the inside volume of the box is subtracted from the outside volume. This equation can
berewritten as
which is a third-degree polynomial. A root of this polynomial is the required thickness x. A
program in a script file that determines the polynomial and solves for the roots is:

Note in the second-to-last line that in order to add the quantity VAI − 24 ⋅ 12 ⋅ 4 to the
polynomial Vin it has to be written as a polynomial of the same order as Vin (Vin is a
polynomial of third order). When the program (saved as Chap8SamPro4) is executed, the
coefficients ofthe polynomial and the value of x are displayed:

Sample Problem 8-5: Floating height of a buoy

An aluminum thin-walled sphere is used as a marker buoy. The sphere has a radius of 60
cm and a wall thickness of 12 mm. The density of aluminum is ρAl = 2690 kg/m3. The buoy
is placed in the ocean, where the density of the water is 1030 kg/m 3. Determine the height h
between the top of the buoy and the surface of the water.

Solution
According to Archimedes’s law, the buoyancy force applied to an object that is placed in a
fluidis equal to the weight of the fluid that is displaced by the object. Accordingly, the
aluminum sphere will be at a depth such that the weight of the sphere is equal to the weight
of the fluid displaced by the part of the sphere that is submerged.

The weight of the sphere is given by

where VAl is the volume of the aluminum; ro and ri are the outside and inside radii of the
sphere,respectively; and g is the gravitational acceleration.

The weight of the water that is displaced by the spherical portion that is submerged is given
by:Setting the two weights equal to each other gives the following equation:

The last equation is a third-degree polynomial for h. The root of the polynomial is the answer.

A solution with MATLAB is obtained by writing the polynomials and using the roots function
to determine the value of h.This is done in the following script file:

When the script file is executed in the Command Window, as shown below, the answer is
threeroots, since the polynomial is of the third degree. The only answer that is physically
possible isthe second, where h = 0.9029 m.

Sample Problem 8-6: Determining the size of a capacitor


An electrical capacitor has an unknown capacitance. In order to determine its capacitance,
thecapacitor is connected to the circuit shown. The switch is first connected to B and the
capacitor is charged. Then, the switch is connected to A and the capacitor discharges
through the resistor. As the capacitor is discharging, the voltage across the capacitor is
measured for 10 s in intervals of 1 s. The recorded measurements are given in the table
below. Plot the voltage asa function of time and determine the capacitance of the capacitor
by fitting an exponential curveto the data points.

t (s) 1 2 3 4 5 6 7 8 9 10
V (V). 9.4 7.31 5.15 3.55 2.81 2.04 1.26 0.97 0.74 0.58

Solution
When a capacitor discharges through a resistor, the voltage of the capacitor as a function of
time is given by

where V0 is the initial voltage, R the resistance of the resistor, and C the capacitance of the
capacitor. As was explained in Section 8.2.2 the exponential function can be written as a
linearequation for ln( V ) and t in the form:
This equation, which has the form y = mx + b, can be fitted to the data points by using the
polyfit(x,y,1) function with t as the independent variable x and ln( V ) as the dependent
variable y. The coefficients m and b determined by the polyfit function are then used to
determine C and V0 by:

The following program written in a script file determines the best-fit exponential function to
the data points, determines C and V0, and plots the points and the fitted function.

When the script file is executed (saved as Chap8SamPro6) the values of C and V0 are
displayed in the Command Window as shown below:

The program creates also the following plot (axis labels were added to the plot using the Plot
Editor):
Sample Problem 8-7: Temperature dependence of viscosity
Viscosity, μ, is a property of gases and fluids that characterizes their resistance to flow. For
most materials, viscosity is highly sensitive to temperature. Below is a table that gives the
viscosity of SAE 10W oil at different temperatures (data from B.R. Munson, D.F. Young, and
T.H. Okiishi, Fundamentals of Fluid Mechanics, 4th ed., John Wiley and Sons, 2002).
Determine an equation that can be fitted to the data.
T (°C) –20 0 20 40 60 80 100 120
μ (N s/m2) (× 10–5) 4 0.38 0.095 0.032 0.015 0.0078 0.0045 0.0032

Solution
To determine what type of equation might provide a good fit to the data, μ is plotted as a
function of T (absolute temperature) with a linear scale for T and a logarithmic scale for μ.
Theplot, shown on the right, indicates that the data points do not appear to line up along a
straightline. This means that a simple exponential function of the form y = bemx, which
models a straight line with these axes, will not provide the best fit. Since the points in the
figure appear tolie along a curved line, a function that can possibly have a good fit to the
data is:
This function can be fitted to the data by using MATLAB’s polyfit(x,y,2) function (second-
degree polynomial), where the independent variable is T and the dependent variable is ln( μ
).The equation above can be solved for μ to give the viscosity as a function of temperature:

The following program determines the best fit to the function and creates a plot that displays
thedata points and the function.

When the program executes (saved as Chap8SamPro7), the coefficients that are determined
bythe polyfit function are displayed in the Command Window (shown below) as three
elements ofthe vector p.

With these coefficients the viscosity of the oil as a function of temperature is:
The plot that is generated shows that the equation correlates well to the data points (axis
labelswere added with the Plot Editor).

You might also like