something wrong in fit function
조회 수: 9 (최근 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
댓글 수: 0
채택된 답변
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'})
%Testing
x = linspace(1, 100);
k = randi(5)
c = randi(5)
y = 1./(1+exp(-k*(x-c)));
myfit = fit(x',y',ft)
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!