Clear Filters
Clear Filters

hello everyone, i am a noob using matlab. please help me i have an equation and 2 unknown variable

8 views (last 30 days)
Somebody please help me. I'm facing this problem. I have an equation with 2 unknown variables. The equation is μ = (μmax*Cs)/(Ks+Cs )
mu = [0.011;0.059;0.053;0.149;0.125]
Cs = [430;428;424;417;409.]
μmax and Ks are the unknown variables. How do I solve the problem using MATLAB? Thanks in advance.
0000 Screenshot.png

Answers (2)

Chuguang Pan
Chuguang Pan on 31 Dec 2019
The equation μ=(μmax*Cs)/(Ks+Cs ) can be converted to μmax*Cs-Ks*μ=μ*Cs.
μ=[0.011;0.059;0.053;0.149;0.125];
Cs=[430;428;424;417;409];
Using least square fitting, you can get the overdetermined equation AX=b.
A=[Cs,-μ];X=[μmax;Ks];b=μ.*Cs;
Then you can get normal equation A'AX=A'b;
Solve the X: X=(A'*A)\(A'*b);
miu=[0.011;0.059;0.053;0.149;0.125];
Cs=[430;428;424;417;409];
A=[Cs,-miu];
b=miu.*Cs;
X=(A'*A)\(A'*b);

Image Analyst
Image Analyst on 31 Dec 2019
Your equation is the "Fluorescence Recovery Curve -- Michaelis-Menten function" or "the Rate equation".
So you can use fitnlm() to fit a non-linear model to it. I've adapted my demo to use your data. The demo is attached and your code is in test6.m. Simply copy the code below, paste into MATLAB and run it to observe the results. You can see the values in my screenshot below.
Actually it's done there twice, first with your original equation as you gave it, and secondly with a vertical offset (to see if we can get a better fit).
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Read in training data from a csv file.
mu = [0.011;0.059;0.053;0.149;0.125]
Cs = [430;428;424;417;409.]
% plot(Cs, mu, 'b.-', 'MarkerSize', 30, 'LineWidth', 2);
% grid on;
% xlabel('Cs', 'FontSize', 20);
% ylabel('mu', 'FontSize', 20);
X = Cs;
Y = mu
% Plot the training data -- what we're going to fit
hFig2 = figure;
plot(X, Y, 'b*-', 'MarkerSize', 20, 'LineWidth', 2);
grid on;
xlabel('X (Time)', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Data to fit exponential to', 'FontSize', fontSize);
drawnow;
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
tableXY = table(X, Y);
% Fit a vertically shifted version of the the Fluorescence Recovery Curve (Michaelis-Menten function).
% One common non-linear equation is the Michaelis-Menten function,
% which is used to describe the reaction rate as a function of substrate concentration.
% The Michaelis-Menten function is characterized by a steep rise that gradually flattens into a plateau.
% Initially, small increases in substrate concentration cause a substantial increase in reaction rate,
% but eventually, additional increases in concentration cause progressively smaller increases in reaction rate.
% Eventually reaction rate plateaus and does not respond to increases in concentration.
% The Michaelis-Menten function has this form:
% rate = Vmax*C / (K + C)
% where r is the growth rate (the dependent variable),
% C is the concentration (the independent variable),
% Vm is the maximum rate in the system, and
% K is the concentration at which the reaction rate is Vm/2.
% These last two parameters (Vm and K) are the Michaelis-Menten parameters to be fit.
% Rewritten in X and Y, you get:
% Y = b1 * X ./ (b2 + X)
% Ref: http://strata.uga.edu/8370/lecturenotes/nonlinearRegression.html
% https://www.graphpad.com/guides/prism/7/curve-fitting/index.htm?reg_classic_hyperbola.htm
%-----------------------------------------------------------------------------------------------------------------------------------
% First we'll try that exact equation. After that, we'll try it again with an offset added to it to see if we can improve it.
modelFunction = @(b, tbl) b(1) * tbl(:, 1) ./ (b(2) + tbl(:, 1));
beta0 = [3, 1]; % Guess values to start with. Just make your best guess.
% Once you get close and see what the coefficients are, you can set beta0 to those values and
% Make another run to get better coefficients.
% Now the next line is where the actual model computation is done.
model1 = fitnlm(tableXY, modelFunction, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
coefficients1 = model1.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = coefficients1(1) * X ./ (coefficients1(2) + X);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Fitting Noisy Data to the Rate Equation', 'FontSize', fontSize);
drawnow;
% Compute residuals
residuals1 = sum(abs(Y - yFitted))
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
xticks(0:ceil(max(X))); % Put up tick mark every 1
xl = xlim; % Get the limits of the axes.
yl = ylim;
% Put the equation up on the figure.
xt = xl(1) + 0.11 * (xl(2) - xl(1));
yt = yl(1) + 0.50 * (yl(2) - yl(1));
message1 = sprintf('Model Equation 1 (has no offset): Y = %.3f * X / (%.3f + X)\nRate constant 1 = %.3f\nResiduals 1 Sum = %.1f', coefficients1(1), coefficients1(2), coefficients1(2), residuals1);
text(xt, yt, message1, 'Color', 'r', 'FontSize', fontSize);
%-----------------------------------------------------------------------------------------------------------------------------------
% Now let's try adding an offset to the equation to see if we can improve it (lower the residuals).
% It will work best for us if we add an offset to that, or
% Y = b1 * X ./ (b2 + X) + b3
modelFunction = @(b, tbl) b(1) * tbl(:, 1) ./ (b(2) + tbl(:, 1)) + b(3);
beta0 = [2.6, 1, 0.5]; % Guess values to start with. Just make your best guess.
% Once you get close and see what the coefficients are, you can set beta0 to those values and
% Make another run to get better coefficients.
% Now the next line is where the actual model computation is done.
model2 = fitnlm(tableXY, modelFunction, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
coefficients2 = model2.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = coefficients2(1) * X ./ (coefficients2(2) + X) + coefficients2(3);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
darkGreen = [0, 0.5, 0];
plot(X, yFitted, '-', 'Color', darkGreen, 'LineWidth', 2);
grid on;
drawnow;
% Compute residuals
residuals2 = sum(abs(Y - yFitted))
% Display the equation for model 2.
message1 = sprintf('Model Equation 2 (has offset): Y = %.3f + %.3f * X / (%.3f + X)\nRate constant 2 = %.3f\nResiduals 2 Sum = %.1f', coefficients2(3), coefficients2(1), coefficients2(2), coefficients2(2), residuals2);
yt = yl(1) + 0.250 * (yl(2) - yl(1));
text(xt, yt, message1, 'Color', darkGreen, 'FontSize', fontSize);
% Put up a legend.
legend('Noisy data', 'Model 1', 'Model 2', 'Location', 'east');
% Enlarge the tick labels.
ax = gca;
ax.FontSize = fontSize;
  5 Comments
rona larasati
rona larasati on 2 Jan 2020
I just copy your code and didn't change the code at all to see how you solve it, and when I tried to run it didn't work :(
Image Analyst
Image Analyst on 2 Jan 2020
Edited: Image Analyst on 2 Jan 2020
I ran this code and it worked fine. You forgot to include the version of MATLAB when you posted. What version is it? The table function was introduced several years ago but maybe you have an antique version. What does this say:
>> which -all table
You should see
C:\Program Files\MATLAB\R2019b\toolbox\matlab\datatypes\@table\table.m % table constructor
C:\Program Files\MATLAB\R2019b\toolbox\matlab\bigdata\@tall\table.m % tall method
C:\Program Files\MATLAB\R2019b\toolbox\parallel\parallel\@codistributed\table.m % codistributed method
C:\Program Files\MATLAB\R2019b\toolbox\parallel\parallel\@distributed\table.m % distributed method
What do you see when you type ver? What toolboxes?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!