Vous êtes sur la page 1sur 25

Introduction to Computing with MATLAB

Gemechu Fanta

Ambo University, Inservice Training


Program
November 4, 2011

1 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

outline of lecture 5

Function Finding
Interpolation and Extrapolation
Regression
The Basic Fitting Interface

2 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Function Finding
This is finding a function that can describe a particular set of
data.
Three function types describe physical phenomena
The linear function:
y (x) = mx + b
(1)
Note that y(0) = b.
The power function:
y (x) = bx m

(2)

Note that y(0) = 0 if m 0, and y (0) = if m < 0


The exponential function:
y (x) = b(10)mx
or its equivalent form:
3 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

(3)

y = be mx

(4)

y(0)=b for both forms.


All of these functions can be written in the form
Y = mX + B

(5)

We can find the values of B and m with the MATLAB polyfit


function.
This function finds the coefficients of a polynomial of specified
degree n that best fits the data, in the so called least squares
sense.
The linear function: y = mx + b. The variables w and z in the
polynomial w = p1 z + p2 are the original data variables x and y, and
we can find the linear function that fits the data by typing p =
polyfit(x,y,1).
The first element p1 of the vector p will be m, and the second
element p2 will be b.
4 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

The power function:y = bx m . In this case


log10 y = mlog10 x + log10 b

(6)

w = p1 z + p2

(7)

which has the form

where the polynomial variables wand Z are related to the original


data variables x and y by w = log10 y and z = log10 x.
Thus we can find the power function that fits the data by typing
p = polyfit(log 10(x), log 10(y ),1).
The first element p1 of the vector p will be m, and the second
element p2 will be log10 b.
We can find b from b = 10p2 .
5 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

The exponential function: y = b(10)mx . In this


case
log10 y = mx + log10 b

(8)

w = p1 z + p2

(9)

which has the form

where the polynomial variables w and z are related to the


original data variables x and y by w = log10 y and z = x.
Thus we can find the exponential function that fits the data by
typing p = polyfit(x,log 10(y ),1).
the first element p1 of the vector p will be m, and the second
element p2 will be log10 b.
We can find b from b = 10p2 .
6 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Examples
1

7 / 25

Temperature Dynamics
The temperature of coffee cooling in a porcelain mug at room
temperature (680 F ) was measured at various times. The data
follows,
Time t(sec) Temperature T(0 F )
0
145
620
130
2266
103
3482
90
Develop a model of the coffees temperature as a function of
time and use the model to estimate how long it will take the
temperature to reach 1200 F .

Gemechu Fanta

Introduction to Computing with MATLAB

Solution
% Enter the data.
time = [0,620,2266,3482] ;
temp = [145,130,103,90] ;
%Subtract the room temperature.
temp = temp - 68 ;
% Plot the oata on rectilinear scales.
subplot (2,2, 1 )
plot (time,temp,time,temp,o),
xlabel ( Time (sec) ) , ...
ylabel (Relative Temperature (deg F))
%
% Plot the c (2,2,2)
subplot(2,2,2)
semilogy (time , temp , time , temp , o ) ,
xlabel ( Time (sec) ) , ...
ylabel ( Relative Temperature
(deg Introduction
F) ) to Computing with MATLAB
8 / 25
Gemechu Fanta

The data forms a straight line on the semilog plot only. Thus it can
be describeed with the exponential function T = 68 + b(10)mx .
Using the polyfit command, the following lines can be added to the
script file.
The computed values are m = 1.5557 104 and
b = 77.4469. Thus our derived model is T = 68 + b(10)mt .
To estimate how long it will take for the coffee to cool to 1200 F ,
we must solve the equation 120 = 68 + b(10)mt for t.
The solution is t = [(log10 (120 68) log10 b)]/m.
The MATLAB command to solve this is:
The computed value is t = 1112 sec.

9 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

%
% Plot the c (2,2,2)
subplot(2,2,2)
semilogy (time , temp , time , temp , o ) ,
xlabel ( Time (sec) ) , ...
ylabel ( Relative Temperature (deg F) )
% Fit a straight line to the transformed data
p = polyfit(time,log10(temp),1)
m = p(1)
b =10p(2)
% Compute the time to reach 120 degrees,
t 120 = (log10(120 - 68) -log10(b))/m
% show the derived curve and estimated point on semilog scales.
t = [0:10:4000];
T = 68 + b*10.(m*t);

10 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

subplot(223)
semilogy(t , T-68,time ,temp ,o,t 120, 120-68 , + ),
xlabel (time (sec) ) , ...
ylabel( Relative Temperature (deg F))
% Show the derived curve and estimated point on rectilinear scales.
subplot(224)
plot(t,T,time,temp-68,o,t 120,120,+), xlabel(Time (sec)),...
ylabel(Temperature(deg F))

11 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

12 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Interpolation and Extrapolation


After we discover a functional relation that describes the data,
we can use it to predict conditions that lie within the range of
the original data.
This process is called interpolation
Extrapolation is the process of the model to predict conditions
that lie outside the original data range.
Extrapolation has a use in making tentative predictions, which
must be backed up by testing.
Example
A 15-cup coffee pot was placed under a water faucet and filled to the
15-cup line. With the outlet valve open, the faucets flow rate was
adjusted until the water level remained constant at 15 cups, and the
time for one cup to flow out of the pot was measured. This
experiment was repeated with the pot filled to the various levels
shown in the following table:
13 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Liquid volume V (cups)


15
12
9
6
1

time to fill one cup t (sec)


6
7
8
9

Obtain the relation between the flow


rate and the number of cups in the
pot. (Use Torricellis principle in
equation form f = rV 1/2 , where f is the
flow rate through the outlet valve in
cups per second, V is the volume of
liquid in the pot in cups, and r is a
constant whose vale is to be found.)

14 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

2. The manufacturer wants to make a 36-cup pot using the same


outlet valve but is concerned that a cup will fill too quickly, casuing
spills. Extrapolate the relation developed in part (a) and predict how
long it will take to fill one cup when the pot contains 36 cups
Solution:
1. The valuse for f are obtained from the reciprocal of the given data
for t. That is, f = 1/t cups per second. From the equation we see
that the relation is a power function where exponent is 0.5. Thus if
we plot log10(f) versus log10(V), we should obtain a straight line.
% Data for the problem.
cups = [6, 9, 12,15];
meas times = [9,8,7,6];
meas flow = 1./meas times;
%
% Fit a straight line to the transformed data.
p = polyfit(log10(cups),log10(meas flow),1);
coeffs = [p(1),10p(2)];
m = coeffs(1)
15 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Solution...
b = coeffs(2)
%
% plot the data and the fitted line on a loglog plot
% how well the line fits the data.
x = [6:0.01:40];
y = b*x.m;
subplot(2,1,1)
loglog(x,y,cups,meas flow,0), grid, xlabel(Volume (cups)),...
ylabel(Flow Rate (cups/sec)), axis ([5 15 0.1 0.3])
The computed values are m = 0.433 and b = 0.0499, and our
derived relation is f = 0.0499V 0.433 .
Because the exponent is 0.433, not 0.5, our model does not
agree exactly with Torricellis principle, but is close.
16 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

2. Note that the fill time is 1/f, the reciprocal of the flow rate. The
remainder of the MATLAB script uses the derived flow rate relation
f = 0.0499V 0.433 to plot the extrapolated fill-time curve 1/f versus t.
% Plot the fill time curve extrapolated to 36 cups.
subplot(2,1,2)
plot(x,1./y, cups,meas times,o), grid, xlabel(Volume(cups)),...
ylabel(Fill time per Cup(sec)), axis([5 36 0 10])
%
% Compute the fill time for V = 36 cups.
V = 36 ;
f 36 = b*Vm
The predicted fill time for one cup is 4.2 sec.

17 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Regression
Analysis in experiments involving two variables, say, x and y
I

Correlation analysis, both variables are random and we are


interested in finding the relation between them
Regression analysis, one of the variables are regarded as an
ordinary variable. We can assign it whatever values we want .
The variable x is the independent or controlled variable . The
variable y is random variable.
Regression analysis deals with the relationship between y and x.

The polyfit function provides the values for the least square fit.
Its syntax is p = polyfit(x,y,n)
The function fits a polynomial of degree n to data described by
the vectors x and y.
The result p is the row vector of length n + 1 that contains the
polynomial coefficients in order of descending powers.
18 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Example
Consider the data x = 1,2,3,...,9 and y = 5,6,10,20,28,33,34,36,42.
Find and plot the first -through fourth-degree polynomials for this
data and evaluate J (the sum of the squares of the vertical
differences between the line and the data points) for each polynomial.
Solution
x = [1:9] ;
y=[5,6,10,20,28,33,34,36,42];
xp = [1 : 0.01 :9];
for k = 1 : 4
coeff = polyfit(x,y,k);
yp(k,:) = polyval(coeff,xp);
J(k) = sum((polyval(coeff,x) - y ).2);
end
subplot(2,2,1)
plot(xp,yp(1,:),x,y,o),axis([0 10 0 50])
19 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

sol...

subplot(2,2,2)
plot(xp,yp(2,:),x,y,o),axis([0 10 0 50])
subplot(2,2,3)
plot(xp,yp(3,:),x,y,o),axis([0 10 0 50])
subplot(2,2,4)
plot(xp,yp(4,:),x,y,o),axis([0 10 0 50])
disp(sprintf(J= %4.2f,J))

20 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

The Basic Fitting Interface


MATLAB supports curve fitting through the Basic Fitting interface.
Using this interface, you can quickly perform basic curve fitting tasks
within the same easy-to-use environment. The interface is designed
so that you can:
Fit data using a cubic spline or a polynomial up to degree 10.
Plot multiple fits simultaneously for a given data set .
Examine the numerical results of a fit.
Interpolate or extrapolate a fit.
Annotate the plot with the numerical fit results and the norm of
residuals.
Save the fit and evaluate results to the MATLAB workspace.

21 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Procedures:

1
2
3

Plot some data.


Select Basic Fitting from the Tools menu.
Click the right arrow button once.

The third pane is used for interpolating or extrapolating a fit. It


appears when you click the right arrow button a second time.
At the top of the first pane is the Select data window which contains
the names of all the data sets you display in the Figure window
associated with the Basic Fitting interface. Use this menu to select
the data set to be fit. You can perform multiple fits for the current
data set. Use the Plot Editor to change the name of a data set.

22 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

The remaining items on the first pane are used as follows.


Center and scale X data. If checked, the data is centered at
zero mean and scaled to unit standard deviation. You may need
to center and scale your data to improve the accuracy of the
subsequent numerical computations.
Plot fits. This panel allows you to visually explore one or more
fits to the current data set.
Check to display fits on figure. Select the fits you want to
display for current data set. You can choose as many fits for a
given data set as you want.
Show equations. If checked, the fit equation is displayed on
the plot.
Significant digits. Select the significant digits associated with
the fit coefficient display.
Plot residuals. If checked the residuals are displayed. You can
display the residuals as a bar plot, a scatter plot, a line plot
using either the same figure window as the data or using a
separate figure window.
23 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Show norm of residuals. If checked, the norm of residuals is


displayed. The norm of residuals is a measure of the goodness of
fit, where a smaller value indicates a better fit. The norm is the
square root of the sum of the squares of the residuals.
The second pane of the Basic Fitting Interface is labeled Numerical
Results, This pane enables you to explore the numerical results of a
single fit to the current data set without plotting the fit. It contains
three items.
Fit. Use this menu to select an equation to fit to the current
data set. The fit results are displayed in the box below the
menu. Note that selecting an equation in this menu does not
affect the state of teh Plot fits selection.
Coefficients and norm of residuals. Displays results for the
equation selected in Fit.
Save to workspace. Launches a dialog box that allows you to
save the fit results to workspace variables.
24 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

The third pane of the Basic Fitting interface contains three items.
Find Y = f(X). Use this to interpolate or extrapolate the
current fit. Enter a scalar or a vector of values corresponding to
the independent variable(X). The current fit is evaluated after
you click on the Evaluate button, and the results are displayed
in the associated window. The current fit is displayed in the Fit
window.
Save to workspace. Launches a dialog box that allows you to
save the evaluated results to workspace variables.
Plot evaluated results. If checked, the evaluated results are
displayed on the data plot.

25 / 25

Gemechu Fanta

Introduction to Computing with MATLAB

Vous aimerez peut-être aussi