Clear Filters
Clear Filters

Custom fittype gives nonsensical parameters in 3 variables but not 2

3 views (last 30 days)
I am trying to fit the following function to some data to the function a/(1+e^(b(x-c))).
I have tried the following
func = @(p1, p2, p3, x) p1./(1+(exp(1).^(p2.*(x-p3))));
fit_type = fittype(func);
data_fit = fit(data_x,data_y, fit_type);
plot(data_x,data_y,"ko")
plot(fit,"r-")
When I plot the output the fitted line is practically a horizontal line at the data's mean where as the data has the shape of a mirrored s curve. However when I manually set p1 or p3 to close to the correct parameter by hand the fit function returns a very good fit of the data. When I manually set p2 to a close to correct parameter I get some error that I cannot understand. The error being:
Error using fittype/testCustomModelEvaluation (line 16)
Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data. This custom equation fails to meet that requirement:
(p1)/(1+(exp(1).^((1.365).*(x-p2))))
I tried adding the following options, but nothing changed.
fit_options = fitoptions('Method','NonlinearLeastSquares', 'MaxFunEvals', 1200, 'MaxIter', 800);
fit_type = fittype(func, 'options', fit_options);
The closest I could find in terms of examples is this https://au.mathworks.com/help/curvefit/custom-nonlinear-census-analysis.html, however it is only in 2 variables.
What am I doing wrong with the custom curve fit?
Note: I cannot give out the raw data I am using, nor the context in which I am using it.

Answers (1)

Aditya
Aditya on 4 Jun 2024
It looks like there are a few issues and possible misunderstandings in your approach to fitting the custom function a/(1+e^(b(x-c))) to your data. Let's address them step by step:
1. Function Definition and fittype Usage
Your function definition and the way you're trying to use fittype seem mostly correct but might be simplified or clarified for better understanding and debugging. The error message you received suggests there's an issue with how the function handles the inputs, especially regarding the dimensions of inputs and outputs.
2. Error in Custom Equation
The error message you encountered when manually setting parameters suggests that the evaluation of your function with those parameters does not produce an output array of the same size as the input x. This could be due to how the operation is parsed or executed internally. However, the expression you provided in the error message seems to have a hardcoded parameter, which might be part of an incorrectly communicated issue rather than the code you're actually running.
3. Correcting the Function and Fitting Process
Let's ensure your function is defined correctly for the fitting process. MATLAB's fit function expects the independent variable (x in your case) to be the first argument of your function after the parameters. Also, ensure your function can handle vectorized inputs properly, which seems to be the case here, but it's always good to double-check.
Here is a clearer way to define your function and perform the fit:
% Define your custom function
func = @(x, p1, p2, p3) p1 ./ (1 + exp(p2 .* (x - p3)));
% Create a fittype from your function
fit_type = fittype(func, 'independent', 'x', 'coefficients', {'p1', 'p2', 'p3'});
% Optional: Define initial guesses and bounds if necessary
startPoints = [1, 1, 1]; % Example starting points, adjust based on your data
fit_options = fitoptions('Method','NonlinearLeastSquares', ...
'StartPoint', startPoints, ...
'MaxFunEvals', 1200, ...
'MaxIter', 800);
% Update fittype with the options
fit_type = setoptions(fit_type, fit_options);
% Perform the fit
data_fit = fit(data_x, data_y, fit_type);
% Plot the original data
plot(data_x, data_y, 'ko'); hold on;
% Plot the fit
plot(data_x, feval(data_fit, data_x), 'r-'); hold off;

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!