フィルターのクリア

curve fitting without the toolbox

207 ビュー (過去 30 日間)
pavlos
pavlos 2014 年 4 月 17 日
コメント済み: Shariefa Shaik 2018 年 2 月 28 日
Hello,
I would like to ask if there are any functions that can I use to fit two series of data without using the Curve Fitting Toolbox.
For example is there a built-in function to fit the data through the "Exponential" type of fitting
a*exp(b*x)
that is found in the toolbox?
If not, how can I write one that performs the "Exponential" fitting?
Thank you very much.
Best,
Pavlos

採用された回答

Star Strider
Star Strider 2014 年 4 月 17 日
There are the functions lsqcurvefit (Optimization Toolbox) and nlinfit (Statistics Toolbox) that will fit an objective function you provide. They each have their own advantages and disadvantages, depending upon what you want to do.
If you don’t have those, using the MATLAB core function fminsearch can do the nonlinear fit with an additional line of code (the OLS cost function). (See the fminsearch documentation for details on what it does and how it works.)
This works:
y = @(b,x) b(1).*exp(-b(2).*x); % Objective function
p = [3; 5]*1E-1; % Create data
x = linspace(1, 10);
yx = y(p,x) + 0.1*(rand(size(x))-0.5);
OLS = @(b) sum((y(b,x) - yx).^2); % Ordinary Least Squares cost function
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(2,1), opts) % Use ‘fminsearch’ to minimise the ‘OLS’ function
figure(1)
plot(x, yx, '*b')
hold on
plot(x, y(B,x), '-r')
hold off
grid

その他の回答 (1 件)

Khalifa Niang
Khalifa Niang 2016 年 8 月 1 日
how to print the values of b(1), b(2)......?
  1 件のコメント
Shariefa Shaik
Shariefa Shaik 2018 年 2 月 28 日
explain the code sir

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by