defining a fitting type

조회 수: 2 (최근 30일)
Wout Laeremans
Wout Laeremans 2023년 1월 12일
편집: Torsten 2023년 1월 16일
I have written a funcion called Even_fit.m and there are 5 coefficients that need to be found from the fit (a,b,c,d,f) and there is one variable N that I want to load from the workspace, which is already fixed before fitting. My question is how I can load this variable in the fit, since when I do:
ft_even = fittype("Even_fit(x,a,b,c,d,f,N)");
It will also take N as a fitting coefficient, which I do not want.
Thank you!

채택된 답변

Torsten
Torsten 2023년 1월 12일
편집: Torsten 2023년 1월 12일
My guess is
fitType = @(x,a,b,c,d,f)Even_fit(x,a,b,c,d,f,N);
fit(x,y,fitType)
  댓글 수: 2
Wout Laeremans
Wout Laeremans 2023년 1월 16일
This does not work
Torsten
Torsten 2023년 1월 16일
편집: Torsten 2023년 1월 16일
Put the vector of the independent variable to the end of the input list for your function "EvenFit":
x = linspace(0,1,10).';
y = 3*x.^2 + 0.1*rand(10,1);
n = 12;
fitType = @(a,b,c,x)Even_fit(x,a,b,c,n);
sol = fit(x,y,fitType)
Warning: Start point not provided, choosing random start point.
sol =
General model: sol(x) = Even_fit(x,a,b,c,n) Coefficients (with 95% confidence bounds): a = 2.803 (2.517, 3.09) b = 0.2072 (-0.09066, 0.505) c = 0.009327 (-0.05463, 0.07328)
plot(sol,x,y)
function yfit = Even_fit(x,a,b,c,n)
yfit = a*x.^2+b*x+c;
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 1월 12일
See the "Create Custom Nonlinear Models and Specify Problem Parameters and Independent Variables" and "Use Anonymous Functions to Work with Problem Parameters and Workspace Variables" examples on the fittype documentation page.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by