Parameter Estimation of Kinetic model - matlab

I have a chemical kinetic model (2-Tissue compartment Model) with Constrained K3(where K3 is rate constant)
I have modelled plasma function along with Chemical kinetic model in order to plot the output characteristics
I would like to estimate the Rate Constant k3 from the Code below
function c_t = output_function_constrainedK3(t, a1, a2, a3,b1,b2,b3,td, tmax,k1,k2,k3)
DV_free= k1/(k2+k3);
K3 = k3*((k1/k2)/DV_free);
K_1 = (k1*k2)/(k2+K3);
K_2 = (k1*K3)/(k2+K3);
c_t = zeros(size(t));
ind = (t > td) & (t < tmax);
c_t(ind)= conv(((t(ind) - td) ./ (tmax - td) * (a1 + a2 + a3)),(K_1*exp(-(k2+K3)*t(ind)+K_2)),'same');
ind = (t >= tmax);
c_t(ind)= conv((a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax))) + a3 * exp(-b3 * (t(ind) - tmax)),(K_1*exp(-(k2+K3)*t(ind)+K_2)),'same');
plot(t,c_t);
figure
%plot(t,c_tnp);
axis([0 50 -2000 80000]);
xlabel time ;
ylabel concentration ;
end
The initial estimates for all the parameters is enlisted below
t=0:0.1:60;
td =0.3;
tmax=0.8;
a1=2501;
a2=18500;
a3=65000;
b1=0.5;
b2=0.7;
b3=0.3;
k1=0.014;
k2=0.051;
k3=0.07;
Kindly suggest me a method to estimate K3 parameter from nonlinear kinetic model code described above
In the above function the parameters values for a1, a2, a3,b1, b2 ,b3, td, tmax, k1, k2 will remain constant.
I would like to know how K3 value changes with change in time t value, for this i would like to initially estimate K3 at t interval t=0:0.1:60..
Any help greatly appreciated

Related

Is the MATLAB command lsim good for linearized state space models?

A state space model is on the form:
dx = Ax + Bu
y = Cx + Du
Nonlinear state space models which is linearized is on this form:
dΔx = AΔx + BΔu
Δy = CΔx + DΔu
Where:
Δx = [x1 - x10; x2 - x20; x3 - x30; .... ; xn - xn0]
Δu = [u1 - u10; u2 - u20; u3 - u30; .... ; um - um0]
The x10, x20, x30, xn0, u10, u20, u30, um0 is constants/initial values for the linearization.
So! The question is about the MATLAB command "lsim":
lsim (sys, u, t, x0)
In this case, sys is the A, B, C, D matrices from the lineraized state space model. u is the insignal vector, t is the time vector. But x0....can i say that x0 is x10, x20, x30, .... ,xn0?
Can I also say that u = u - u0 , and u0 is , u10 u10, u20, u30,... , um0 ?
Example:
u = linspace(5, 5, 100); % insignal 5
t = linspace(0, 100, 100); % 100 seconds
u0 = [0.2; -1.2; -3];
u = u - u0; %
x0 = [-2; 2; -1]
lsim (sys, u, t, x0)
This will results:
Δx = [x1 - 2; x2 + 2; x3 - 1]
Δu = [u1 + 0.2; u2 - 1.2; u3 - 3]
Am I right?
There is a readily available Simulink S-Function dsfunc.m which is the simulation of a State Space model, and which can be easily adapted for included linearized State Space matrices, changing at every time step.
Just build a simulink file dsfunc.slx and run it with the sim command sim('dsfunc') command.
The good part is the S-Function deals properly with the state, and you just adapt the State Space model each time, keeping the state from one iteration to another.
Also you can do it equally easily with a single for loop. Linearizing constant are included as shown.
function [t,u,x,y]=example2
% Start
dt=0.001;
N=1000;
n=2;
r=1;
t=(0:N-1)'*dt;
u=rand(N,r);
% First
[A,B,C,D,x0,y0]=ABCD(t(1),zeros(n,1),u(1,:)');
x(1,:)=x0';
y(1,:)=(C*x(1,:)'+D*u(1,:)'+y0)';
% Cycle
for i=2:N
[A,B,C,D,x0,y0]=ABCD(t(i),x(i-1,:)',u(i-1,:)');
x(i,:)=(A*x(i-1,:)'+B*u(i-1,:)'+x0)';
y(i,:)=(C*x(i,:)'+D*u(i,:)'+y0)';
end
plot(t,[u x y]);legend({'u' 'x1' 'x2' 'y'});
function [A,B,C,D,x0,y0]=ABCD(t,x,u)
A=[0.1 -0.9;0 0.1];
B=0.5*ones(2,1);
C=[1 1];
x0=[0];y0=[0];

My matlab neural network backpropagation algorithm seems buggy

Here is my code. I think it is wrong because the difference between this computed gradient and my numerical estimate is too significant. It doesn't seem to be due to wrongly inverting matrices, etc.
For context, Y is the output layer, X is the input layer, and there is only 1 hidden layer. Theta1 is the weights for the first input layer and Theta2 is the weights for the hidden layer.
for t = 1:m
% do fw prop again...
a1 = [1 X(i,:)];
a2 = [1 sigmoid(a1 * Theta1')];
a3 = sigmoid(a2 * Theta2');
delta_3 = a3' - Y(:, t);
delta_2 = Theta2' * delta_3 .* a2' .* (1 - a2)';
delta_2 = delta_2(2:end,:);
Theta1_grad = Theta1_grad + delta_2 * [1 X(i, :)];
Theta2_grad = Theta2_grad + delta_3 * [1 sigmoid([1 X(i,:)] * Theta1')];
end
grad = [Theta1_grad(:) ; Theta2_grad(:)];

How to create a table in Matlab?

Following is the graph that I used to calculate the PMV at points: A, B, C, D, E
%This is the code I used to calculate the PMV at points: A, B, C, D, E.
%Where:
%ta=tr=interior temperature setting (22°C, 23°C, 27°C) (variable);
%va=0,2 m/s (invariable);
%RH=50% (invariable);
%W=0 (invariable);
%Met= energy metabolism (1,2 met or 1,4 met) (variable);
%Iclo= static clothing insulation (0,5 clo or 1,0 clo) (variable).
ta=22.0;
tr=22.0;
va=0.2; %air speed
RH=50; %relative humidity
W=0;%mechanical work
Met=1.2;%energy metabolism in met (1 met=58.2 W/m2)
Iclo=0.5;%static clothing insulation
%preparation of variables
PHI=RH/100;%hygrometric dimensionless degree
Icl=Iclo*.155;%Conversion from clo to m2K/W
M=Met*58.15;%conversion of metabolism in unit of measurement of SI
Iclr=Icldyn_7730(va, Icl, M); %calculation of dynamic clothing insulation
vw=0.0052*(M-58);
vr=va+vw;
PMV_Fanger=PMV_evaluator( M,W,ta,tr,vr,PHI,Iclr );
Observation: the functions I called are the following: "Icldyn_7730"
function [ Icldyn ] = Icldyn_7730(va, Iclst, M)
%calculation of dynamic clothing insulation
%Input data
% va, air speed, m/s
% Iclst, static clothing insulation
% M, metabolism in W/m2
vw=0.0052*(M-58);
if vw>0.7
vw=0.7;
end
vr=va+vw;
%Static cloting insulation conversion m2K/W to clo
Iclo = Iclst/0.155;
%Clothing area factor
if Iclst <=0.078
fcl= 1.00 + 1.290 * Iclst;
else
fcl= 1.05 + 0.645 * Iclst;
end
%Static boundary layer thermal insulation in quiet air in m2K/W
Iast = 0.111;
%Total static insulation
Itotst= Iclst + Iast / fcl;
%Clothing insulation correction for wind (vr) and and walking (vw)
vraux= vr;
if vraux > 3.5
vraux=3.5;
end
if vraux < 0.15
vraux=0.15;
end
vwaux=vw;
if vwaux>0.7
vwaux=0.7;
end
CorIt=exp(-0.281*(vraux-0.15)+0.044*(vraux-0.15)^2-0.492*vwaux+0.176*vwaux^2);
if CorIt>1
CorIt=1;
end
CorIa=exp(-0.533*(vraux-0.15)+0.069*(vraux-0.15)^2-0.462*vwaux+0.201*vwaux^2);
if CorIa>1
CorIa=1;
end
Itr = Itotst * CorIt;
Iar = CorIa * Iast;
if Iclo<=0.6
Itr= ((0.6-Iclo) * Iar + Iclo * Itr) / 0.6;
end
Itdyn = Itr;
Iadyn = Iar;
Icldyn = Itdyn - Iadyn / fcl;
end
and "PMV_evaluator"
function [ PMV ] = PMV_evaluator( M,W,ta,tr,vr,PHI,Icl )
%Function for the calculation of the PMV index
% Input data
% M, metabolic rate in W/m2
% W, mechanical work in W/m2
% ta, air temperature in °C
% tr, mean radiant temperature in °C
% vr, rwlative air velocity in m/s
% PHI, hygrometric ratio dimensionless
% Icl in m2K/W (dynamic clothing insulation )
if (ta >=0)
ps = exp (16.6536-4030.183 / (235 + ta ));
else
ps = 0.6105* exp (21.875*ta / (265.5 + ta ));
end;
TAA = ta+273.0;
TRA = tr+273.0;
TCLA = TAA + (35.5-ta) / (3.5*Icl+0.1);
hcf = 12.1 * sqrt(vr);
%Clothing area factor
if Icl <=0.078
fcl= 1.00 + 1.290 * Icl;
else
fcl= 1.05 + 0.645 * Icl;
end
% Start of the loop for the evaluation of clothing surface temperature}
P1 = Icl * fcl;
P2 = P1 * 3.96;
P3 = P1 * 100;
P4 = P1 * TAA;
P5 = 308.7 - 0.028 * (M-W) + P2 * (TRA/100)^4;
XN = TCLA/100;
XF = XN;
EPS = 0.00015;
CONV = 100;
N=1;
while (CONV>EPS)
XF = (XF+XN)/2;
hcn = 2.38 * ((abs(100*XF - TAA))).^0.25;
if (hcf<=hcn)
hc = hcn;
else
hc = hcf;
end
XN = (P5+P4*hc-P2*XF^4)/(100+P3*hc);
CONV=abs(XF-XN);
end
tcl = 100*XN-273;
% End of the loop for the evaluation of clothing surface temperature}
%Skin diffusion heat loss
HL1=3.05*0.001*(5733-6.99*(M-W)-1000*PHI*ps);
%Sweat heat loss
if (M-W)>58.15
HL2= 0.42 * ((M-W)-58.15);
else
HL2=0;
end
%Respiration latent heat loss
HL3= 1.7*0.00001 * M * (5867-1000*PHI*ps);
%Respiration dry heat loss
HL4= 0.0014 * M * (34-ta);
%Radiative heat loss
HL5= 3.96 * fcl * ((0.01*tcl+2.73)^4-(0.01*tr+2.73)^4);
%Convective heat loss
HL6= fcl * hc * (tcl-ta);
%Thermal sensation transformation coefficient}
TS= 0.303 * exp(-0.036*M) + 0.028;
PMV= TS * (M-W-HL1-HL2-HL3-HL4-HL5-HL6);
end
How can I create a table like the following with MATLAB?
The data in the table are the values of PMV. They have been obtained from the individual calculations of MATLAB.
Consider using the table variable introduced in the later versions of matlab, this variable allows for disparate data sources. The full matlab help has an example where the a set of categorical row names occupies the first column while a set of headers occupies the top.
The writetable commmand in matlab will also write a table variable (rows/columns/headers etc) to an excel spreadsheet.
Verbosing zhqiat's answer, your table could be generated by the following code:
data = [-1.5924 -0.2152 -1.1426 0.0421; -1.5924 -0.2152 -1.1426 0.0421; -1.2319 0.0313 -0.8241 0.2595; 0.2329 1.0332 0.4686 1.1427; 0.2329 1.0332 0.4686 1.1427];
row_names = {'A', 'B', 'C', 'D', 'E'};
var_names = {'met1d2_clo0d5', 'met1d2_clo1d0', 'met1d4_clo0d5', 'met1d4_clo1d0'};
var_description = {'M = 1.2 met - 0.5 clo', 'M = 1.2 met - 1. clo', 'M = 1.4 met - 0.5 clo', 'M = 1.4 met - 1.0 clo' };
testtable = array2table(data, 'VariableNames', var_names, 'RowNames', row_names);
testtable.Properties.VariableDescriptions = var_description;
This would result in something like this:
Screenshot of Matlab-Table
We all might got your question wrong. So please try to refine it, if the right answers are missing.

Matlab:Gaussian Model with Variable Mean and Standard Deviation

I would like to add Gaussian noise Model with variable Standard Deviation (Sigma) and Variable Mean (Mu) to my Output Curve which is shown in the figure below
http://i.imgur.com/hABfsiC.jpg
Following function generates the Output curve expressed in the figure above
function c_t = output_function_constrainedK2(t, a1, a2, a3,b1,b2,b3,td, tmax,k1,k2,k3)
K_1 = (k1*k2)/(k2+k3);
K_2 = (k1*k3)/(k2+k3);
DV_free= k1/(k2+k3);
c_t = zeros(size(t));
ind = (t > td) & (t < tmax);
c_t(ind)= conv(((t(ind) - td) ./ (tmax - td) * (a1 + a2 + a3)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
ind = (t >= tmax);
c_t(ind)= conv((a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax))) + a3 * exp(-b3 * (t(ind) - tmax)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
plot(t,c_t);
axis([0 50 0 1400]);
xlabel('Time[mins]');
ylabel('concentration [Mbq]');
title('Model :Constrained K2');
end
The output values for the above function are
output_function_constrainedK2(0:0.1:50,2501,18500,65000,0.5,0.7,0.3,...
0.28,0.9,0.014,0.051,0.07)
Now i would like to add the Gaussian probability distribution function with variable Standard deviation Sigma and mean to the above function, could any one please help me through this , I am an absolute beginner to matlab
between these lines
c_t(ind)= conv((a1 * exp(-b1 * (t(ind) - tmax))+ a2 * exp(-b2 * (t(ind) - tmax))) + a3 * exp(-b3 * (t(ind) - tmax)),(K_1*exp(-(k2+k3)*t(ind)+K_2)),'same');
plot(t,c_t);
add the following
c_t = c_t + normrnd(mu,sigma,length(c_t),1)
refer to the normrnd documentation for more information about the normrnd function. Or type
help normrnd
in the matlab console.
edit to correct this answer according to your last comment:
leave c_t as before, and you will have to create a new vector:
c_t_noise = c_t + normrnd(mu,sigma,1,length(c_t))
also changed the order of parameteres in normrnd to fit your dimension. to plot the two curves, just use the extended plot, like this:
plot(t,c_t, t,c_t_noise)
or do a hold on like this
plot(t,c_t)
hold on %tells matlab to put plots in the same figure
plot(t,c_t_noise)
hold off %this line if you pretend to make some other plot, desactivate hold on
more info about "hold" function in matlab console
help hold

Neural Networks: Sigmoid Activation Function for continuous output variable

Okay, so I am in the middle of Andrew Ng's machine learning course on coursera and would like to adapt the neural network which was completed as part of assignment 4.
In particular, the neural network which I had completed correctly as part of the assignment was as follows:
Sigmoid activation function: g(z) = 1/(1+e^(-z))
10 output units, each which could take 0 or 1
1 hidden layer
Back-propagation method used to minimize cost function
Cost function:
where L=number of layers, s_l = number of units in layer l, m = number of training examples, K = number of output units
Now I want to adjust the exercise so that there is one continuous output unit that takes any value between [0,1] and I am trying to work out what needs to change, so far I have
Replaced the data with my own, i.e.,such that the output is continuous variable between 0 and 1
Updated references to the number of output units
Updated the cost function in the back-propagation algorithm to:
where a_3 is the value of the output unit determined from forward propagation.
I am certain that something else must change as the gradient checking method shows the gradient determined by back-propagation and that by the numerical approximation no longer match up. I did not change the sigmoid gradient; it is left at f(z)*(1-f(z)) where f(z) is the sigmoid function 1/(1+e^(-z))) nor did I update the numerical approximation of the derivative formula; simply (J(theta+e) - J(theta-e))/(2e).
Can anyone advise of what other steps would be required?
Coded in Matlab as follows:
% FORWARD PROPAGATION
% input layer
a1 = [ones(m,1),X];
% hidden layer
z2 = a1*Theta1';
a2 = sigmoid(z2);
a2 = [ones(m,1),a2];
% output layer
z3 = a2*Theta2';
a3 = sigmoid(z3);
% BACKWARD PROPAGATION
delta3 = a3 - y;
delta2 = delta3*Theta2(:,2:end).*sigmoidGradient(z2);
Theta1_grad = (delta2'*a1)/m;
Theta2_grad = (delta3'*a2)/m;
% COST FUNCTION
J = 1/(2 * m) * sum( (a3-y).^2 );
% Implement regularization with the cost function and gradients.
Theta1_grad(:,2:end) = Theta1_grad(:,2:end) + Theta1(:,2:end)*lambda/m;
Theta2_grad(:,2:end) = Theta2_grad(:,2:end) + Theta2(:,2:end)*lambda/m;
J = J + lambda/(2*m)*( sum(sum(Theta1(:,2:end).^2)) + sum(sum(Theta2(:,2:end).^2)));
I have since realised that this question is similar to that asked by #Mikhail Erofeev on StackOverflow, however in this case I wish the continuous variable to be between 0 and 1 and therefore use a sigmoid function.
First, your cost function should be:
J = 1/m * sum( (a3-y).^2 );
I think your Theta2_grad = (delta3'*a2)/m;is expected to match the numerical approximation after changed to delta3 = 1/2 * (a3 - y);).
Check this slide for more details.
EDIT:
In case there is some minor discrepancy between our codes, I pasted my code below for your reference. The code has already been compared with numerical approximation function checkNNGradients(lambda);, the Relative Difference is less than 1e-4 (not meets the 1e-11 requirement by Dr.Andrew Ng though)
function [J grad] = nnCostFunctionRegression(nn_params, ...
input_layer_size, ...
hidden_layer_size, ...
num_labels, ...
X, y, lambda)
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
hidden_layer_size, (input_layer_size + 1));
Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
num_labels, (hidden_layer_size + 1));
m = size(X, 1);
J = 0;
Theta1_grad = zeros(size(Theta1));
Theta2_grad = zeros(size(Theta2));
X = [ones(m, 1) X];
z1 = sigmoid(X * Theta1');
zs = z1;
z1 = [ones(m, 1) z1];
z2 = z1 * Theta2';
ht = sigmoid(z2);
y_recode = zeros(length(y),num_labels);
for i=1:length(y)
y_recode(i,y(i))=1;
end
y = y_recode;
regularization=lambda/2/m*(sum(sum(Theta1(:,2:end).^2))+sum(sum(Theta2(:,2:end).^2)));
J=1/(m)*sum(sum((ht - y).^2))+regularization;
delta_3 = 1/2*(ht - y);
delta_2 = delta_3 * Theta2(:,2:end) .* sigmoidGradient(X * Theta1');
delta_cap2 = delta_3' * z1;
delta_cap1 = delta_2' * X;
Theta1_grad = ((1/m) * delta_cap1)+ ((lambda/m) * (Theta1));
Theta2_grad = ((1/m) * delta_cap2)+ ((lambda/m) * (Theta2));
Theta1_grad(:,1) = Theta1_grad(:,1)-((lambda/m) * (Theta1(:,1)));
Theta2_grad(:,1) = Theta2_grad(:,1)-((lambda/m) * (Theta2(:,1)));
grad = [Theta1_grad(:) ; Theta2_grad(:)];
end
If you want to have continuous output try not to use sigmoid activation when computing target value.
a1 = [ones(m, 1) X];
a2 = sigmoid(X * Theta1');
a2 = [ones(m, 1) z1];
a3 = z1 * Theta2';
ht = a3;
Normalize input before using it in nnCostFunction. Everything else remains same.