0% found this document useful (0 votes)
76 views

MP Matlab Codes

The document provides Matlab code to estimate parameters alpha and beta in the concentration relationship A = alpha*B*exp(beta*B) using the Gauss-Newton method. It initializes alpha and beta, calculates residuals and the Jacobian, inverts the Jacobian to estimate updates to the parameters, and iterates to minimize the sum of squared residuals. The code converges in 3 iterations and estimates alpha as 9.7076 and beta as -2.4994.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

MP Matlab Codes

The document provides Matlab code to estimate parameters alpha and beta in the concentration relationship A = alpha*B*exp(beta*B) using the Gauss-Newton method. It initializes alpha and beta, calculates residuals and the Jacobian, inverts the Jacobian to estimate updates to the parameters, and iterates to minimize the sum of squared residuals. The code converges in 3 iterations and estimates alpha as 9.7076 and beta as -2.4994.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

QUESTION 3A:

SOLUTION:

Matlab Code:

clc
clear all

load water_in_channel.txt
time_day1=water_in_channel(12:17,2);
height_day1=water_in_channel(12:17,4);

t=time_day1;
h=height_day1;
n=length(t);
H=0;
T=0630;
fprintf('i z H\n')
for i=1:n
z=1;
for j=1:n
if j~=i
z=z*(T-t(j))/(t(i)-t(j));
end
end
H=H+h(i)*z;
fprintf('%d %f %f\n',i,z,H)
end
fprintf('\n Height of water on DAY 1 at 0630H = %f\n',H)

Output:

i z H
1 0.001473 0.022834
2 -0.128149 -2.283846
3 0.866597 58.464594
4 0.271654 98.832412
5 -0.017927 93.866770
6 0.006351 96.159531

Height of water on DAY 1 at 0630H = 96.159531


QUESTION 4A
SOLUTION:

Matlab Code:

clear all
clc

syms a b x a0 A

%%Given
display('GIVEN : A = alpha*B*exp(beta*B)');
A = [0.75; 1.25; 1.45; 1.25; 0.85; 0.55; 0.35; 0.28; 0.18];
B = [0.1; 0.2; 0.4; 0.6; 0.9; 1.3; 1.5; 1.7; 1.8];
ConcData = table(A,B)

%%Solution using Matrix notation


fprintf('SOLUTION:\n\n')

x0 =[1;1;1;1;1;1;1;1;1];
M =[x0,B];
Y =[reallog(0.75/0.1); reallog(1.25/0.2); reallog(1.45/0.4);
reallog(1.25/0.6); reallog(0.85/0.9); reallog(0.55/1.3);reallog(0.35/1.5);
reallog(0.28/1.7); reallog(0.18/1.8)];
n = M'*Y;
d =M'* M;
X = inv(d) * n

alpha = exp(X(1,1)) % alpha


beta = X(2,1) % beta

hold all
plot(B,A,'r*')
%plot the measured population
fplot(@(B) alpha*B*exp(beta*B),[0.1,1.8],'b--')
%plot the approximation
title('Concentration of Polymer Configuration A in terms of Polymer B')
%set axis lables, title and legend
xlabel('B')
ylabel('ln(A/B)')

B = 1; %Setting the value of B=1


A = alpha*B*exp(beta*B); %Solve for A when B=1

%%Answer
fprintf('The values of a and b are %0.4f/mmolar and %0.4f/mmolar,
respectivery.\n',alpha,beta);
fprintf('The concentration of A is %0.4f mmolar when the concentration of B
is 1 mmolar.\n',A);
Output:

GIVEN : A = alpha*B*exp(beta*B)

ConcData =

A B
____ ___

0.75 0.1
1.25 0.2
1.45 0.4
1.25 0.6
0.85 0.9
0.55 1.3
0.35 1.5
0.28 1.7
0.18 1.8

SOLUTION:

X =

2.268178502736422
-2.473308765704634

alpha =

9.661785859642892

beta =

-2.473308765704634

The values of a and b are 9.6618/mmolar and -2.4733/mmolar, respectivery.


The concentration of A is 0.8145 mmolar when the concentration of B is 1
mmolar.
QUESTION 4B:
SOLUTION:

Matlab Code:

function [ unknowns,steps,r ] = GaussNewtonConcentration( )


%GaussNewtonConcentration - Uses the Gauss-Newton Method to find the
%the values of alpha and beta from the provided concentration data
%(in mmolar) of A and B.

format long
tol = 0.001; %set a value for the accuracy
maxstep = 1000; %set maximum number of steps to run for

A = [0.75; 1.25; 1.45; 1.25; 0.85; 0.55; 0.35; 0.28; 0.18];


B = [0.1; 0.2; 0.4; 0.6; 0.9; 1.3; 1.5; 1.7; 1.8];
a = [5;-2.5]; %set initial guess for a0 and b
m=length(B); %determine number of functions
n=length(a); %determine number of unkowns
aold = a;
fprintf('Iter alpha beta Sum of
Squars of r \n')
for k=1:maxstep %iterate through process
S = 0;
for i=1:m
for j=1:n
J(i,j) = df(B(i),a(1,1),a(2,1),j); %calculate Jacobian
JT(j,i) = J(i,j); %and its trnaspose
end
end
Jz = -JT*J; %multiply Jacobian and
%negative transpose
for i=1:m
r(i,1) = A(i) - (a(1,1)*B(i)*exp(a(2,1)*B(i))); %calculate r
S = S + r(i,1)^2; %calculate the sum of the squares of the
residuals
end
S;
g = Jz\JT; %mulitply Jz inverse by J transpose
a = aold-g*r; %calculate new approximation
unknowns = a; %set w equal to most recent approximations of
the unkowns
fprintf('%d %0.10f %0.10f
%0.10f\n',k,a(1,1),a(2,1),S);

abs(a(1,1)-aold(1,1)); %calculate error


if (abs(a(1,1)-aold(1,1)) <= tol); %if less than tolerance break
break
end
aold = a; %set aold to a
end
steps = k;
alpha = unknowns(1,1);
beta = unknowns(2,1);
hold all
plot(B,A,'r*') %plot the measured population
fplot(@(B) alpha*B*exp(beta*B),[0.1,1.8])
%plot the approximation
title('Concentration of Polymer Configuration A in terms of Polymer B')
%set axis lables, title and legend
xlabel('Concentration of Polymer B')
ylabel('Concentration of Polymer A')
legend('Given Concentration Data','Gauss-Newton Approximation')
end

function value = df(B,a1,a2,index) %calculate partial derivatives


switch index
case 1
value = B*exp(a2*B);
case 2
value = B*a1*exp(B);
end
end
Output:

Iter alpha beta Sum of Squars of


r
1 9.7095054151 -2.4997013689 1.6830192014
2 9.7082293062 -2.4995488089 0.0191530830
3 9.7075773977 -2.4993968537 0.0191626537

ans =

9.707577397721552
-2.499396853676382

You might also like