Académique Documents
Professionnel Documents
Culture Documents
Gemechu Fanta
1 / 25
Gemechu Fanta
outline of lecture 5
Function Finding
Interpolation and Extrapolation
Regression
The Basic Fitting Interface
2 / 25
Gemechu Fanta
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)
Gemechu Fanta
(3)
y = be mx
(4)
(5)
Gemechu Fanta
(6)
w = p1 z + p2
(7)
Gemechu Fanta
(8)
w = p1 z + p2
(9)
Gemechu Fanta
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
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
%
% 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
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
12 / 25
Gemechu Fanta
Gemechu Fanta
14 / 25
Gemechu Fanta
Gemechu Fanta
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
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
Regression
Analysis in experiments involving two variables, say, x and y
I
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
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
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
21 / 25
Gemechu Fanta
Procedures:
1
2
3
22 / 25
Gemechu Fanta
Gemechu Fanta
Gemechu Fanta
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