How to fit a defined function?
11 ビュー (過去 30 日間)
古いコメントを表示
I want to fit a custom function to my experimental data. For simplicity, I have some arbitary x & y values and a very simple linear function. I write the following code:
clear all;
x = [1 2 3 4 5]'; % x data
y = [.8 4 10 18 23]'; % y data
Y = lsqcurvefit(fun,1,x,y) % fitting function 'fun' defined below to find parameter 'a'
function y = fun(a,x)
y = a*x; % just a simple function for example, in actual problem, it is a long complicated function with various parameters
end
I get the following error: 'Not enough input arguements.'
I know this particular simple function can be defined as anonymous function and be fitted but I don't it that way.
0 件のコメント
採用された回答
Star Strider
2022 年 11 月 10 日
The ‘fun’ function must be presented to lsqcurvefit as a function handle using the ‘@’ operator —
x = [1 2 3 4 5]'; % x data
y = [.8 4 10 18 23]'; % y data
Y = lsqcurvefit(@fun,1,x,y) % fitting function 'fun' defined below to find parameter 'a'
function y = fun(a,x)
y = a*x; % just a simple function for example, in actual problem, it is a long complicated function with various parameters
end
.
2 件のコメント
その他の回答 (2 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!