How do I create a new function for a least-square file?
1 次查看(过去 30 天)
显示 更早的评论
Hi, in the given files, I am aiming to do what has been done in this example shown below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1644801/image.png)
The solution of the least squares problem is any
that satisfies
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1644911/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1644916/image.png)
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];
[SP,grad]=enzyme(y0,k,dt,T)
%experiment 2
y0=[1;1]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
[SP,grad]=enzyme(y0,k,dt,T)
%experime 3
y0=[1;0]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
[SP,grad]=enzyme(y0,k,dt,T)
%experiment 4
y0=[4;1]
dt=0.01;
T=1;
k=[5;1];
enzyme(y0,k,dt,T)
[SP,grad]=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);
%update the gradient dydk using the expression derived in Task 3
dydk = dydk+dt*(dfdy*dydk+dfdk);
end
%update the state y following the expression from Task 2
y = y+dt*f;
end
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!