MATLAB: How to find a constant value in an equation

constant-searchingStatistics and Machine Learning Toolbox

Hello, I try to find a constant value in my equation, it has this form :
Y = log(1+a*b*x)/(a*b*x)
I know the value of a, and I'm trying to get b value. Thank you for your advices.

Best Answer

Nolann, I guess you had trouble adapting my demos so I did a full blown demo for you. Try this:
% Uses fitnlm() to fit a non-linear model (a log formula) through noisy data.
% Requires the Statistics and Machine Learning Toolbox, which is where fitnlm() is contained.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create the X coordinates: 30 points from 0.01 to 20, inclusive.
X = linspace(0.01, 20, 100);
% Define function that the X values obey.
a = 10 % Defined to be fixed by the user.
b = 0.1 % Arbitrary sample values I picked.
Y = log(1+a*b*X) ./ (a*b*X); % Get a vector. No noise in this Y yet.
% Add noise to Y.
Y = Y + 0.02 * randn(1, length(Y));
% Now we have noisy training data that we can send to fitnlm().
% Plot the noisy initial data.
plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
tbl = table(X', Y');
% Define the model as Y = log(1+a*b*X) ./ (a*b*X)
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) log(1 + a * b * x(:, 1)) ./ (a * b * x(:, 1));
beta0 = .5; % Guess values to start with. Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = log(1 + a * coefficients(1) * X) ./ (a * coefficients(1) * 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('Non-linear Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 25;
message = sprintf('Coefficients for Y = log(a * b * X) ./ (a * b * X) :\n a = %8.5f\n b = %8.5f',...
a, coefficients);
text(4, 0.7, message, 'FontSize', 23, 'Color', 'r', 'FontWeight', 'bold', 'Interpreter', 'none');
% 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')
Replace my sample x and y with your own. Attach your x and y data if you can't figure out how to do that.