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

效果如下

代码如下

%% 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. [BZOJ2453]维护队列|分块

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  2. pycharm设置 django模板语言

    ``` 参考:https://www.zhihu.com/question/65342278/answer/229993987 在setting-language&frameworks-pyt ...

  3. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  4. Linux设置编译器环境变量

    Linux设置编译器环境变量 https://jingyan.baidu.com/article/9f7e7ec0bb22aa6f29155453.html Linux添加环境变量与GCC编译器添加I ...

  5. php扩展

    swoole 命令行/php-fpm下实现的 多线程.异步.多种网络协议等 https://wiki.swoole.com/wiki/page/6.html 编译安装, 注意:需命令行运行,作为内部服 ...

  6. Hibernate多对多两种情况

    Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...

  7. 搜索引擎--范例:谈谈django--mysql数据库的一些常用命令

    现在基本没有什么能离得开数据库了,django我一直用的都是mysql的数据库,这次和大家说说django--mysql数据库的一些常用命令吧 1:命令行登陆mysql C:\Users\Admini ...

  8. Django基础之视图

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  9. (7)case语句

    (1)case 语法 case "变量" in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; 模式3) 命令序列3 ;; *) 无匹配后命令序列 esac (2)多系统配 ...

  10. (一)mysql基础和安装mysql5.7

    (1)数据库系统 RDS:关系型,oracle,mysql,mariaDB,percona server ,DB2 NoSQL:Redis,MongoDB,memcache (2)SQL语言:结构化查 ...