How to define fittype object from custom function?

조회 수: 5 (최근 30일)
Fotaras Sitof
Fotaras Sitof 2015년 11월 24일
답변: Aditya 2025년 3월 3일
Dear all,
I'm trying to define a fittype object from the function y = fun(x,c1,c2,c3,c4,c5,c6,P), where c1,c2,c3,c4,c5,c6 are the variable fitting parameters and P is a constant struct:
ft = fittype(@(c1,c2,c3,c4,c5,c6,x) fun(x,c1,c2,c3,c4,c5,c6,P), 'independent', {'x'}, 'dependent', {'y'});
but get the following errors:
Error using fittype>iTestCustomModelEvaluation (line 730) 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:
Error in fittype>iCreateFittype (line 367) iTestCustomModelEvaluation( obj );
Error in fittype (line 324) obj = iCreateFittype( obj, varargin{:} );
The function works fine producing same size x & y data so I have no clue why defining the fittype fails.
Thanks for any help.

답변 (1개)

Aditya
Aditya 2025년 3월 3일
HI Fotaras,
When defining a fittype object in MATLAB, it's crucial to ensure that your custom function returns an output of the same size and shape as the input data. The error you're encountering suggests that this condition might not be satisfied when using your function fun.
% Example input
x_test = linspace(0, 10, 100); % Example x data
c1 = 1; c2 = 2; c3 = 3; c4 = 4; c5 = 5; c6 = 6; % Example parameters
P = struct('param1', value1, 'param2', value2); % Define P with your specific fields
% Evaluate the function
y_test = fun(x_test, c1, c2, c3, c4, c5, c6, P);
% Check size
disp(size(x_test));
disp(size(y_test));
% Define the fittype
ft = fittype(@(c1, c2, c3, c4, c5, c6, x) fun(x, c1, c2, c3, c4, c5, c6, P), ...
'independent', {'x'}, 'dependent', {'y'}, ...
'coefficients', {'c1', 'c2', 'c3', 'c4', 'c5', 'c6'});
function y = fun(x, c1, c2, c3, c4, c5, c6, P)
% Your function implementation
% Ensure y is the same size as x
end

카테고리

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