⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fitdemo.m

📁 曲线拟合程序
💻 M
字号:
%% Optimal Fit of a Non-linear Function
% This is a demonstration of the optimal fitting of a non-linear function to a
% set of data.  It uses FMINSEARCH, an implementation of the Nelder-Mead simplex
% (direct search) algorithm, to minimize a nonlinear function of several
% variables.
%
% Copyright 1984-2002 The MathWorks, Inc. 
% $Revision: 5.15 $ $Date: 2002/04/02 17:52:33 $

%%
% First, create some sample data and plot it.

% t = (0:.1:2)';
% y = [5.8955 3.5639 2.5173 1.9790 1.8990 1.3938 1.1359 1.0096 1.0343 ...
%      0.8435 0.6856 0.6100 0.5392 0.3946 0.3903 0.5474 0.3459 0.1370 ...
%      0.2211 0.1704 0.2636]';

function [coeff,estimated_lambda] = fitdemo(XData, YData)

if ( size(XData) ~= size(YData) )
    error('The two input matrices have to be of the same sizes');
end

plot(XData,YData,'ro'); hold on; h = plot(XData,YData,'b'); hold off;
title('Input data');

if ( size(XData, 1) == 1 )
    t = XData';
    y = YData';
elseif(size(XData, 2) == 1)
    t = XData;
    y = YData;
end

%%
% The goal is to fit the following function with two linear parameters and two
% nonlinear parameters to the data:
%
%     y =  C(1)*exp(-lambda(1)*t) + C(2)*exp(-lambda(2)*t)
% 
% To fit this function, we've create a function FITFUN.  Given the nonlinear
% parameter (lambda) and the data (t and y), FITFUN calculates the error in the
% fit for this equation and updates the line (h).

% type fitfun

%%
% Make a guess for initial estimate of lambda (start) and invoke FMINSEARCH.  It
% minimizes the error returned from FITFUN by adjusting lambda.  It returns the
% final value of lambda.

start = [1;0];
options = optimset('TolX',0.1);
estimated_lambda = fminsearch('fitfun',start,options,t,y,h);
%estimated_lambda = [estimated_lambda guidata(gcf)];
coeff = guidata(h);
'estimated_lambda = estimated_lambda';
'coeff = coeff';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -