function [p, K, R2_vector, v_vector]=rate_matrix(beta, x)
%------------------- LineShapeKin RATE_MATRIX -----------------------------
%-- version 3.0 
%
% 3/17/08
% Written by Evgenii Kovrigin, Medical College of Wisconsin
%
% This function builds a rate (K) matrix for the spin exchange between the
% sites corresponding to the equation:  
%      P+L=PL, Kd=k2/k1, where k1=kon*[L], k2=Koff
%
%   NOTES:  this is a version with fixed preset Kd (only koff and scaling factor
%   are variable parameters.

global B_vector Model  X_columns 


  
%--- extract fixed (preset) values
%- use fixed preset Kd
Kd = beta(B_vector.Kd);   

%-- extract variable parameters
koff=beta(B_vector.Koff);




%-- extract experimental parameters:

Ptot = x(X_columns.Ptotal);    
Ltot = Ptot*x(X_columns.LPratio);  






%% Analytical solution for [L] out of 
%
%   Kd = [P][L]/[PL]
%
a=1;
b=-(Ptot+Ltot+Kd);
c=Ptot*Ltot;
sola=-b/(2*a);
solb=sqrt(b^2-(4*a*c))/(2*a);
sol1=[(sola+solb) (sola-solb)];
Peq=max(Ptot-sol1); %-- get positive solution
PLeq=Ptot-Peq;
Leq=Ltot-PLeq;

% Equilibrium mole fractions of species containting P 
p=[Peq/Ptot,PLeq/Ptot];

%-- relationships of true constants
kon=koff/Kd;

% K matrix elements
kab=kon*Leq;
kba=koff;

K=[-kab,kba;kab,-kba];



%-- prepare relaxation rates and frequencies for the individual species
 R2_vector=[ x(X_columns.R2A),  x(X_columns.R2B) ];  
 v_vector=[ x(X_columns.wA),    x(X_columns.wB)   ];  

