博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matlab自带的曲线拟合程序
阅读量:5096 次
发布时间:2019-06-13

本文共 2835 字,大约阅读时间需要 9 分钟。

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

效果如下

代码如下

%% 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;endif ( size(XData) ~= size(YData) )    error('The two input matrices have to be of the same sizes');endplot(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);endc = A\y;z = A*c;err = norm(z-y);set(gcf,'DoubleBuffer','on');set(handle,'ydata',z)guidata(handle, c);drawnowpause(.04)

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

2016-1-23 1059

 

转载于:https://www.cnblogs.com/arxive/p/5152950.html

你可能感兴趣的文章
★一张图弄明白从零维到十维
查看>>
Java开发学习心得(一):SSM环境搭建
查看>>
固定渲染管线与可编程渲染管线的区别
查看>>
MVC框架
查看>>
微信小程序-发起 HTTPS 请求
查看>>
<Java><!!!><面试题>
查看>>
oracle 11g 导出数据库时,出现空表无法导出问题解决
查看>>
WPF动画设置1(转)
查看>>
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
IOS push消息的数字不减少的问题
查看>>
mysql报错Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>