Docsity

Numerical Computing: Fitting Data to Exponential Model & Evaluating Integrals with Matlab , Exams of Computer Science

Toggle sidebar
bg1
Numerical Computing S’09 MATH/CSCI 4800-01
Computational portion of Test II
Due Thursday April 16, in class
NOTES
1. This test is to be taken with no collaboration with any other individual. At the top of your answer
package, print the following statement and sign it. “ I certify that I have followed the ground rules of
this test.”
2. The following are essential if full credit is sought.
(a) Matlab scripts, printed outputs and figures must be attached. Scripts should include appropriate
comments, and printouts and figures should be suitably labelled.
(b) Provide adequate explanation and justification.
Problems
1. (20 points) Experimental measurements reveal the following data for population p(t) of a bacterium
as a function of time: t, hours p(t)
1 0.716
2 0.893
3 1.055
4 1.134
5 1.167
6 1.281
7 1.994
8 2.500
9 3.151
10 4.300
11 5.308
12 6.510
You are to fit the data to the exponential model p=c1ec2tin the least squares sense. Fitting is to be
carried out in two different ways: (i) directly, and (ii) by linearization. A direct fit would minimize the
mean squared error between the data (ti, pi) and the exponential model c1ec2t.A linearized fit would
minimize the mean squared error between the the logarithmic data (ti,ln pi) and the logarithmic
model ln c1+c2t. For details see page 210 of the Text.
For each case proceed as follows.
(a) Write down an expression for the least squares error.
(b) Write down the system of equations that results when the error is minimized with respect to
the coefficients. (Note that the equations will be linear in the coefficients ln c1and c2for the
linearized approach and nonlinear in c1and c2for the direct approach.)
(c) Write a Matlab program and find the coefficients. (For the linearized approach you may use any
of the several procedures discussed in class, including the backslash command in Matlab. For the
direct approach use Newton’s method for nonlinear systems.)
Hints.
i. Do the linearized problem first. Use the answer to this problem (i.e., the values of c1, c2you
found) as as the starting guess for the direct problem.
ii. If V is a vector, then the Matlab command sum(V) gives you the sum of the components of
the vector.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Numerical Computing: Fitting Data to Exponential Model & Evaluating Integrals with Matlab and more Exams Computer Science in PDF only on Docsity!

Numerical Computing S’09 MATH/CSCI 4800- Computational portion of Test II Due Thursday April 16, in class

NOTES

  1. This test is to be taken with no collaboration with any other individual. At the top of your answer package, print the following statement and sign it. “ I certify that I have followed the ground rules of this test.”
  2. The following are essential if full credit is sought.

(a) Matlab scripts, printed outputs and figures must be attached. Scripts should include appropriate comments, and printouts and figures should be suitably labelled. (b) Provide adequate explanation and justification.

Problems

  1. (20 points) Experimental measurements reveal the following data for population p(t) of a bacterium as a function of time: t, hours p(t) 1 0. 716 2 0. 893 3 1. 055 4 1. 134 5 1. 167 6 1. 281 7 1. 994 8 2. 500 9 3. 151 10 4. 300 11 5. 308 12 6. 510 You are to fit the data to the exponential model p = c 1 ec^2 t^ in the least squares sense. Fitting is to be carried out in two different ways: (i) directly, and (ii) by linearization. A direct fit would minimize the mean squared error between the data (ti, pi) and the exponential model c 1 ec^2 t. A linearized fit would minimize the mean squared error between the the logarithmic data (ti, ln pi) and the logarithmic model ln c 1 + c 2 t. For details see page 210 of the Text. For each case proceed as follows.

(a) Write down an expression for the least squares error. (b) Write down the system of equations that results when the error is minimized with respect to the coefficients. (Note that the equations will be linear in the coefficients ln c 1 and c 2 for the linearized approach and nonlinear in c 1 and c 2 for the direct approach.) (c) Write a Matlab program and find the coefficients. (For the linearized approach you may use any of the several procedures discussed in class, including the backslash command in Matlab. For the direct approach use Newton’s method for nonlinear systems.) Hints. i. Do the linearized problem first. Use the answer to this problem (i.e., the values of c 1 , c 2 you found) as as the starting guess for the direct problem. ii. If V is a vector, then the Matlab command sum(V) gives you the sum of the components of the vector.

(d) Plot the data and the two fitting functions on the same graph. Comment on the goodness of the fits. Which one captures better the trend of the data?

(a) Denote the data points as (ti, pi). The fitting function is p = c 1 ec^2 t. The least-squares errors for the two cases are:

Direct case: E =

∑^12

i=

[

c 1 ec^2 ti^ − pi

] 2

Linearized case: E =

∑^12

i=

[

d 1 + c 2 ti − ln pi

] 2

where d 1 = ln c 1 in the second expression above.

(b) Error is minimized by setting the first partial derivatives of E with respect to each of the two coefficients to zero, leading to the following results. Linearized case:

∂E ∂d 1

∑^12

i=

[

d 1 + c 2 ti − ln pi

]

∂E

∂c 2

∑^12

i=

[

d 1 + c 2 ti − ln pi

]

ti = 0.

Simplification yields the normal equations

12 d 1 +

i=

ti

c 2 =

∑^12

i=

ln pi,

d 1

i=

ti

i=

t^2 i

c 2 =

∑^12

i=

ti ln pi.

Direct case:

∂E ∂c 1

∑^12

i=

[

c 1 ec^2 ti^ − pi

]

ec^2 ti^ = 0,

∂E

∂c 2

∑^12

i=

[

c 1 ec^2 ti^ − pi

]

c 1 tiec^2 ti^ = 0.

These simplify to the normal equations

∑^12

i=

c 1 e^2 c^2 ti^ −

∑^12

i=

piec^2 ti^ = 0 ,

∑^12

i=

c^21 t 1 e^2 c^2 ti^ −

∑^12

i=

pic 1 tiec^2 ti^ = 0

(c) See Matlab Log. The fitting coefficients are found to be c 1 = 0. 5165 , c 2 = 0. 2028 for the linearized case and c 1 = 0. 4057 , c 2 = 0.2319 for the direct case.

(d) See attached figure. The two fits seem to capture the data trend equally well.

x

K

Plot of K(x)

Figure 2: Plot of K(x) in problem 2.

TEST II (Computational Part)

Matlab Log

Problem 1.

% ScriptFile LS.m

% Computes least-squares fit using linearization

clc

% Specify data points

T=[1:12];

P=[0.716 0.893 1.055 1.134 1.167 1.281 1.994 2.500 3. ...

4.300 5.308 6.510];

% Specify matrices A and b

A11=12;

A12=sum(T);

A21=A12;

A22=sum(T.*T);

b1=sum(log(P));

b2=sum(T.*log(P));

A=[A11 A12; A21 A22];

b=[b1;b2];

% Solve Ak=b

k=A\b;

% Identify coefficients c_1 and c_

c1=exp(k(1)); c2=k(2);

% Plot data and fit

plot(T,P,'ro');

hold on

TT=linspace(1,12);

PP=c1exp(c2TT);

plot(TT,PP);

xlabel('t'); ylabel('p');

title('Least square fits: red (linear), green (nonlinear)');

% Display coefficients of the fitting curve

disp(c1);

disp(c2);

Numerical Output

c(1)= 5.1652e-

c(2)= 2.0278e-

% Script File Newton_main2d.m

% Computes nonlinear least-squares fit

% Requires program Newton2d.m for nonlinear root finding

% Requires function f2.m for Newton2d.m

while k <= m

k=k+1;

fprintf('k= %i \n',k);

[f,Jf]=feval(fun,x);

dx = -Jf\f;

x=x+dx;

disp('x');

disp(x);

disp('f');

disp(f);

if norm(dx)<tol, x; return;end

end

warning(sprintf('root not found within tolerance after %d

iterations\n',k));

function [F,JF]=f2(x)

% Input

% x: column vector (x1; x2)

% Output:

% F: function (F1;F2)

% JF: jacobian of F with respect to x

% Supply needed data

global T

global P

C1=x(1);

C2=x(2);

V1=exp(C2*T);

V2=exp(2C2T);

% Define function F

F1=C1sum(V2)-sum(P.V1);

F2=C1^2sum(T.V2)-C1sum(P.T.*V1);

F=[F1;F2];

% Define jacobian

J11=sum(V2);

J12=2C1sum(T.V2)-sum(P.T.*V1);

% J21=sum(T.*V2);

% J22=2C1sum(T.T.V2)-sum(P.T.T.*V1);

J21=2C1sum(T.V2)-sum(P.T.*V1);

J22=2C1^2sum(T.T.V2)-C1sum(P.T.T.V1);

JF=[J11,J12;J21,J22];

Numerical Output

C(1)= 4.0567e-

C(2)= 2.3195e-

Problem 2

% Script File integral.m

% Specify function and limits of integration

clc

clear all

I=[];

for j=1:

xx(j)=(j-1)/25;

x(j)=1-exp(-5*xx(j));

f=@(t) 1/sqrt(1-x(j).x(j).sin(t));

% Specify tolerance

tol0=0.001;

% Begin adaptive procedure

sum=0; n=1; a(1)=0; b(1)=pi/2; tol(1)=tol0;

app(1)=trap(f,0,pi/2);

while n>

c=(a(n)+b(n))/2;

oldapp=app(n);

app(n)=trap(f,a(n),c);

app(n+1)=trap(f,c,b(n));

if abs(oldapp-(app(n)+app(n+1)))<3*tol(n)

sum=sum+app(n)+app(n+1); % success

n=n-1; % done with interval

else

b(n+1)=b(n);b(n)=c; % set up new intervals

a(n+1)=c;

tol(n)=tol(n)/2; tol(n+1)=tol(n);

n=n+1; % go to end of list,

repeat

end

end

I=[I sum];

end

format short e

plot(x,I,'-o');

xlabel('x'); ylabel('K');

title('Plot of K(x)');