# How do I create a new function for a least-square file?

2 views (last 30 days)
Sergio on 18 Mar 2024
Edited: Torsten on 18 Mar 2024
Hi, in the given files, I am aiming to do what has been done in this example shown below:
The solution of the least squares problem is any that satisfies
for all 𝑥
We have the given relevant terms to include:
• is the residual vector
• if , then solves the linear equation 𝐴𝑥 = 𝑏
• if , then is a least squares approximate solution of the equation
• in most least squares applications, 𝑚 > 𝑛 and 𝐴𝑥 = 𝑏 has no solution
In the attached files, I need to form the matrix of the experiment results, then to identify the residual vector and then create a new function called enzymeLeastSquares.
This function should calculate the residual vector and the corresponding Jacobian. To do this, the function should utilize the enzyme function to obtain the numerical concentrations at time T. Additionally, I will need to refer to the experimental data provided in Table 1 of the homeworkLeastSquares.pdf document, which is also attached.
What I am not sure of, is how do I form this matrix by the given experimental data and how is that used to calculate a set of least square optimization residual vectors?
Thanks
%experiment 1
y0=[2;1]
dt=0.01;
T=1;
k=[5;1];
%experiment 2
y0=[1;1]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
%experime 3
y0=[1;0]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
%experiment 4
y0=[4;1]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
function [f,dfdy,dfdk] = enzymeRhs(y,k)
%ENZYMERHS evaluates the right hand side of the enzyme ODE system.
%
% [F,DFDY,DFDK] = ENZYMERHS(Y,K) evaluates the right hand side for the
% enzyme system with rates K and current concentration Y and returns F,
% the value of the right hand side as well as DFDY and DFDK, the
% gradient of the right hand side function with respect to
% concentrations Y and rates K, respectively.
%
C = 0.1; %constant creation of substrate
f = zeros(2,1);
f(1) = y(2) - k(1)*y(1)/(k(2)+y(1)) + C;
f(2) = -y(2) + k(1)*y(1)/(k(2)+y(1));
if nargout>1
dfdy = [-k(1)/(k(2)+y(1))+k(1)*y(1)/(k(2)+y(1))^2 1;
k(1)/(k(2)+y(1))-k(1)*y(1)/(k(2)+y(1))^2 -1];
dfdk = [-y(1)/(k(2)+y(1)) k(1)*y(1)/(k(2)+y(1))^2;
y(1)/(k(2)+y(1)) -k(1)*y(1)/(k(2)+y(1))^2];
end
end
function [y,dydk] = enzyme(y0,k,dt,T)
%ENZYME
%
% [Y,DYDK] = ENZYME(Y0,K,DT,T) integrates the enzyme system with initial
% concentration Y0 and rates k from time 0 to time T using the explicit
% Euler method with time step DT. The concentration at end time T is
% returned in Y and the sensistivity DYDK with respect to the rates k.
%
y = y0(:);
if nargout == 2
dydk = zeros(2,2);
end
for nn = 1:round(T/dt)
if nargout<2
f = enzymeRhs(y,k);
else
[f,dfdy,dfdk] = enzymeRhs(y,k);
dydk = dydk+dt*(dfdy*dydk+dfdk);
end
%update the state y following the expression from Task 2
y = y+dt*f;
end
end

### Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

R2023b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!