Matlab自带的曲线拟合程序
这个函数的功能是能自动搜索参数的取值,从而使得方程的误差最小。
效果如下
代码如下
%% 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自带的曲线拟合程序的更多相关文章
- MATLAB实例:非线性曲线拟合
MATLAB实例:非线性曲线拟合 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用最小二乘法拟合非线性曲线,给出两种方法:(1)指定非线性函数,(2) ...
- DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...
- DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- Qt配置cmake;运行带参数的程序
配置cmake编译器,步骤如下: 步骤1: Qt下新建一个project. 步骤2: 在该project目录下创建一个CMakeLists.txt文件,并按规范编写该文件. Tip: projec ...
- 使用matlab自带工具实现rcnn
平台:matlab2016b matlab自带一个cifar10Net工具可用于深度学习. 图片标注 这里使用的是matlab自带的工具trainingImageLabeler对图像进行roi的标注. ...
- [转]利用C#自带组件强壮程序日志
利用C#自带组件强壮程序日志 在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...
- sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4
sphider 丁廷臣简体中文完美汉化版带蜘蛛搜索引擎程序 v1.3.4是最官方的新版,免费开源,用官方最新发布原版汉化.未更改任何内核文件. Sphider 是一个完美的带有蜘蛛的搜索引擎程序. S ...
- 转载——利用C#自带组件强壮程序日志
利用C#自带组件强壮程序日志 在项目正式上线后,如果出现错误,异常,崩溃等情况 我们往往第一想到的事就是查看日志 所以日志对于一个系统的维护是非常重要的 声明 正文中的代码只是一个栗子,一个非常简 ...
随机推荐
- 「6月雅礼集训 2017 Day1」看无可看
[题目大意] 给出n个数,a[1]...a[n],称作集合S,求
- 路径方案数_mod_SPFA_记忆化搜索_C++
本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本来是写个 DP 分分钟就 A 了,结果老师要我们写记忆化搜索(无奈脸) 算啦,随手一改又是一个标准的记忆化搜索(目测好像是记忆化搜索容易 ...
- 矩阵快速幂&T1
T1 知识储备 在写这一题之前,我们首先要了解矩阵乘法(我就是因为不懂弄了好久...) 矩阵的运算()-----(信息学奥赛一本通之提高篇) 矩阵的加法减法是十分简单的,就是把2个矩阵上对应的位置相加 ...
- algorithm ch2 insertsort
刚开始看到insertsort,思路就是使用新来的元素与前述已经排好序的元素比较.然后进行插入或者跳到下一次比较. 实现的代码如下: void InsertSort(int *pArray, int ...
- LeetCode 6:Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...
- (转)linux samb服务器配置
1. 首先安装samba服务. 2. 配置防火墙(要先关闭) 3. 创建samba服务器用户:smbpasswd -a username 删除samba服务器用户:smbpasswd -x u ...
- 【bzoj2242】计算器
#include<bits/stdc++.h> #define inf 1000000000 using namespace std; typedef long long ll; ?a:g ...
- 2.flume架构以及核心组件
flume组件主要包含三部分 source:从各个地方收集数据 channel:聚集,相当于临时数据存放的地方.因为数据来的时候,不可能来一条便写一次,那样效率太低,而是先把数据放在通道里,等通道满了 ...
- 对象存储 - Swift 原理 及 Swift+keystone+dashboard 架构搭建
1. 原理介绍 Swift 架构.原理及功能: http://www.cnblogs.com/sammyliu/p/4955241.html 总结的很详细也很全面,受益匪浅,感谢分享. 2. keys ...
- selenium 多表单切换处理(iframe/frame)
在web应用中,前台网页的设计一般会用到iframe/frame表单嵌套页面的应用.简单的就是一个页面签嵌套多个HEML/JSP文件.selenium webdriver 只能在同一页面识别定位元素 ...