something wrong in fit function

3 次查看(过去 30 天)
I am using matlab in a mooc and apparently missing some point. The code is
% Create the custom logistic fittype with fit parameters k and c
ft = fittype( 'y= 1./(1+exp(-k*(x-c)))',...
'Independent','x', 'Dependent','y',...
'Coefficients',...
('k','c'));
The error message is
File: solution.m Line: 9 Column: 5
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
The problem seems to be the comma between 'k' and 'c'. Since there are 2 parameters I need a comma between them. I have tried everything I can think of but I can't get past the error messages (which are slightly different depending on the exact format of the expression). In all cases it seems to be complaining about that comma.
I have never used matlab for an actual project, so maybe I am missing something obvious. I have been working on it for hours and can't get it to fly. Could someone with more experience please tell me what my actual error is? It makes no sense that it is really the comma.
Thanks,
ilan

采纳的回答

Dyuman Joshi
Dyuman Joshi 2024-1-7
You can use either a cell array of char vectors or string array for multiple coefficients (or dependent/independent variables for that matter).
Remove the 'y = ' part, as that is not needed.
% Create the custom logistic fittype with fit parameters k and c
ft = fittype('1./(1+exp(-k*(x-c)))',...
'Independent','x', 'Dependent','y',...
'Coefficients', {'k', 'c'})
ft =
General model: ft(k,c,x) = 1./(1+exp(-k*(x-c)))
%Testing
x = linspace(1, 100);
k = randi(5)
k = 1
c = randi(5)
c = 5
y = 1./(1+exp(-k*(x-c)));
myfit = fit(x',y',ft)
Warning: Start point not provided, choosing random start point.
myfit =
General model: myfit(x) = 1./(1+exp(-k*(x-c))) Coefficients (with 95% confidence bounds): k = 1 (1, 1) c = 5 (5, 5)
  2 个评论
Ilan Tal
Ilan Tal 2024-1-7
Thanks for your answer. The y= part was already something I didn't notice after hours of attempts.
Sometime a fresh pair of eyes is all it takes. Now the error messages are gone....
Dyuman Joshi
Dyuman Joshi 2024-1-7
Yes, a new perspective does help out!
Glad to have helped :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by