这个函数的功能是能自动搜索参数的取值,从而使得方程的误差最小。

效果如下

代码如下

%% 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 nargin<1,
XData = (0:.1:2)';
YData = [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]';
clc;
end 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); % C1,C2
'estimated_lambda = estimated_lambda';
'coeff = coeff';

要拟合的函数为

function err = fitfun(lambda,t,y,handle)
%FITFUN Used by FITDEMO.
% FITFUN(lambda,t,y,handle) returns the error between the data and the values
% computed by the current function of lambda.
%
% FITFUN assumes a function of the form
%
% y = c(1)*exp(-lambda(1)*t) + ... + c(n)*exp(-lambda(n)*t)
%
% with n linear parameters and n nonlinear parameters. % Copyright 1984-2002 The MathWorks, Inc.
% $Revision: 5.8 $ $Date: 2002/04/08 20:04:42 $ A = zeros(length(t),length(lambda));
for j = 1:length(lambda)
A(:,j) = exp(-lambda(j)*t);
end
c = A\y;
z = A*c;
err = norm(z-y); set(gcf,'DoubleBuffer','on');
set(handle,'ydata',z)
guidata(handle, c);
drawnow
pause(.04)

因为这种拟合方法用的比较少,所以特此总结出来,希望后来对自己的道路段的拟合会有用处!

2016-1-23 1059

Matlab自带的曲线拟合程序的更多相关文章

  1. MATLAB实例:非线性曲线拟合

    MATLAB实例:非线性曲线拟合 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用最小二乘法拟合非线性曲线,给出两种方法:(1)指定非线性函数,(2) ...

  2. DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...

  3. DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  4. DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  5. Qt配置cmake;运行带参数的程序

    配置cmake编译器,步骤如下: 步骤1:  Qt下新建一个project. 步骤2:  在该project目录下创建一个CMakeLists.txt文件,并按规范编写该文件. Tip: projec ...

  6. 使用matlab自带工具实现rcnn

    平台:matlab2016b matlab自带一个cifar10Net工具可用于深度学习. 图片标注 这里使用的是matlab自带的工具trainingImageLabeler对图像进行roi的标注. ...

  7. [转]利用C#自带组件强壮程序日志

    利用C#自带组件强壮程序日志   在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...

  8. sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4

    sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4是最官方的新版,免费开源,用官方最新发布原版汉化.未更改任何内核文件. Sphider 是一个完美的带有蜘蛛的搜索引擎程序. S ...

  9. 转载——利用C#自带组件强壮程序日志

    利用C#自带组件强壮程序日志   在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...

随机推荐

  1. Junit使用的超简单介绍

    Junit使用的超简单介绍 前言:我对Junit了解的并不多,只是今天突然听到有人提到了它,而且现在时间还早,所以我觉得我不妨更一篇关于Junit4的超级超级简单的用法,全当是为了省去看官网demo的 ...

  2. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  3. Ubuntu下hadoop集群搭建

    --修改IP地址(克隆镜像后可修改可不修改) http://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html -------------- ...

  4. JVM 性能排查 自己做的笔记

    Live Memory 1.Class Tracker 展示类或包的实例个数与时间的关系.需要录制才可以看到. 1)可以添加指定的类或包. 2.All Objects 查看所有类的实例个数和大小.可以 ...

  5. 汕头市队赛 SRM 06 C 秀恩爱

    C 秀恩爱 SRM 06 背景&&描述         KPM坐在直升机上俯瞰小渔村景象.         渔村可看作二维平面,密密麻麻地到处都是单身狗,KPM当前所在坐标为(sx,s ...

  6. 【洛谷 P1712】 [NOI2016]区间 (线段树+尺取)

    题目链接 emmm看起来好像无从下手, \(l_i,r_i\)这么大,肯定是要离散化的. 然后我们是选\(m\)个区间,我们先对这些区间按长度排个序也不影响. 排序后,设我们取的\(m\)个区间的编号 ...

  7. DotNETCore 学习笔记 依赖注入和多环境

    Dependency Injection ------------------------------------------------------------------------ ASP.NE ...

  8. linux软件管理(Vim编辑器使用) ——(七)

    windows : .exe     安装 .卸载 安装:  mysql.exe  cc.exe 卸载 : 该软件唯一的标识  ,包名   alibaba android : *.apk   卸载 包 ...

  9. 时间戳 JS PHP MYSQL

    Unix 时间戳  Unix timestamp ('1970-01-01 00:00:00' GMT 之后的秒数) JS var strtime = '2014-04-23 18:55:49:123 ...

  10. python的算法:二分法查找(1)

    1.什么是二分法查找: 1.从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: 2.如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从 ...