Inverse sigmoid fit!

3 次查看(过去 30 天)
Panos Ale
Panos Ale 2017-12-20
评论: Sam Chak 2025-4-9
I found this code in order to fit a sigmoid curve to limited data:
% Define the data to be fit
x=(0:1:10)'; % Explanatory variable
y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect)
y = y + 20*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x,y,f,[1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure(1)
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')
My question is can I input somewhere the finverse funtion to make this usable for an inverse sigmoid fit?

回答(1 个)

Parag
Parag 2025-4-9
The code provided currently fits a quadratic curve using MATLAB’s nlinfit function, which is not a sigmoid function. If the intention is to fit a sigmoid curve and also retrieve its inverse (to estimate input values from the output), the approach can be adapted accordingly.
A sigmoid function typically has the form:
y = A / (1 + exp(-k*(x - x0)))
Where:
  • A is the maximum value (upper asymptote),
  • k determines the steepness of the curve,
  • x0 is the midpoint (inflection point).
This function can be fitted using “nlinfit, and once the parameters are estimated, the inverse can be analytically defined and used as needed. It is important to ensure that the output values passed into the inverse function lie within a valid range to avoid mathematical errors.
Please refer the following MATLAB implementation for the same:
% Sample data (sigmoid shape)
x = linspace(0, 10, 50)';
y = 10 ./ (1 + exp(-1.5*(x - 5))) + 0.5*randn(size(x)); % Sigmoid with noise
% Define sigmoid function
sigmoid = @(F, x) F(1) ./ (1 + exp(-F(2)*(x - F(3))));
% Initial parameter guesses: [Amplitude, Slope, Midpoint]
F0 = [max(y), 1, mean(x)];
% Fit using nlinfit
F_fitted = nlinfit(x, y, sigmoid, F0);
% Define inverse sigmoid function
inverseSigmoid = @(y_val) F_fitted(3) - (1/F_fitted(2)) * log(F_fitted(1)./y_val - 1);
% Plot original fit
figure;
plot(x, y, 'bo'); hold on;
x_fit = linspace(min(x), max(x), 200);
plot(x_fit, sigmoid(F_fitted, x_fit), 'r', 'LineWidth', 2);
legend('Data', 'Sigmoid Fit');
xlabel('x'); ylabel('y'); title('Sigmoid Curve Fit');
% Plot inverse
y_vals = linspace(0.01*F_fitted(1), 0.99*F_fitted(1), 200);
x_est = inverseSigmoid(y_vals);
figure;
plot(y_vals, x_est, 'k', 'LineWidth', 2);
xlabel('y'); ylabel('Estimated x'); title('Inverse of Sigmoid Fit');
Please refer to the below MATLAB documentation to know more about sigmoid function:
I hope this helps you start!
  3 个评论
Parag
Parag 2025-4-9
The error occurs because the built-in 'sigmoid' function in MATLAB is specifically designed for use with `dlarray` inputs, which are commonly found in deep learning workflows. It does not accept standard double inputs directly, leading to following error : "Incorrect number or types of inputs or outputs for function 'sigmoid'."
Kindly, refer the following MathWorks ducumentation to know more about 'dlarray' : https://www.mathworks.com/help/deeplearning/ref/dlarray.html
You can resolve the issue using the following two methods:
1. Use a Custom-Defined Sigmoid Function
This approach is efficient and well-suited for nonlinear curve fitting tasks:
sigmoid = @(z) 1 ./ (1 + exp(-z)); % Define sigmoid for double inputs
sigmoidfcn = @(F, x) F(1) * sigmoid(F(2)*(x - F(3)));
2. Use Built-in sigmoid with dlarray
To leverage MATLAB’s built-in 'sigmoid', convert inputs to 'dlarray' and extract results:
sigmoidfcn = @(F, x) F(1) * extractdata(sigmoid(dlarray(F(2)*(x - F(3)))));
This method enables compatibility with deep learning functions while applying the sigmoid transform.
I hope the above methods helps in resolving the issue that you are facing.
Sam Chak
Sam Chak 2025-4-9
Thank you for your explanations. Previously, I believed that the sigmoid(x) function is a specific function from the Deep Learning Toolbox, named 'sigmoid' because it is exactly the mathematical expression of the logistic sigmoid function, thereby alleviating users from the need to remember the formula and the minus sign:
However, the OP's question is somewhat unclear and I believe it may relate to fitting a curve using the beautiful logit function, which is the inverse of the logistic sigmoid function.
x = linspace(0, 1, 1001);
y = log(x./(1 - x));
plot(x, y, 'linewidth', 3, 'color', "#0068D7"), grid on,
xlabel('x'), ylabel('f(x)'), title('Logit function')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by