声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 7638|回复: 17

[综合讨论] matlab多参数指数拟合如何实现

  [复制链接]
发表于 2011-4-18 10:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我有一组数据
x=[5 6 7 8 9 .....30]
y=[0.07187  0.05888 0.04987 0.04324 0.03815 0.03417 0.03093 0.02824 0.02598 0.02406 0.02241 0.02096 0.0197 0.01857 0.01757 0.01667 0.01585 0.01511 0.01444  0.01383 0.01326 0.01275 0.01226 0.01182 0.0114 0.01101]
绘出来像个指数函数。我猜想它是指数函数,用matlab进行拟合,但是指数的底不知道,还有N多其他参数也不知道,应该如何拟合啊,求高手帮忙,要具体程序过程。我编了好几个程序都运行不成功。
回复
分享到:

使用道具 举报

 楼主| 发表于 2011-4-18 10:41 | 显示全部楼层
初始回归系数也不清楚。
发表于 2011-4-18 10:57 | 显示全部楼层
  1. General model Exp2:
  2.      f(x) = a*exp(b*x) + c*exp(d*x)
  3. Coefficients (with 95% confidence bounds):
  4.        a =      0.1916  (0.1792, 0.204)
  5.        b =      -0.319  (-0.3373, -0.3006)
  6.        c =     0.04072  (0.03814, 0.04329)
  7.             d =    -0.04487  (-0.0476, -0.04214)

  8. Goodness of fit:
  9.   SSE: 1.669e-006
  10.   R-square: 0.9997
  11.   Adjusted R-square: 0.9997
  12.   RMSE: 0.0002754
复制代码

t2011041802.jpg
  1. function createFit(x,y)
  2. %CREATEFIT Create plot of data sets and fits
  3. %   CREATEFIT(X,Y)
  4. %   Creates a plot, similar to the plot in the main Curve Fitting Tool,
  5. %   using the data that you provide as input.  You can
  6. %   use this function with the same data you used with CFTOOL
  7. %   or with different data.  You may want to edit the function to
  8. %   customize the code and this help message.
  9. %
  10. %   Number of data sets:  1
  11. %   Number of fits:  1% Data from data set "y vs. x":
  12. %     X = x:
  13. %     Y = y:
  14. %     Unweighted% Auto-generated by MATLAB on 18-Apr-2011 10:56:55% Set up figure to receive data sets and fits
  15. f_ = clf;
  16. figure(f_);
  17. set(f_,'Units','Pixels','Position',[578 254 688 485]);
  18. % Line handles and text for the legend.
  19. legh_ = [];
  20. legt_ = {};
  21. % Limits of the x-axis.
  22. xlim_ = [Inf -Inf];
  23. % Axes for the plot.
  24. ax_ = axes;
  25. set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
  26. set(ax_,'Box','on');
  27. axes(ax_);
  28. hold on;% --- Plot data that was originally in data set "y vs. x"
  29. x = x(:);
  30. y = y(:);
  31. h_ = line(x,y,'Parent',ax_,'Color',[0.333333 0 0.666667],...
  32.     'LineStyle','none', 'LineWidth',1,...
  33.     'Marker','.', 'MarkerSize',12);
  34. xlim_(1) = min(xlim_(1),min(x));
  35. xlim_(2) = max(xlim_(2),max(x));
  36. legh_(end+1) = h_;
  37. legt_{end+1} = 'y vs. x';% Nudge axis limits beyond data limits
  38. if all(isfinite(xlim_))
  39.     xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
  40.     set(ax_,'XLim',xlim_)
  41. else
  42.     set(ax_, 'XLim',[4.75, 30.25]);
  43. end% --- Create fit "fit 1"
  44. ok_ = isfinite(x) & isfinite(y);
  45. if ~all( ok_ )
  46.     warning( 'GenerateMFile:IgnoringNansAndInfs',...
  47.         'Ignoring NaNs and Infs in data.' );
  48. end
  49. st_ = [0.15243570612980811 -0.25709456653830987 0.033745642790019854 -0.038489626682204618 ];
  50. ft_ = fittype('exp2');% Fit this model using new data
  51. cf_ = fit(x(ok_),y(ok_),ft_,'Startpoint',st_);
  52. % Alternatively uncomment the following lines to use coefficients from the
  53. % original fit. You can use this choice to plot the original fit against new
  54. % data.
  55. %    cv_ = { 0.19159877789258178, -0.31895949786907274, 0.040716429909295873, -0.044867078417144955};
  56. %    cf_ = cfit(ft_,cv_{:});% Plot this fit
  57. h_ = plot(cf_,'fit',0.95);
  58. set(h_(1),'Color',[1 0 0],...
  59.     'LineStyle','-', 'LineWidth',2,...
  60.     'Marker','none', 'MarkerSize',6);
  61. % Turn off legend created by plot method.
  62. legend off;
  63. % Store line handle and fit name for legend.
  64. legh_(end+1) = h_(1);
  65. legt_{end+1} = 'fit 1';% --- Finished fitting and plotting data. Clean up.
  66. hold off;
  67. % Display legend
  68. leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
  69. h_ = legend(ax_,legh_,legt_,leginfo_{:});
  70. set(h_,'Interpreter','none');
  71. % Remove labels from x- and y-axes.
  72. xlabel(ax_,'');
  73. ylabel(ax_,'');
复制代码

评分

1

查看全部评分

 楼主| 发表于 2011-4-18 13:45 | 显示全部楼层
回复 3 # qibbxxt 的帖子

不好意思啊,求教高手,前面的系数是怎么求出来的?直接用的matlab的工具箱吗?还是程序运行的结果啊?
 楼主| 发表于 2011-4-18 14:28 | 显示全部楼层
回复 3 # qibbxxt 的帖子

另外,您怎么这么厉害,不知道有什么学习经验可以传授一下吗?
发表于 2011-4-18 14:49 | 显示全部楼层
  1. x=[5:30];
  2. >> y=[0.07187 0.05888 0.04987 0.04324 0.03815 0.03417 0.03093 0.02824 0.02598 0.02406 0.02241 0.02096 0.0197 0.01857 0.01757 0.01667 0.01585 0.01511 0.01444 0.01383 0.01326 0.01275 0.01226 0.01182 0.0114 0.01101];
  3. >> f=fittype('a*exp(b*x)+c*exp(d*x)','coeff',{'a','b','c','d'});
  4. >> [c,gof2]=fit(x',y',f)
  5. Warning: Start point not provided, choosing random start point.
  6. > In fit>handlewarn at 705
  7. In fit at 321

  8. c =

  9. General model:
  10. c(x) = a*exp(b*x)+c*exp(d*x)
  11. Coefficients (with 95% confidence bounds):
  12. a = 0.00362 (-0.02079, 0.02803)
  13. b = 0.1983 (-0.107, 0.5036)
  14. c = -3.244e-010 (-1.577e-008, 1.512e-008)
  15. d = 0.7468 (-0.7801, 2.274)


  16. gof2 =

  17. sse: 1.7698
  18. rsquare: -282.6509
  19. dfe: 22
  20. adjrsquare: -321.3306
  21. rmse: 0.2836
复制代码
请教主任,我用的上面的指数函数进行拟合,为什么和你给出的系数差别如此大呢!请指教!
发表于 2011-4-18 14:55 | 显示全部楼层
回复 4 # sophialll 的帖子

这有些也是跟qibbxxt学的, 我来补充下
1.command window下cftool
2.>> data %% 输入x,y
3.>> Fitting >> New fit
4. Type of fit >> Exponential >> 选a*exp(b*x) + c*exp(d*x) >> apply
5. File >> Generate M-file
发表于 2011-4-18 15:29 | 显示全部楼层
回复 7 # ChaChing 的帖子

用cftool工具和我自己用代码操作(代码在6楼),选择的函数都是指数函数,为什么拟合的系数差别比较大呢!
个人感觉应该和warning有关,是不是应该给定初值,如果是这样的话,请教您初值一般应该如何给才能保证精度呢?
 楼主| 发表于 2011-4-18 19:50 | 显示全部楼层
回复 7 # ChaChing 的帖子

:handshake太感谢了,都是高手
发表于 2011-4-18 22:03 | 显示全部楼层
回复 8 # meiyongyuandeze 的帖子

最近回帖都没刷新下, 所以都没注意到有新回帖!
以前自己没使用过Curve Fitting Toolbox, 来此为了回帖才稍微看下的
试下你的写法, 的确不同, 但也与你的不同, 我的是R2009a
  1. >> f=fittype('a*exp(b*x)+c*exp(d*x)','coeff',{'a','b','c','d'});

  2. >> [c,gof2]=fit(x',y',f)
  3. Warning: Start point not provided, choosing random start point.
  4. > In fit>handlewarn at 817
  5.   In fit at 380
  6. Maximum number of function evaluations exceeded. Increasing
  7. MaxFunEvals (in fit options) may allow for a better fit, or
  8. the current equation may not be a good model for the data.

  9. c =

  10.      General model:
  11.        c(x) = a*exp(b*x)+c*exp(d*x)
  12.      Coefficients (with 95% confidence bounds):
  13.        a =     -0.0237  (-3.612e+004, 3.612e+004)
  14.        b =      0.9074  (-234.8, 236.6)
  15.        c =     0.02348  (-3.612e+004, 3.612e+004)
  16.        d =      0.9077  (-235.7, 237.5)

  17. gof2 =

  18.            sse: 6.5381e+012
  19.        rsquare: -1.0479e+015
  20.            dfe: 22
  21.     adjrsquare: -1.1907e+015
  22.           rmse: 5.4515e+005
复制代码
发表于 2011-4-18 22:06 | 显示全部楼层
本帖最后由 ChaChing 于 2011-4-18 22:09 编辑

比较下与3F的差异, 不难发现是fittype的差异
至於两者的差异, 有空再看看help说明了, 汗, 还没详看

  1. >> f=fittype('exp2');
  2. >> [c,gof2]=fit(x',y',f)

  3. c =

  4.      General model Exp2:
  5.        c(x) = a*exp(b*x) + c*exp(d*x)
  6.      Coefficients (with 95% confidence bounds):
  7.        a =      0.1916  (0.1792, 0.204)
  8.        b =      -0.319  (-0.3373, -0.3006)
  9.        c =     0.04072  (0.03814, 0.04329)
  10.        d =    -0.04487  (-0.0476, -0.04214)

  11. gof2 =

  12.            sse: 1.6686e-006
  13.        rsquare: 0.9997
  14.            dfe: 22
  15.     adjrsquare: 0.9997
  16.           rmse: 2.7540e-004
复制代码

点评

看了一晚上的help文件,在百度和google上晃了很长时间,也没发现问题的所在,汗........  发表于 2011-4-19 22:12
发表于 2011-4-20 00:09 | 显示全部楼层
回复 8 # meiyongyuandeze 的帖子

我也没发现问题的所在!
无编辑权限了, 不然真想从10F开始分割成另一帖, 因已是另一个问题
发表于 2011-4-20 08:54 | 显示全部楼层
回复 12 # ChaChing 的帖子

我已经将问题抽来,重新分割成另外的一个帖子,请关注讨论!
发表于 2011-4-21 10:48 | 显示全部楼层
就那么实现呗!!!

点评

反对: 2.0
反对: 2
  发表于 2011-4-21 23:41
头像被屏蔽
发表于 2011-4-21 11:04 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-30 00:28 , Processed in 0.104382 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表