楼主: fantuanxiaot
13133 151

[程序化交易] [新年大礼]MATLAB资源、代码、书籍分享[by fantuanxiaot]   [推广有奖]

版主

Ψ▄┳一大卫卍卐席尔瓦

大师

9%

还不是VIP/贵宾

-

威望
7
论坛币
-172117 个
通用积分
13.0175
学术水平
3775 点
热心指数
3811 点
信用等级
3447 点
经验
150468 点
帖子
7695
精华
32
在线时间
1322 小时
注册时间
2013-2-3
最后登录
2019-7-29

初级学术勋章 初级热心勋章 中级热心勋章 中级学术勋章 初级信用勋章 中级信用勋章 高级热心勋章 高级学术勋章 特级学术勋章 特级热心勋章 高级信用勋章 特级信用勋章

fantuanxiaot 发表于 2014-12-27 13:38:17 |显示全部楼层
本帖最后由 fantuanxiaot 于 2014-12-30 09:41 编辑

        fantuanxiaot的新年大礼:一些MATLAB代码、各类学习资料、书籍等。        祝大家新年快乐!     

  其余请见藤椅、地板、板凳、报纸、8楼、10楼等本页的楼层       

我原来的一些资源帖子:
[原创]MATLAB提取整数多项式
[原创]MATLAB网页爬取我偶像的若干图片
[原创]将离歌版主的风险收益做了个gui
[原创]太阳出来爬山gif图
[原创]期权定价的二叉树和三叉树图
[原创]欧式期权三角和核弹头作图
[原创]马克维茨边界作图
[原创]遗传算法模拟退火

更多精彩请见:量化投资离歌量化各类软件实战

一,资料与文献分享、神经网络、配对交易、HMM等

本帖隐藏的内容



2 投资组合选择.pdf (1.63 MB)
3 Pairs Trading with Robust Correlation.pdf (333.94 KB)
7 Stock_Prediction_–_A_Neural_Network_Approach.pdf (577.88 KB) 8 Modified Neural Network Algorithms for Predicting Trading Signals o f Stock Ma.pdf (1.87 MB)
9 indexing_and_statistical_arbitrage.pdf (4.89 MB)
10 Next-Day_Stock_Trend_Prediction_Using_the_Self-organizing_Associative_Memory_.pdf (69.25 KB)
2 Improving_technical_trading_systems_by_using_a_new_MATLAB-based_genetic_algori.pdf (904.09 KB)
1 regexp_Matlab.pdf (365.77 KB)
21 matlab正则表达式.doc (85.5 KB)
20 matlab正则基础.doc (64.5 KB)
1 Bayesian Networks for Data Mining.pdf (364.8 KB)
25 隐马尔可夫模型.pdf (190.09 KB)
23 EM算法介绍.pdf (1003.71 KB)
24 EM算法与HMM参数估计简介.pdf (310.79 KB)

4 Dynamic modeling of mean-reverting spreads.pdf (506.57 KB)
5 Genetic Programming in Statistical Arbitrage.pdf (978.27 KB)

16 stat_arb markets.pdf (327.91 KB) 17 Statistical Arbitrage and Market Efficiency.pdf (404.65 KB)
19 Optimal Arbitrage Strategies.pdf (371.48 KB)
20 Synthetic Floating Crude Oil Storage.pdf (499.89 KB)



二,MATLAB的代码分享

一个图形的复制技术:by fantuanxiaot

Copy_Plot.jpg

本帖隐藏的内容

  1. %  copyright by fantuanxiaot
  2. %  基于matlab的图片复制
  3. %  一个图片复制动态展示
  4. %  看看图片一是如何复制到图片二的
  5. clc
  6. clear all
  7. close all
  8. %  设置一个图1
  9. %  图1的句柄
  10. h1=figure(1);
  11. t=0:pi/10:2*pi;
  12. r=2;
  13. x=r*exp(i*t);
  14. s='g';
  15. plot(x,s,'linewidth',2)
  16. hold on
  17. plot(x,'rp','markersize',10)
  18. xlabel('Real')
  19. ylabel('Image')
  20. positions=get(h1,'position');
  21. %  图2的句柄
  22. h2=figure(2);
  23. set(h2,'position',[positions(1)-300,positions(2),positions(3),positions(4)])
  24. set(h1,'position',[positions(1)+300,positions(2),positions(3),positions(4)])
  25. pause(1)
  26. %  图1的轴句柄
  27. h1_axes=findobj(h1,'type','axes');
  28. h2_axes=copyobj(h1_axes,h2);
  29. %  这样图1就复制到了图2
复制代码


一个图形让X轴标注并且使得文字倾斜:by faruto

Rotation_Example.jpg


本帖隐藏的内容

  1. %  copyright by faruto and fantuanxiaot
  2. %  一个让x轴和y轴标注倾斜的窍门
  3. clc
  4. close all
  5. clear all
  6. figure('color',[0.5 1 1],'units','normal','position',[0 0 0.4 0.5])
  7. movegui(gcf,'center')
  8. plot(magic(16))
  9. %  获得句柄
  10. current_axes=gca;
  11. strx=get(current_axes,'XTickLabel');
  12. stry=get(current_axes,'YTickLabel');
  13. x=get(current_axes,'XTick');
  14. y=get(current_axes,'YTick');
  15. yl=ylim(current_axes);
  16. xl=xlim(current_axes);
  17. %  句柄设置
  18. set(current_axes,'XTickLabel',[]);
  19. set(current_axes,'YTickLabel',[]);
  20. %  使之倾斜
  21. xtoy=zeros(1,length(x))+yl(1)-(max(yl)-min(yl))/20;
  22. ytox=zeros(1,length(y))+xl(1)-(max(xl)-min(xl))/20;
  23. text(x,xtoy,strx,'rotation',40,'HorizontalAlignment'...
  24.     ,'center','FontName','times','FontSize',16,'backgr','g');
  25. text(ytox,y,stry,'rotation',-40,'HorizontalAlignment'...
  26.     ,'center','FontName','times','FontSize',16,'backgr','m');
复制代码


一些图形大家自己看着吧:by fantuanxiaot

Patch_Plot.jpg


本帖隐藏的内容


  1. %  各类养眼的作图
  2. %  大家拿去自己练习练习
  3. %  copyright by fantuanxiaot
  4. clc
  5. clear all
  6. close all
  7. format compact
  8. figure('color',[0.5 1 1],'position',[100 80 1200 580])
  9. %%  右嵌入的作图
  10. subplot(2,3,1)
  11. patch('xdata',[ones(1,11)*5 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
  12.     'facecolor',[1 0.9 0.9],'edgecolor','g')
  13. hold on
  14. plot(1:10,'color',[0.7 0 1])
  15. %  上左边
  16. legend('patch plot','straight line',2)
  17. set(gca,'fontname','Times New Roman')
  18. %%  左嵌入的作图
  19. subplot(2,3,2)
  20. patch('xdata',[ones(1,11)*0 ones(1,11)*5],'ydata',[10:-1:0 0:10],...
  21.     'facecolor',[1 0.9 0.9],'edgecolor','g')
  22. hold on
  23. plot(1:10,'color',[0.7 0 1])
  24. %  上右边
  25. legend('patch plot','straight line',1)
  26. set(gca,'fontname','Times New Roman')
  27. %%  两边嵌入的作图
  28. subplot(2,3,3)
  29. patch('xdata',[ones(1,11)*0 ones(1,11)*4.99],'ydata',[10:-1:0 0:10],...
  30.     'facecolor',[1 0.9 0.9],'edgecolor','g')
  31. hold on
  32. patch('xdata',[ones(1,11)*5.01 ones(1,11)*10],'ydata',[0:10 10:-1:0],...
  33.     'facecolor',[1 1 0],'edgecolor','g')
  34. plot(1:10,'color',[0.7 0 1],'linewidth',1)
  35. %  上右边
  36. legend('patch plot 1','patch plot 2','straight line',1)
  37. set(gca,'fontname','Times New Roman')
  38. %%  做一个多维色彩的bar图形
  39. subplot(2,3,4)
  40. y=randn(1,10);
  41. h_bar=bar(y);
  42. h_bar_children=get(h_bar,'Children');
  43. %  每一行代表四个点的坐标
  44. h_matrix=get(h_bar_children,'Faces');
  45. %  每一个坐标代表
  46. color_matrix=get(h_bar_children,'FaceVertexCData');
  47. %  对每一行h_matrix,也就是一个方块,通过color_matrix取出4个点的坐标,其对应的颜色在color_matrix中,将4个点的颜色
  48. %  信息均修改为more_color
  49. for more_color=1:length(y)
  50. color_matrix(h_matrix(more_color,:))=more_color;
  51. end
  52. set(h_bar_children,'FaceVertexCData',color_matrix)
  53. legend('bar plot')
  54. set(gca,'fontname','Times New Roman')
  55. colorbar
  56. %%  做一个bar图形,上面与下面的颜色不同
  57. clear all
  58. subplot(2,3,5)
  59. y=randn(1,100);
  60. y=sort(y);
  61. h_bar=bar(y);
  62. h_bar_children=get(h_bar,'Children');
  63. %  每一行代表四个点的坐标
  64. h_matrix=get(h_bar_children,'Faces');
  65. %  每一个坐标代表
  66. color_matrix=get(h_bar_children,'FaceVertexCData');
  67. %  上下颜色不同
  68. big_index=find(y>=0);
  69. small_index=find(y<0);
  70. color_matrix(h_matrix(small_index,:))=8;
  71. color_matrix(h_matrix(big_index,:))=4;
  72. set(h_bar_children,'FaceVertexCData',color_matrix,'edgecolor','none')
  73. legend('bar plot')
  74. set(gca,'fontname','Times New Roman')
  75. %%  另一类bar图形,区分收益率的正负
  76. subplot(2,3,6)
  77. %  模拟的收益率
  78. N=100;
  79. c=normrnd(0,1,1,100);
  80. index_big=find(c>=0);
  81. index_small=find(c<0);
  82. index_date_num=1:N;
  83. bar(index_date_num(index_big),c(index_big),'facecolor','g','edgecolor','g')
  84. hold on
  85. bar(index_date_num(index_small),c(index_small),'facecolor','r','edgecolor','r')
  86. set(gca,'fontname','Times New Roman')
  87. legend('bar plot')
  88. clear all
复制代码



如何用MATLAB作图跟踪自己的动态权益收益:by fantuanxiaot

Return_Rotation.jpg

本帖隐藏的内容


  1. %  用matlab如何记录自己的收益与回报
  2. %  正的收益红色,负的收益绿色
  3. %  copyright by fantuanxiaot
  4. %  准备工作
  5. clc
  6. clear all
  7. close all
  8. format compact
  9. figure(1)
  10. set(figure(1),'color',[0.5 1 1])
  11. set(figure(1),'position',[1 1 800 530])
  12. movegui(figure(1),'center')
  13. set(gca,'units','normalized','position',[0.15,0.2,0.7,0.7])
  14. %  取最近100天的dynamic return
  15. N=100;
  16. %  记录收益标准的天数
  17. M=20;
  18. %  收益率的准备
  19. single_return=normrnd(0,0.07,1,N);
  20. date_num=today-N+1:today;
  21. date_str=datestr(date_num,29);
  22. date_cellstr=cellstr(date_str);
  23. %  做柱状图
  24. x_index=1:N;
  25. one_index=find(single_return>=0);
  26. two_index=find(single_return<0);
  27. bar(x_index(one_index),single_return(one_index),...
  28.     'facecolor','r','edgecolor','r')
  29. hold on
  30. bar(x_index(two_index),single_return(two_index),...
  31.     'facecolor','g','edgecolor','g')
  32. title('Follow Your Return Day by Day','fontname',...
  33.     'Times','fontsize',14)
  34. set(gca,'xtick',[])
  35. set(gca,'xticklabel',[])
  36. y_range=ylim(gca);
  37. %  重构坐标
  38. y_high=zeros(1,M)+min(y_range)-(range(y_range))/30;
  39. x_index=ceil((1:M)./M*N);
  40. str=date_cellstr(x_index);
  41. text(x_index,y_high,str,'rotation',-40,'HorizontalAlignment'...
  42.     ,'left','FontName','times','FontSize',10,'vertical','bottom');
  43. set(gca,'fontname','Times','fontsize',14)
  44. y_index=linspace(min(y_range),0,10)';
  45. hold on
  46. plot(repmat(x_index,length(y_index),1),...
  47.     repmat(y_index,1,length(x_index)),'b--','linewidth',1)
  48. clear all
复制代码


基于常方差弹性期权定价模型的比较静态分析(常方差弹性期权定价模型见Hull的《期权期货》):by fantuanxiaot

Gev_Option_Pricing_Plot2.jpg


Gev_Option_Pricing_Plot1.jpg


本帖隐藏的内容



  1. function Gev_Option_Pricing_Plot()
  2. %  copyright by fantuanxiaot
  3. %  基于常方差弹性欧式看涨期权定价模型的二维和三维比较静态分析
  4. %  S是期权的基期价格,K为行权价格,T是到期时间,O是波动参数
  5. %  R是年连续无风险利率,Q是年连续红利率,A是常方差弹性模型的alpha参数
  6. %  参数的设置
  7. S=10;
  8. K=12;
  9. T=1;
  10. O=0.16;
  11. R=0.04;
  12. Q=0.01;
  13. A=0.95;
  14. %% 基于常方差弹性欧式看涨期权定价模型的二维比较分析图
  15. %  subplot(2,2,1)
  16. figure(1)
  17. set(figure(1),'color','w')
  18. subplot(2,2,1)
  19. hold on
  20. k=11:0.2:13;
  21. s=9:0.5:10;
  22. for i=1:length(k)
  23.     c1(i)=Cev_Call_Option_Pricing(s(1),k(i),T,O,R,Q,A);
  24.     c2(i)=Cev_Call_Option_Pricing(s(2),k(i),T,O,R,Q,A);
  25.     c3(i)=Cev_Call_Option_Pricing(s(3),k(i),T,O,R,Q,A);
  26. end
  27. c=[c1;c2;c3];
  28. minc=min(min(c));
  29. maxc=max(max(c));
  30. xlim([k(1)-0.1 k(end)+0.1])
  31. ylim([minc-0.1 maxc+0.1])
  32. plot(k,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  33. hold on
  34. plot(k,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  35. plot(k,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  36. xlabel('执行价格','FontName','楷体','FontSize',16);
  37. ylabel('期权价格','FontName','楷体','FontSize',16);
  38. title('常方差弹性欧式看涨期权定价和执行价格的关系','FontName','楷体','FontSize',16);
  39. legend('基期价格=9','基期价格=9.5','基期价格=10',1)
  40. set(gca,'fontname','Times New Roman','FontSize',12)
  41. %  subplot(2,2,2)
  42. clear c1 c2 c3
  43. subplot(2,2,2)
  44. hold on
  45. alpha=0.8:0.02:0.96;
  46. s=9:0.5:10;
  47. for i=1:length(alpha)
  48.     c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,R,Q,alpha(i));
  49.     c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,R,Q,alpha(i));
  50.     c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,R,Q,alpha(i));
  51. end
  52. c=[c1;c2;c3];
  53. minc=min(min(c));
  54. maxc=max(max(c));
  55. xlim([alpha(1)-0.05 alpha(end)+0.05])
  56. ylim([minc-0.1 maxc+0.1])
  57. plot(alpha,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  58. hold on
  59. plot(alpha,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  60. plot(alpha,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  61. xlabel('常方差弹性模型参数阿尔法','FontName','楷体','FontSize',16);
  62. ylabel('期权价格','FontName','楷体','FontSize',16);
  63. title('常方差弹性欧式看涨期权定价和阿尔法的关系','FontName','楷体','FontSize',16);
  64. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  65. set(gca,'fontname','Times New Roman','FontSize',12)
  66. %  subplot(2,2,3)
  67. clear c1 c2 c3
  68. subplot(2,2,3)
  69. hold on
  70. t=0.5:0.1:1.5;
  71. for i=1:length(t)
  72.     c1(i)=Cev_Call_Option_Pricing(s(1),K,t(i),O,R,Q,A);
  73.     c2(i)=Cev_Call_Option_Pricing(s(2),K,t(i),O,R,Q,A);
  74.     c3(i)=Cev_Call_Option_Pricing(s(3),K,t(i),O,R,Q,A);
  75. end
  76. c=[c1;c2;c3];
  77. minc=min(min(c));
  78. maxc=max(max(c));
  79. xlim([t(1)-0.05 t(end)+0.05])
  80. ylim([minc-0.1 maxc+0.1])
  81. plot(t,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  82. hold on
  83. plot(t,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  84. plot(t,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  85. xlabel('到期时间','FontName','楷体','FontSize',16);
  86. ylabel('期权价格','FontName','楷体','FontSize',16);
  87. title('常方差弹性欧式看涨期权定价和到期时间的关系','FontName','楷体','FontSize',16);
  88. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  89. set(gca,'fontname','Times New Roman','FontSize',12)
  90. %  subplot(2,2,4)
  91. subplot(2,2,4)
  92. hold on
  93. clear c1 c2 c3
  94. r=0.03:0.005:0.06;
  95. for i=1:length(r)
  96.     c1(i)=Cev_Call_Option_Pricing(s(1),K,T,O,r(i),Q,A);
  97.     c2(i)=Cev_Call_Option_Pricing(s(2),K,T,O,r(i),Q,A);
  98.     c3(i)=Cev_Call_Option_Pricing(s(3),K,T,O,r(i),Q,A);
  99. end
  100. c=[c1;c2;c3];
  101. minc=min(min(c));
  102. maxc=max(max(c));
  103. xlim([r(1)-0.005 r(end)+0.005])
  104. ylim([minc-0.1 maxc+0.1])
  105. plot(r,c1,'ro-','markersize',12,'markerfacecolor','r','markeredgecolor','b')
  106. hold on
  107. plot(r,c2,'gs-','markersize',12,'markerfacecolor','g','markeredgecolor','b')
  108. plot(r,c3,'m<-','markersize',12,'markerfacecolor','m','markeredgecolor','b')
  109. xlabel('无风险利率','FontName','楷体','FontSize',16);
  110. ylabel('期权价格','FontName','楷体','FontSize',16);
  111. title('常方差弹性欧式看涨期权定价和无风险利率的关系','FontName','楷体','FontSize',16);
  112. legend('基期价格=9','基期价格=9.5','基期价格=10',2)
  113. set(gca,'fontname','Times New Roman','FontSize',12)
  114. hold off
  115. %% 基于常方差弹性欧式看涨期权定价模型的三维比较分析图
  116. clear c
  117. %  c是length(k)*length(alpha)阶的矩阵
  118. %  mesh_k和mesh_alpha是length(alpha)*length(k)阶的矩阵
  119. for i=1:length(k)
  120.     for j=1:length(alpha)
  121.         c(i,j)=Cev_Call_Option_Pricing(S,k(i),T,O,R,Q,alpha(j));
  122.     end
  123. end
  124. [mesh_k,mesh_alpha]=meshgrid(k,alpha);
  125. figure(2)
  126. set(figure(2),'color','w')
  127. subplot(1,2,1)
  128. h=meshz(mesh_alpha,mesh_k,c');
  129. hold on
  130. colormap('HSV')
  131. set(h,'linewidth',3,'edgelighting','phong')
  132. xlabel('阿尔法','FontName','楷体','FontSize',12);
  133. ylabel('执行价格','FontName','楷体','FontSize',12);
  134. zlabel('期权价值','FontName','楷体','FontSize',12);
  135. title('常方差弹性欧式看涨期权定价和执行价格与阿尔法的关系','FontName','楷体','FontSize',16);
  136. clear c
  137. subplot(1,2,2)
  138. for i=1:length(t)
  139.     for j=1:length(r)
  140.         c(i,j)=Cev_Call_Option_Pricing(S,K,t(i),O,r(j),Q,A);
  141.     end
  142. end
  143. [mesh_t,mesh_r]=meshgrid(t,r);
  144. h=meshz(mesh_t,mesh_r,c');
  145. hold on
  146. set(h,'linewidth',3,'edgelighting','phong')
  147. xlabel('到期时间','FontName','楷体','FontSize',12);
  148. ylabel('无风险利率','FontName','楷体','FontSize',12);
  149. zlabel('期权价值','FontName','楷体','FontSize',12);
  150. title('常方差弹性欧式看涨期权定价和无风险利率与到期时间的关系','FontName','楷体','FontSize',16);
  151. hold off
  152. end
  153. function C=Cev_Call_Option_Pricing(S,K,T,O,R,Q,A)
  154. %  copyright by fantuanxiaot
  155. %  模型具体请见赫尔的《期权期货及其他衍生品》
  156. %  常方差弹性欧式看涨期权定价模型
  157. %  而在这里0<alpha<1
  158. v=O^2/(2*(R-Q)*(A-1))*(exp(2*(R-Q)*(A-1)*T)-1);
  159. a=(K*exp((Q-R)*T))^(2*(1-A))/v/(1-A)^2;
  160. b=1/(1-A);
  161. c=S^(2*(1-A))/v/(1-A)^2;
  162. %  ncx2cdf代表非中心化的卡方分布cdf
  163. %  ncx2cdf第一参数代表自变量,第二个参数是自由度,第三个参数是非中心化参数
  164. d1=1-ncx2cdf(a,c,b+2);
  165. d2=ncx2cdf(c,a,b);
  166. %  这样就得到了基于常方差弹性模型的欧式看涨期权定价
  167. C=S*exp(-Q*T)*d1-K*exp(-R*T)*d2;
  168. end
复制代码










已有 10 人评分经验 威望 论坛币 学术水平 热心指数 信用等级 收起 理由
accumulation + 100 + 1 + 1 + 1 精彩帖子
kychan + 60 + 5 + 5 + 5 太精彩了!看了都知道是精华
孤独的散步者翱 + 100 + 1 + 1 精彩帖子
newfei188 + 1 精彩帖子
admin + 1 精彩帖子
zbin7451f + 100 + 5 + 5 + 5 对论坛有贡献
离歌レ笑 + 100 + 5 + 5 + 5 精彩帖子
chenyi112982 + 100 + 5 + 5 + 5 精彩帖子
我的素质低 + 100 + 5 + 5 + 5 精彩帖子
shrimpn + 3 精彩帖子

总评分: 经验 + 560  威望 + 1  论坛币 + 103  学术水平 + 28  热心指数 + 27  信用等级 + 26   查看全部评分

本帖被以下文库推荐

stata SPSS
ydb8848 发表于 2014-12-27 13:41:52 |显示全部楼层
关于啥的?,。。。。
回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:51:36 |显示全部楼层

一个神经网络Gui源代码分享

本帖最后由 fantuanxiaot 于 2014-12-27 13:55 编辑

一个MATLAB的BP神经网络Gui源代码分享,大家自己研究研究


  1. function varargout = bp_demo(varargin)
  2. % BP_DEMO M-file for bp_demo.fig
  3. %      BP_DEMO, by itself, creates a new BP_DEMO or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = BP_DEMO returns the handle to a new BP_DEMO or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      BP_DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in BP_DEMO.M with the given input arguments.
  11. %
  12. %      BP_DEMO('Property','Value',...) creates a new BP_DEMO or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before bp_demo_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to bp_demo_OpeningFcn via varargin.
  17. %
  18. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  19. %      instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES

  22. % Edit the above text to modify the response to help bp_demo

  23. % Last Modified by GUIDE v2.5 09-Jan-2010 19:18:03

  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name',       mfilename, ...
  27.                    'gui_Singleton',  gui_Singleton, ...
  28.                    'gui_OpeningFcn', @bp_demo_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @bp_demo_OutputFcn, ...
  30.                    'gui_LayoutFcn',  [] , ...
  31.                    'gui_Callback',   []);
  32. if nargin && ischar(varargin{1})
  33.     gui_State.gui_Callback = str2func(varargin{1});
  34. end

  35. if nargout
  36.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38.     gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT


  41. % --- Executes just before bp_demo is made visible.
  42. function bp_demo_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject    handle to figure
  45. % eventdata  reserved - to be defined in a future version of MATLAB
  46. % handles    structure with handles and user data (see GUIDATA)
  47. % varargin   command line arguments to bp_demo (see VARARGIN)

  48. % Choose default command line output for bp_demo
  49. handles.output = hObject;

  50. % Update handles structure
  51. guidata(hObject, handles);


  52. % UIWAIT makes bp_demo wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);


  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = bp_demo_OutputFcn(hObject, eventdata, handles)
  56. % varargout  cell array for returning output args (see VARARGOUT);
  57. % hObject    handle to figure
  58. % eventdata  reserved - to be defined in a future version of MATLAB
  59. % handles    structure with handles and user data (see GUIDATA)

  60. % Get default command line output from handles structure
  61. varargout{1} = handles.output;



  62. function t_sim_Callback(hObject, eventdata, handles)
  63. % hObject    handle to t_sim (see GCBO)
  64. % eventdata  reserved - to be defined in a future version of MATLAB
  65. % handles    structure with handles and user data (see GUIDATA)

  66. % Hints: get(hObject,'String') returns contents of t_sim as text
  67. %        str2double(get(hObject,'String')) returns contents of t_sim as a double


  68. % --- Executes during object creation, after setting all properties.
  69. function t_sim_CreateFcn(hObject, eventdata, handles)
  70. % hObject    handle to t_sim (see GCBO)
  71. % eventdata  reserved - to be defined in a future version of MATLAB
  72. % handles    empty - handles not created until after all CreateFcns called

  73. % Hint: edit controls usually have a white background on Windows.
  74. %       See ISPC and COMPUTER.
  75. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  76.     set(hObject,'BackgroundColor','white');
  77. end


  78. % --- Executes on button press in train_button.
  79. function train_button_Callback(hObject, eventdata, handles)
  80. % hObject    handle to train_button (see GCBO)
  81. % eventdata  reserved - to be defined in a future version of MATLAB
  82. % handles    structure with handles and user data (see GUIDATA)

  83. warning off
  84. global net P_train T_train N Epochs Show Goal Lr fcn_hidden fcn_output fcn_learn
  85. % default value
  86. N=10;
  87. Epochs=1000;
  88. Show=10;
  89. Goal=1e-3;
  90. Lr=0.01;
  91. fcn_hidden='tansig';
  92. fcn_output='purelin';
  93. fcn_learn='trainlm';
  94. % create neural network
  95. net=newff(minmax(P_train),[N size(T_train,1)],{fcn_hidden,fcn_output},fcn_learn);
  96. net.trainParam.epochs=Epochs;
  97. net.trainParam.show=Show;
  98. net.trainParam.goal=Goal;
  99. net.trainParam.lr=Lr;
  100. net.trainParam.showwindow=0;
  101. [net,tr]=train(net,P_train,T_train);
  102. % show waitbar
  103. perf=tr.perf;
  104. perf_t=abs(perf(2:end)-perf(1))/(perf(1)-perf(end));
  105. h=waitbar(0,'please wait.......');
  106. for i=1:length(perf_t)
  107.     waitbar(perf_t(i));
  108. end
  109. close(h);

  110. % --- Executes on button press in sim_button.
  111. function sim_button_Callback(hObject, eventdata, handles)
  112. % hObject    handle to sim_button (see GCBO)
  113. % eventdata  reserved - to be defined in a future version of MATLAB
  114. % handles    structure with handles and user data (see GUIDATA)

  115. global net P_test T_test mint maxt T_sim

  116. % simulation
  117. cla(handles.axes1);
  118. T_sim=sim(net,P_test);
  119. axes(handles.axes1);
  120. plot(1:size(T_test,2),T_test,'b-o');
  121. hold on;
  122. if get(handles.normalize_radiobutton,'value')==1
  123.     T_sim=postmnmx(T_sim,mint,maxt);
  124.     plot(1:size(T_sim,2),T_sim,'r-*');
  125.     legend(handles.axes1,'真实值','仿真值')
  126.     title('归一化')
  127. else
  128.     plot(1:size(T_sim,2),T_sim,'k-^');
  129.     legend(handles.axes1,'真实值','仿真值')
  130.     title('未归一化')
  131. end
  132. set(handles.t_sim,'enable','on','string',num2str(T_sim));
  133. grid on;


  134. % --- Executes on button press in save_button.
  135. function save_button_Callback(hObject, eventdata, handles)
  136. % hObject    handle to save_button (see GCBO)
  137. % eventdata  reserved - to be defined in a future version of MATLAB
  138. % handles    structure with handles and user data (see GUIDATA)

  139. global net
  140. [filename,pathname]=uiputfile('*.mat','save data as');
  141. file=strcat(pathname,filename);
  142. save(file,'net');



  143. % --- Executes on button press in close_button.
  144. function close_button_Callback(hObject, eventdata, handles)
  145. % hObject    handle to close_button (see GCBO)
  146. % eventdata  reserved - to be defined in a future version of MATLAB
  147. % handles    structure with handles and user data (see GUIDATA)

  148. close;

  149. function t_test_Callback(hObject, eventdata, handles)
  150. % hObject    handle to t_test (see GCBO)
  151. % eventdata  reserved - to be defined in a future version of MATLAB
  152. % handles    structure with handles and user data (see GUIDATA)

  153. % Hints: get(hObject,'String') returns contents of t_test as text
  154. %        str2double(get(hObject,'String')) returns contents of t_test as a double
  155. global T_test
  156. T_test=str2num(get(hObject,'string'));

  157. % --- Executes during object creation, after setting all properties.
  158. function t_test_CreateFcn(hObject, eventdata, handles)
  159. % hObject    handle to t_test (see GCBO)
  160. % eventdata  reserved - to be defined in a future version of MATLAB
  161. % handles    empty - handles not created until after all CreateFcns called

  162. % Hint: edit controls usually have a white background on Windows.
  163. %       See ISPC and COMPUTER.
  164. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  165.     set(hObject,'BackgroundColor','white');
  166. end



  167. function p_test_Callback(hObject, eventdata, handles)
  168. % hObject    handle to p_test (see GCBO)
  169. % eventdata  reserved - to be defined in a future version of MATLAB
  170. % handles    structure with handles and user data (see GUIDATA)

  171. % Hints: get(hObject,'String') returns contents of p_test as text
  172. %        str2double(get(hObject,'String')) returns contents of p_test as a double
  173. global P_test
  174. P_test=str2num(get(hObject,'string'));

  175. % --- Executes during object creation, after setting all properties.
  176. function p_test_CreateFcn(hObject, eventdata, handles)
  177. % hObject    handle to p_test (see GCBO)
  178. % eventdata  reserved - to be defined in a future version of MATLAB
  179. % handles    empty - handles not created until after all CreateFcns called

  180. % Hint: edit controls usually have a white background on Windows.
  181. %       See ISPC and COMPUTER.
  182. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  183.     set(hObject,'BackgroundColor','white');
  184. end



  185. function epochs_Callback(hObject, eventdata, handles)
  186. % hObject    handle to epochs (see GCBO)
  187. % eventdata  reserved - to be defined in a future version of MATLAB
  188. % handles    structure with handles and user data (see GUIDATA)

  189. % Hints: get(hObject,'String') returns contents of epochs as text
  190. %        str2double(get(hObject,'String')) returns contents of epochs as a double
  191. global Epochs
  192. epochs=get(handles.epochs,'string');
  193. Epochs=str2num(epochs);
  194. flag=~isequal(Epochs,round(Epochs));
  195. if Epochs<=0 || flag
  196.     warndlg('最大训练次数必须是正整数','Input Error');
  197. else
  198.     if Epochs<=5
  199.         warndlg('最大训练次数太小容易导致欠拟合问题','Input Warning');
  200.     end
  201. end


  202. % --- Executes during object creation, after setting all properties.
  203. function epochs_CreateFcn(hObject, eventdata, handles)
  204. % hObject    handle to epochs (see GCBO)
  205. % eventdata  reserved - to be defined in a future version of MATLAB
  206. % handles    empty - handles not created until after all CreateFcns called

  207. % Hint: edit controls usually have a white background on Windows.
  208. %       See ISPC and COMPUTER.
  209. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  210.     set(hObject,'BackgroundColor','white');
  211. end



  212. function show_Callback(hObject, eventdata, handles)
  213. % hObject    handle to show (see GCBO)
  214. % eventdata  reserved - to be defined in a future version of MATLAB
  215. % handles    structure with handles and user data (see GUIDATA)

  216. % Hints: get(hObject,'String') returns contents of show as text
  217. %        str2double(get(hObject,'String')) returns contents of show as a double
  218. global Show
  219. show=get(handles.show,'string');
  220. Show=str2num(show);
  221. flag=~isequal(Show,round(Show));
  222. if Show<=0 || flag
  223.     warndlg('最大训练次数必须是正整数','Input Error');
  224. end


  225. % --- Executes during object creation, after setting all properties.
  226. function show_CreateFcn(hObject, eventdata, handles)
  227. % hObject    handle to show (see GCBO)
  228. % eventdata  reserved - to be defined in a future version of MATLAB
  229. % handles    empty - handles not created until after all CreateFcns called

  230. % Hint: edit controls usually have a white background on Windows.
  231. %       See ISPC and COMPUTER.
  232. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  233.     set(hObject,'BackgroundColor','white');
  234. end



  235. function goal_Callback(hObject, eventdata, handles)
  236. % hObject    handle to goal (see GCBO)
  237. % eventdata  reserved - to be defined in a future version of MATLAB
  238. % handles    structure with handles and user data (see GUIDATA)

  239. % Hints: get(hObject,'String') returns contents of goal as text
  240. %        str2double(get(hObject,'String')) returns contents of goal as a double
  241. global Goal
  242. goal=get(handles.goal,'string');
  243. Goal=str2num(goal);
  244. if Goal <0
  245.     warndlg('训练目标必须是正数','Input Error');
  246. else
  247.     if Goal >1
  248.         warndlg('训练目标设置太大容易导致欠拟合问题','Input Warning');
  249.     elseif Goal <1e-10
  250.         warndlg('训练目标设置太小容易导致过拟合问题','Input Warning');
  251.     end
  252. end

  253. % --- Executes during object creation, after setting all properties.
  254. function goal_CreateFcn(hObject, eventdata, handles)
  255. % hObject    handle to goal (see GCBO)
  256. % eventdata  reserved - to be defined in a future version of MATLAB
  257. % handles    empty - handles not created until after all CreateFcns called

  258. % Hint: edit controls usually have a white background on Windows.
  259. %       See ISPC and COMPUTER.
  260. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  261.     set(hObject,'BackgroundColor','white');
  262. end



  263. function lr_Callback(hObject, eventdata, handles)
  264. % hObject    handle to lr (see GCBO)
  265. % eventdata  reserved - to be defined in a future version of MATLAB
  266. % handles    structure with handles and user data (see GUIDATA)

  267. % Hints: get(hObject,'String') returns contents of lr as text
  268. %        str2double(get(hObject,'String')) returns contents of lr as a double
  269. global Lr
  270. lr=get(handles.lr,'string');
  271. Lr=str2num(lr);
  272. if Lr<0 || Lr>1
  273.     warndlg('学习率建议设在0-1之间','Input Warning');
  274. end

  275. % --- Executes during object creation, after setting all properties.
  276. function lr_CreateFcn(hObject, eventdata, handles)
  277. % hObject    handle to lr (see GCBO)
  278. % eventdata  reserved - to be defined in a future version of MATLAB
  279. % handles    empty - handles not created until after all CreateFcns called

  280. % Hint: edit controls usually have a white background on Windows.
  281. %       See ISPC and COMPUTER.
  282. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  283.     set(hObject,'BackgroundColor','white');
  284. end



  285. function number_hidden_Callback(hObject, eventdata, handles)
  286. % hObject    handle to number_hidden (see GCBO)
  287. % eventdata  reserved - to be defined in a future version of MATLAB
  288. % handles    structure with handles and user data (see GUIDATA)

  289. % Hints: get(hObject,'String') returns contents of number_hidden as text
  290. %        str2double(get(hObject,'String')) returns contents of number_hidden as a double
  291. global N
  292. n=get(handles.number_hidden,'string');
  293. N=str2num(n);
  294. flag=~isequal(N,round(N));
  295. if N<=0 || flag
  296.     warndlg('隐含层神经元个数必须是正整数','Input Error');
  297. else
  298.     if N<=2
  299.         warndlg('隐含层神经元个数太小容易导致欠拟合问题','Input Warning');
  300.     elseif N>=50
  301.         warndlg('隐含层神经元个数太大容易导致过拟合问题','Input Warning');
  302.     end
  303. end

  304. % --- Executes during object creation, after setting all properties.
  305. function number_hidden_CreateFcn(hObject, eventdata, handles)
  306. % hObject    handle to number_hidden (see GCBO)
  307. % eventdata  reserved - to be defined in a future version of MATLAB
  308. % handles    empty - handles not created until after all CreateFcns called

  309. % Hint: edit controls usually have a white background on Windows.
  310. %       See ISPC and COMPUTER.
  311. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  312.     set(hObject,'BackgroundColor','white');
  313. end



  314. function fcn_hidden_Callback(hObject, eventdata, handles)
  315. % hObject    handle to fcn_hidden (see GCBO)
  316. % eventdata  reserved - to be defined in a future version of MATLAB
  317. % handles    structure with handles and user data (see GUIDATA)

  318. % Hints: get(hObject,'String') returns contents of fcn_hidden as text
  319. %        str2double(get(hObject,'String')) returns contents of fcn_hidden as a double
  320. global fcn_hidden
  321. h_hidden=get(handles.hiddenfcn_popupmenu,'string');
  322. fcn_hidden=h_hidden{get(handles.hiddenfcn_popupmenu,'value')};

  323. % --- Executes during object creation, after setting all properties.
  324. function fcn_hidden_CreateFcn(hObject, eventdata, handles)
  325. % hObject    handle to fcn_hidden (see GCBO)
  326. % eventdata  reserved - to be defined in a future version of MATLAB
  327. % handles    empty - handles not created until after all CreateFcns called

  328. % Hint: edit controls usually have a white background on Windows.
  329. %       See ISPC and COMPUTER.
  330. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  331.     set(hObject,'BackgroundColor','white');
  332. end



  333. function fcn_output_Callback(hObject, eventdata, handles)
  334. % hObject    handle to fcn_output (see GCBO)
  335. % eventdata  reserved - to be defined in a future version of MATLAB
  336. % handles    structure with handles and user data (see GUIDATA)

  337. % Hints: get(hObject,'String') returns contents of fcn_output as text
  338. %        str2double(get(hObject,'String')) returns contents of fcn_output as a double
  339. global fcn_output
  340. h_output=get(handles.outputfcn_popupmenu,'string');
  341. fcn_output=h_output{get(handles.outputfcn_popupmenu,'value')};

  342. % --- Executes during object creation, after setting all properties.
  343. function fcn_output_CreateFcn(hObject, eventdata, handles)
  344. % hObject    handle to fcn_output (see GCBO)
  345. % eventdata  reserved - to be defined in a future version of MATLAB
  346. % handles    empty - handles not created until after all CreateFcns called

  347. % Hint: edit controls usually have a white background on Windows.
  348. %       See ISPC and COMPUTER.
  349. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  350.     set(hObject,'BackgroundColor','white');
  351. end



  352. function fcn_learn_Callback(hObject, eventdata, handles)
  353. % hObject    handle to fcn_learn (see GCBO)
  354. % eventdata  reserved - to be defined in a future version of MATLAB
  355. % handles    structure with handles and user data (see GUIDATA)

  356. % Hints: get(hObject,'String') returns contents of fcn_learn as text
  357. %        str2double(get(hObject,'String')) returns contents of fcn_learn as a double
  358. global fcn_learn
  359. h_learn=get(handles.fcn_learn_popupmenu,'string');
  360. fcn_learn=h_learn{get(handles.fcn_learn_popupmenu,'value')};

  361. % --- Executes during object creation, after setting all properties.
  362. function fcn_learn_CreateFcn(hObject, eventdata, handles)
  363. % hObject    handle to fcn_learn (see GCBO)
  364. % eventdata  reserved - to be defined in a future version of MATLAB
  365. % handles    empty - handles not created until after all CreateFcns called

  366. % Hint: edit controls usually have a white background on Windows.
  367. %       See ISPC and COMPUTER.
  368. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  369.     set(hObject,'BackgroundColor','white');
  370. end



  371. function p_train_Callback(hObject, eventdata, handles)
  372. % hObject    handle to p_train (see GCBO)
  373. % eventdata  reserved - to be defined in a future version of MATLAB
  374. % handles    structure with handles and user data (see GUIDATA)

  375. % Hints: get(hObject,'String') returns contents of p_train as text
  376. %        str2double(get(hObject,'String')) returns contents of p_train as a double
  377. global P_train str
  378. P_train=str2num(get(hObject,'string'));
  379. str=P_train;

  380. % --- Executes during object creation, after setting all properties.
  381. function p_train_CreateFcn(hObject, eventdata, handles)
  382. % hObject    handle to p_train (see GCBO)
  383. % eventdata  reserved - to be defined in a future version of MATLAB
  384. % handles    empty - handles not created until after all CreateFcns called

  385. % Hint: edit controls usually have a white background on Windows.
  386. %       See ISPC and COMPUTER.
  387. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  388.     set(hObject,'BackgroundColor','white');
  389. end



  390. function t_train_Callback(hObject, eventdata, handles)
  391. % hObject    handle to t_train (see GCBO)
  392. % eventdata  reserved - to be defined in a future version of MATLAB
  393. % handles    structure with handles and user data (see GUIDATA)

  394. % Hints: get(hObject,'String') returns contents of t_train as text
  395. %        str2double(get(hObject,'String')) returns contents of t_train as a double
  396. global T_train
  397. T_train=str2num(get(hObject,'string'));

  398. % --- Executes during object creation, after setting all properties.
  399. function t_train_CreateFcn(hObject, eventdata, handles)
  400. % hObject    handle to t_train (see GCBO)
  401. % eventdata  reserved - to be defined in a future version of MATLAB
  402. % handles    empty - handles not created until after all CreateFcns called

  403. % Hint: edit controls usually have a white background on Windows.
  404. %       See ISPC and COMPUTER.
  405. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  406.     set(hObject,'BackgroundColor','white');
  407. end


  408. % --- Executes on button press in checkbox1.
  409. function checkbox1_Callback(hObject, eventdata, handles)
  410. % hObject    handle to checkbox1 (see GCBO)
  411. % eventdata  reserved - to be defined in a future version of MATLAB
  412. % handles    structure with handles and user data (see GUIDATA)

  413. % Hint: get(hObject,'Value') returns toggle state of checkbox1
  414. global P_train T_train P_test T_test a data
  415. if get(handles.checkbox1,'value')==1
  416.     [filename,pathname,filterindex]=uigetfile({'*.xls';'*.txt';'*.csv';'*.mat'},'select data-file');
  417.     str=[pathname filename];
  418.     switch filterindex
  419.         case 1
  420.             data=xlsread(str);
  421.         case 2
  422.             data=load(str);
  423.         case 3
  424.             data=csvread(str);
  425.         case 4
  426.             data=importdata(str);
  427.     end
  428.     if isstruct(data)
  429.         P_train=data.P_train;
  430.         T_train=data.T_train;
  431.         P_test=data.P_test;
  432.         T_test=data.T_test;
  433.         set(handles.p_train,'enable','off','string',[num2str(size(P_train,1)) '行' num2str(size(P_train,2)) '列']);
  434.         set(handles.t_train,'enable','off','string',[num2str(size(T_train,1)) '行' num2str(size(T_train,2)) '列']);
  435.         set(handles.p_test,'enable','off','string',[num2str(size(P_test,1)) '行' num2str(size(P_test,2)) '列']);
  436.         set(handles.t_test,'enable','off','string',[num2str(size(T_test,1)) '行' num2str(size(T_test,2)) '列']);
  437.         set(handles.normalize_radiobutton,'enable','off');
  438.         set(handles.nonormalize_radiobutton,'enable','off');
  439.         set(handles.slider1,'enable','off');
  440.         set(handles.train_button,'enable','off');
  441.         set(handles.number_hidden,'enable','off');
  442.         set(handles.hiddenfcn_popupmenu,'enable','off');
  443.         set(handles.outputfcn_popupmenu,'enable','off');
  444.         set(handles.fcn_learn_popupmenu,'enable','off');
  445.         set(handles.epochs,'enable','off');
  446.         set(handles.show,'enable','off');
  447.         set(handles.goal,'enable','off');
  448.         set(handles.lr,'enable','off');
  449.     else
  450.         a=randperm(size(data,2));
  451.         s_v=get(handles.slider1,'value');
  452.         P_train=data(1:10,a(1:round(size(data,2)*s_v)));
  453.         T_train=data(11,a(1:round(size(data,2)*s_v)));
  454.         P_test=data(1:10,a(round(size(data,2)*s_v)+1:end));
  455.         T_test=data(11,a(round(size(data,2)*s_v)+1:end));
  456.         set(handles.p_train,'enable','off','string',[num2str(size(P_train,1)) '行' num2str(size(P_train,2)) '列']);
  457.         set(handles.t_train,'enable','off','string',[num2str(size(T_train,1)) '行' num2str(size(T_train,2)) '列']);
  458.         set(handles.p_test,'enable','off','string',[num2str(size(P_test,1)) '行' num2str(size(P_test,2)) '列']);
  459.         set(handles.t_test,'enable','off','string',[num2str(size(T_test,1)) '行' num2str(size(T_test,2)) '列']);
  460.         set(handles.slider1,'enable','on');
  461.         set(handles.normalize_radiobutton,'enable','on');
  462.         set(handles.nonormalize_radiobutton,'enable','on');
  463.     end
  464. else
  465.     p_train=get(handles.p_train,'string');
  466.     P_train=str2num(p_train);
  467.     t_train=get(handles.t_train,'string');
  468.     T_train=str2num(t_train);
  469.     set(handles.slider1,'enable','off');
  470.     set(handles.p_train,'enable','on','string',' ');
  471.     set(handles.t_train,'enable','on','string',' ');
  472.     set(handles.p_test,'enable','on','string',' ');
  473.     set(handles.t_test,'enable','on','string',' ');
  474.     set(handles.t_sim,'enable','on','string',' ');
  475.     set(handles.TextClear_Menu,'enable','on');
  476.     set(handles.Text1Save_ConMenu,'enable','on');
  477.     set(handles.Text1Clear_ConMenu,'enable','on');
  478.     set(handles.normalize_radiobutton,'enable','on');
  479.     set(handles.nonormalize_radiobutton,'enable','on');
  480.     set(handles.train_button,'enable','on');
  481.     set(handles.number_hidden,'enable','on');
  482.     set(handles.hiddenfcn_popupmenu,'enable','on');
  483.     set(handles.outputfcn_popupmenu,'enable','on');
  484.     set(handles.fcn_learn_popupmenu,'enable','on');
  485.     set(handles.epochs,'enable','on');
  486.     set(handles.show,'enable','on');
  487.     set(handles.goal,'enable','on');
  488.     set(handles.lr,'enable','on');
  489. end


  490. % --- Executes on selection change in hiddenfcn_popupmenu.
  491. function hiddenfcn_popupmenu_Callback(hObject, eventdata, handles)
  492. % hObject    handle to hiddenfcn_popupmenu (see GCBO)
  493. % eventdata  reserved - to be defined in a future version of MATLAB
  494. % handles    structure with handles and user data (see GUIDATA)
  495. % global fcn_hidden
  496. % h_hidden=get(hObject,'string');
  497. % fcn_hidden=h_hidden{get(hObject,'value')};

  498. % Hints: contents = get(hObject,'String') returns hiddenfcn_popupmenu contents as cell array
  499. %        contents{get(hObject,'Value')} returns selected item from hiddenfcn_popupmenu


  500. % --- Executes during object creation, after setting all properties.
  501. function hiddenfcn_popupmenu_CreateFcn(hObject, eventdata, handles)
  502. % hObject    handle to hiddenfcn_popupmenu (see GCBO)
  503. % eventdata  reserved - to be defined in a future version of MATLAB
  504. % handles    empty - handles not created until after all CreateFcns called

  505. % Hint: popupmenu controls usually have a white background on Windows.
  506. %       See ISPC and COMPUTER.
  507. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  508.     set(hObject,'BackgroundColor','white');
  509. end


  510. % --- Executes on selection change in outputfcn_popupmenu.
  511. function outputfcn_popupmenu_Callback(hObject, eventdata, handles)
  512. % hObject    handle to outputfcn_popupmenu (see GCBO)
  513. % eventdata  reserved - to be defined in a future version of MATLAB
  514. % handles    structure with handles and user data (see GUIDATA)

  515. % Hints: contents = get(hObject,'String') returns outputfcn_popupmenu contents as cell array
  516. %        contents{get(hObject,'Value')} returns selected item from outputfcn_popupmenu

  517. % global fcn_output
  518. % h_output=get(hObject,'string');
  519. % fcn_output=h_output{get(hObject,'value')};

  520. % --- Executes during object creation, after setting all properties.
  521. function outputfcn_popupmenu_CreateFcn(hObject, eventdata, handles)
  522. % hObject    handle to outputfcn_popupmenu (see GCBO)
  523. % eventdata  reserved - to be defined in a future version of MATLAB
  524. % handles    empty - handles not created until after all CreateFcns called

  525. % Hint: popupmenu controls usually have a white background on Windows.
  526. %       See ISPC and COMPUTER.
  527. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  528.     set(hObject,'BackgroundColor','white');
  529. end


  530. % --- Executes on selection change in fcn_learn_popupmenu.
  531. function fcn_learn_popupmenu_Callback(hObject, eventdata, handles)
  532. % hObject    handle to fcn_learn_popupmenu (see GCBO)
  533. % eventdata  reserved - to be defined in a future version of MATLAB
  534. % handles    structure with handles and user data (see GUIDATA)

  535. % Hints: contents = get(hObject,'String') returns fcn_learn_popupmenu contents as cell array
  536. %        contents{get(hObject,'Value')} returns selected item from fcn_learn_popupmenu

  537. % global fcn_learn
  538. % h_learn=get(hObject,'string');
  539. % fcn_learn=h_learn{get(hObject,'value')};

  540. % --- Executes during object creation, after setting all properties.
  541. function fcn_learn_popupmenu_CreateFcn(hObject, eventdata, handles)
  542. % hObject    handle to fcn_learn_popupmenu (see GCBO)
  543. % eventdata  reserved - to be defined in a future version of MATLAB
  544. % handles    empty - handles not created until after all CreateFcns called

  545. % Hint: popupmenu controls usually have a white background on Windows.
  546. %       See ISPC and COMPUTER.
  547. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  548.     set(hObject,'BackgroundColor','white');
  549. end


  550. % --- Executes on slider movement.
  551. function slider1_Callback(hObject, eventdata, handles)
  552. % hObject    handle to slider1 (see GCBO)
  553. % eventdata  reserved - to be defined in a future version of MATLAB
  554. % handles    structure with handles and user data (see GUIDATA)

  555. % Hints: get(hObject,'Value') returns position of slider
  556. %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

  557. global data P_train T_train P_test T_test a
  558. slider_value=get(hObject,'value');
  559. if slider_value<=0.1
  560.     warndlg('警告:训练集数据太少,建议设置为全部数据的60%-80%','Training Set Warning');
  561. end
  562. P_train=data(1:10,a(1:round(size(data,2)*slider_value)));
  563. T_train=data(11,a(1:round(size(data,2)*slider_value)));
  564. P_test=data(1:10,a(round(size(data,2)*slider_value)+1:end));
  565. T_test=data(11,a(round(size(data,2)*slider_value)+1:end));
  566. set(handles.p_train,'string',[num2str(size(P_train,1)) '行' num2str(size(P_train,2)) '列'],'enable','off');
  567. set(handles.t_train,'string',[num2str(size(T_train,1)) '行' num2str(size(T_train,2)) '列'],'enable','off');
  568. set(handles.p_test,'string',[num2str(size(P_test,1)) '行' num2str(size(P_test,2)) '列'],'enable','off');
  569. set(handles.t_test,'string',[num2str(size(T_test,1)) '行' num2str(size(T_test,2)) '列'],'enable','off');


  570. % --- Executes during object creation, after setting all properties.
  571. function slider1_CreateFcn(hObject, eventdata, handles)
  572. % hObject    handle to slider1 (see GCBO)
  573. % eventdata  reserved - to be defined in a future version of MATLAB
  574. % handles    empty - handles not created until after all CreateFcns called

  575. % Hint: slider controls usually have a light gray background.
  576. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  577.     set(hObject,'BackgroundColor',[.9 .9 .9]);
  578. end


  579. % --- Executes on button press in normalize_radiobutton.
  580. function normalize_radiobutton_Callback(hObject, eventdata, handles)
  581. % hObject    handle to normalize_radiobutton (see GCBO)
  582. % eventdata  reserved - to be defined in a future version of MATLAB
  583. % handles    structure with handles and user data (see GUIDATA)

  584. % Hint: get(hObject,'Value') returns toggle state of normalize_radiobutton

  585. global P_train T_train P_test mint maxt data a

  586. if get(hObject,'value')==1
  587.     set(handles.nonormalize_radiobutton,'value',0);
  588.     [P_train,minp,maxp,T_train,mint,maxt]=premnmx(P_train,T_train);
  589.     P_test=tramnmx(P_test,minp,maxp);
  590. else
  591.     set(handles.nonormalize_radiobutton,'value',1);
  592.     if get(handles.checkbox1,'value')==1
  593.         slider_value=get(handles.slider1,'value');
  594.         P_train=data(1:10,a(1:round(size(data,2)*slider_value)));
  595.         T_train=data(11,a(1:round(size(data,2)*slider_value)));
  596.         P_test=data(1:10,a(round(size(data,2)*slider_value)+1:end));
  597.     else
  598.        P_train=str2num(get(handles.p_train,'string'));
  599.        T_train=str2num(get(handles.t_train,'string'));
  600.        P_test=str2num(get(handles.p_test,'string'));
  601.     end
  602. end


  603. % --- Executes on button press in nonormalize_radiobutton.
  604. function nonormalize_radiobutton_Callback(hObject, eventdata, handles)
  605. % hObject    handle to nonormalize_radiobutton (see GCBO)
  606. % eventdata  reserved - to be defined in a future version of MATLAB
  607. % handles    structure with handles and user data (see GUIDATA)

  608. % Hint: get(hObject,'Value') returns toggle state of nonormalize_radiobutton

  609. global P_train T_train P_test mint maxt data a
  610. if get(hObject,'value')==1
  611.     set(handles.normalize_radiobutton,'value',0);
  612.     if get(handles.checkbox1,'value')==1
  613.         slider_value=get(handles.slider1,'value');
  614.         P_train=data(1:10,a(1:round(size(data,2)*slider_value)));
  615.         T_train=data(11,a(1:round(size(data,2)*slider_value)));
  616.         P_test=data(1:10,a(round(size(data,2)*slider_value)+1:end));
  617.     else
  618.        P_train=str2num(get(handles.p_train,'string'));
  619.        T_train=str2num(get(handles.t_train,'string'));
  620.        P_test=str2num(get(handles.p_test,'string'));
  621.     end
  622. else
  623.     set(handles.normalize_radiobutton,'value',1);
  624.     [P_train,minp,maxp,T_train,mint,maxt]=premnmx(P_train,T_train);
  625.     P_test=tramnmx(P_test,minp,maxp);
  626. end


  627. % --------------------------------------------------------------------
  628. function File_Menu_Callback(hObject, eventdata, handles)
  629. % hObject    handle to File_Menu (see GCBO)
  630. % eventdata  reserved - to be defined in a future version of MATLAB
  631. % handles    structure with handles and user data (see GUIDATA)


  632. % --------------------------------------------------------------------
  633. function Edit_Menu_Callback(hObject, eventdata, handles)
  634. % hObject    handle to Edit_Menu (see GCBO)
  635. % eventdata  reserved - to be defined in a future version of MATLAB
  636. % handles    structure with handles and user data (see GUIDATA)

  637. if get(handles.checkbox1,'value')==1
  638.     set(handles.TextClear_Menu,'enable','off');
  639. end


  640. % --------------------------------------------------------------------
  641. function Untitled_9_Callback(hObject, eventdata, handles)
  642. % hObject    handle to Untitled_9 (see GCBO)
  643. % eventdata  reserved - to be defined in a future version of MATLAB
  644. % handles    structure with handles and user data (see GUIDATA)


  645. % --------------------------------------------------------------------
  646. function Help_Callback(hObject, eventdata, handles)
  647. % hObject    handle to Help (see GCBO)
  648. % eventdata  reserved - to be defined in a future version of MATLAB
  649. % handles    structure with handles and user data (see GUIDATA)

  650. open('readme.txt');
  651. % --------------------------------------------------------------------
  652. function About_Menu_Callback(hObject, eventdata, handles)
  653. % hObject    handle to About_Menu (see GCBO)
  654. % eventdata  reserved - to be defined in a future version of MATLAB
  655. % handles    structure with handles and user data (see GUIDATA)

  656. About;
  657. % --------------------------------------------------------------------
  658. function FigureCopy_Menu_Callback(hObject, eventdata, handles)
  659. % hObject    handle to FigureCopy_Menu (see GCBO)
  660. % eventdata  reserved - to be defined in a future version of MATLAB
  661. % handles    structure with handles and user data (see GUIDATA)
  662. new_f_handle=figure('visible','off');
  663. new_axes=copyobj(handles.axes1,new_f_handle);
  664. set(new_axes,'units','default','position','default');
  665. [filename,pathname]=uiputfile('*.jpg','save picture as');
  666. file=strcat(pathname,filename);
  667. print(new_f_handle,'-djpeg',file);
  668. delete(new_f_handle);

  669. % --------------------------------------------------------------------
  670. function DataImport_Menu_Callback(hObject, eventdata, handles)
  671. % hObject    handle to DataImport_Menu (see GCBO)
  672. % eventdata  reserved - to be defined in a future version of MATLAB
  673. % handles    structure with handles and user data (see GUIDATA)
  674. global P_train T_train P_test T_test a data
  675. set(handles.checkbox1,'value',1);
  676. [filename,pathname,filterindex]=uigetfile({'*.xls';'*.txt';'*.csv';'*.mat'},'select data-file');
  677. str=[pathname filename];
  678. switch filterindex
  679.     case 1
  680.         data=xlsread(str);
  681.     case 2
  682.         data=load(str);
  683.     case 3
  684.         data=csvread(str);
  685.     case 4
  686.         data=importdata(str);
  687. end
  688. if isstruct(data)
  689.     P_train=data.P_train;
  690.     T_train=data.T_train;
  691.     P_test=data.P_test;
  692.     T_test=data.T_test;
  693.     set(handles.p_train,'enable','off','string',[num2str(size(P_train,1)) '行' num2str(size(P_train,2)) '列']);
  694.         set(handles.t_train,'enable','off','string',[num2str(size(T_train,1)) '行' num2str(size(T_train,2)) '列']);
  695.         set(handles.p_test,'enable','off','string',[num2str(size(P_test,1)) '行' num2str(size(P_test,2)) '列']);
  696.         set(handles.t_test,'enable','off','string',[num2str(size(T_test,1)) '行' num2str(size(T_test,2)) '列']);
  697.     set(handles.normalize_radiobutton,'enable','off');
  698.     set(handles.nonormalize_radiobutton,'enable','off');
  699.     set(handles.slider1,'enable','off');
  700.     set(handles.train_button,'enable','off');
  701.     set(handles.number_hidden,'enable','off');
  702.     set(handles.hiddenfcn_popupmenu,'enable','off');
  703.     set(handles.outputfcn_popupmenu,'enable','off');
  704.     set(handles.fcn_learn_popupmenu,'enable','off');
  705.     set(handles.epochs,'enable','off');
  706.     set(handles.show,'enable','off');
  707.     set(handles.goal,'enable','off');
  708.     set(handles.lr,'enable','off');
  709. else
  710.     a=randperm(size(data,2));
  711.     s_v=get(handles.slider1,'value');
  712.     P_train=data(1:10,a(1:round(size(data,2)*s_v)));
  713.     T_train=data(11,a(1:round(size(data,2)*s_v)));
  714.     P_test=data(1:10,a(round(size(data,2)*s_v)+1:end));
  715.     T_test=data(11,a(round(size(data,2)*s_v)+1:end));
  716.     set(handles.p_train,'enable','off','string',[num2str(size(P_train,1)) '行' num2str(size(P_train,2)) '列']);
  717.     set(handles.t_train,'enable','off','string',[num2str(size(T_train,1)) '行' num2str(size(T_train,2)) '列']);
  718.     set(handles.p_test,'enable','off','string',[num2str(size(P_test,1)) '行' num2str(size(P_test,2)) '列']);
  719.     set(handles.t_test,'enable','off','string',[num2str(size(T_test,1)) '行' num2str(size(T_test,2)) '列']);
  720.     set(handles.slider1,'enable','on');
  721.     set(handles.normalize_radiobutton,'enable','on');
  722.     set(handles.nonormalize_radiobutton,'enable','on');
  723. end



  724. % --------------------------------------------------------------------
  725. function DataExport_Menu_Callback(hObject, eventdata, handles)
  726. % hObject    handle to DataExport_Menu (see GCBO)
  727. % eventdata  reserved - to be defined in a future version of MATLAB
  728. % handles    structure with handles and user data (see GUIDATA)
  729. global P_train T_train P_test T_test T_sim
  730. [filename,pathname]=uiputfile('*.mat','save data as');
  731. file=strcat(pathname,filename);
  732. save(file,'P_train','T_train','P_test','T_test','T_sim');

  733. % --------------------------------------------------------------------
  734. function Exit_Menu_Callback(hObject, eventdata, handles)
  735. % hObject    handle to Exit_Menu (see GCBO)
  736. % eventdata  reserved - to be defined in a future version of MATLAB
  737. % handles    structure with handles and user data (see GUIDATA)
  738. close;


  739. % --------------------------------------------------------------------
  740. function TextClear_Menu_Callback(hObject, eventdata, handles)
  741. % hObject    handle to TextClear_Menu (see GCBO)
  742. % eventdata  reserved - to be defined in a future version of MATLAB
  743. % handles    structure with handles and user data (see GUIDATA)
  744. set(handles.p_train,'string',' ');
  745. set(handles.t_train,'string',' ');
  746. set(handles.p_test,'string',' ');
  747. set(handles.t_test,'string',' ');
  748. set(handles.t_sim,'string',' ');

  749. % --------------------------------------------------------------------
  750. function FigureClear_Menu_Callback(hObject, eventdata, handles)
  751. % hObject    handle to FigureClear_Menu (see GCBO)
  752. % eventdata  reserved - to be defined in a future version of MATLAB
  753. % handles    structure with handles and user data (see GUIDATA)
  754. cla(handles.axes1);

  755. % --------------------------------------------------------------------
  756. function NetImport_Menu_Callback(hObject, eventdata, handles)
  757. % hObject    handle to NetImport_Menu (see GCBO)
  758. % eventdata  reserved - to be defined in a future version of MATLAB
  759. % handles    structure with handles and user data (see GUIDATA)
  760. global net
  761. [filename,pathname,filterindex]=uigetfile('*.mat','select net-file');
  762. str=[pathname filename];
  763. net=importdata(str);

  764. % --------------------------------------------------------------------
  765. function NetworkExport_Menu_Callback(hObject, eventdata, handles)
  766. % hObject    handle to NetworkExport_Menu (see GCBO)
  767. % eventdata  reserved - to be defined in a future version of MATLAB
  768. % handles    structure with handles and user data (see GUIDATA)
  769. global net
  770. [filename,pathname]=uiputfile('*.mat','save data as');
  771. file=strcat(pathname,filename);
  772. save(file,'net');


  773. % --------------------------------------------------------------------
  774. function FigureCopy_ConMenu_Callback(hObject, eventdata, handles)
  775. % hObject    handle to FigureCopy_ConMenu (see GCBO)
  776. % eventdata  reserved - to be defined in a future version of MATLAB
  777. % handles    structure with handles and user data (see GUIDATA)
  778. new_f_handle=figure('visible','off');
  779. new_axes=copyobj(handles.axes1,new_f_handle);
  780. set(new_axes,'units','default','position','default');
  781. [filename,pathname]=uiputfile('*.jpg','save picture as');
  782. file=strcat(pathname,filename);
  783. print(new_f_handle,'-djpeg',file);
  784. delete(new_f_handle);

  785. % --------------------------------------------------------------------
  786. function FigureClear_ConMenu_Callback(hObject, eventdata, handles)
  787. % hObject    handle to FigureClear_ConMenu (see GCBO)
  788. % eventdata  reserved - to be defined in a future version of MATLAB
  789. % handles    structure with handles and user data (see GUIDATA)
  790. cla(handles.axes1);

  791. % --------------------------------------------------------------------
  792. function Text1Save_ConMenu_Callback(hObject, eventdata, handles)
  793. % hObject    handle to Text1Save_ConMenu (see GCBO)
  794. % eventdata  reserved - to be defined in a future version of MATLAB
  795. % handles    structure with handles and user data (see GUIDATA)
  796. [filename,pathname]=uiputfile('*.mat','save data as');
  797. file=strcat(pathname,filename);
  798. p_train=str2num(get(handles.p_train,'string'));
  799. save(file,'p_train');

  800. % --------------------------------------------------------------------
  801. function Text1Clear_ConMenu_Callback(hObject, eventdata, handles)
  802. % hObject    handle to Text1Clear_ConMenu (see GCBO)
  803. % eventdata  reserved - to be defined in a future version of MATLAB
  804. % handles    structure with handles and user data (see GUIDATA)
  805. set(handles.p_train,'string',' ');

  806. % --------------------------------------------------------------------
  807. function FigureCopy_ConuMenu_Callback(hObject, eventdata, handles)
  808. % hObject    handle to FigureCopy_ConMenu (see GCBO)
  809. % eventdata  reserved - to be defined in a future version of MATLAB
  810. % handles    structure with handles and user data (see GUIDATA)


  811. % --------------------------------------------------------------------
  812. function Figure_ConMenu_Callback(hObject, eventdata, handles)
  813. % hObject    handle to Figure_ConMenu (see GCBO)
  814. % eventdata  reserved - to be defined in a future version of MATLAB
  815. % handles    structure with handles and user data (see GUIDATA)


  816. % --------------------------------------------------------------------
  817. function Text1_ConMenu_Callback(hObject, eventdata, handles)
  818. % hObject    handle to Text1_ConMenu (see GCBO)
  819. % eventdata  reserved - to be defined in a future version of MATLAB
  820. % handles    structure with handles and user data (see GUIDATA)
  821. if get(handles.checkbox1,'value')==1
  822.     set(handles.Text1Save_ConMenu,'enable','off');
  823.     set(handles.Text1Clear_ConMenu,'enable','off');
  824. end


  825. % --------------------------------------------------------------------
  826. function Text5Save_ConMenu_Callback(hObject, eventdata, handles)
  827. % hObject    handle to Text5Save_ConMenu (see GCBO)
  828. % eventdata  reserved - to be defined in a future version of MATLAB
  829. % handles    structure with handles and user data (see GUIDATA)
  830. [filename,pathname]=uiputfile('*.mat','save data as');
  831. file=strcat(pathname,filename);
  832. t_sim=str2num(get(handles.t_sim,'string'));
  833. save(file,'t_sim');

  834. % --------------------------------------------------------------------
  835. function Text5Clear_ConMenu_Callback(hObject, eventdata, handles)
  836. % hObject    handle to Text5Clear_ConMenu (see GCBO)
  837. % eventdata  reserved - to be defined in a future version of MATLAB
  838. % handles    structure with handles and user data (see GUIDATA)
  839. set(handles.t_sim,'string',' ');

  840. % --------------------------------------------------------------------
  841. function Text4Save_ConMenu_Callback(hObject, eventdata, handles)
  842. % hObject    handle to Text4Save_ConMenu (see GCBO)
  843. % eventdata  reserved - to be defined in a future version of MATLAB
  844. % handles    structure with handles and user data (see GUIDATA)
  845. [filename,pathname]=uiputfile('*.mat','save data as');
  846. file=strcat(pathname,filename);
  847. t_test=str2num(get(handles.t_test,'string'));
  848. save(file,'t_test');

  849. % --------------------------------------------------------------------
  850. function Text4Clear_ConMenu_Callback(hObject, eventdata, handles)
  851. % hObject    handle to Text4Clear_ConMenu (see GCBO)
  852. % eventdata  reserved - to be defined in a future version of MATLAB
  853. % handles    structure with handles and user data (see GUIDATA)
  854. set(handles.t_test,'string',' ');

  855. % --------------------------------------------------------------------
  856. function Text3Save_ConMenu_Callback(hObject, eventdata, handles)
  857. % hObject    handle to Text3Save_ConMenu (see GCBO)
  858. % eventdata  reserved - to be defined in a future version of MATLAB
  859. % handles    structure with handles and user data (see GUIDATA)
  860. [filename,pathname]=uiputfile('*.mat','save data as');
  861. file=strcat(pathname,filename);
  862. p_test=str2num(get(handles.p_test,'string'));
  863. save(file,'p_test');

  864. % --------------------------------------------------------------------
  865. function Text3Clear_ConMenu_Callback(hObject, eventdata, handles)
  866. % hObject    handle to Text3Clear_ConMenu (see GCBO)
  867. % eventdata  reserved - to be defined in a future version of MATLAB
  868. % handles    structure with handles and user data (see GUIDATA)
  869. set(handles.p_test,'string',' ')

  870. % --------------------------------------------------------------------
  871. function Text2Save_ConMenu_Callback(hObject, eventdata, handles)
  872. % hObject    handle to Text2Save_ConMenu (see GCBO)
  873. % eventdata  reserved - to be defined in a future version of MATLAB
  874. % handles    structure with handles and user data (see GUIDATA)
  875. [filename,pathname]=uiputfile('*.mat','save data as');
  876. file=strcat(pathname,filename);
  877. t_train=str2num(get(handles.t_train,'string'));
  878. save(file,'t_train');

  879. % --------------------------------------------------------------------
  880. function Text2Clear_ConMenu_Callback(hObject, eventdata, handles)
  881. % hObject    handle to Text2Clear_ConMenu (see GCBO)
  882. % eventdata  reserved - to be defined in a future version of MATLAB
  883. % handles    structure with handles and user data (see GUIDATA)
  884. set(handles.t_train,'string',' ');

  885. % --------------------------------------------------------------------
  886. function Text2_ConMenu_Callback(hObject, eventdata, handles)
  887. % hObject    handle to Text2_ConMenu (see GCBO)
  888. % eventdata  reserved - to be defined in a future version of MATLAB
  889. % handles    structure with handles and user data (see GUIDATA)
  890. if get(handles.checkbox1,'value')==1
  891.     set(handles.Text2Save_ConMenu,'enable','off');
  892.     set(handles.Text2Clear_ConMenu,'enable','off');
  893. end

  894. % --------------------------------------------------------------------
  895. function Text3_ConMenu_Callback(hObject, eventdata, handles)
  896. % hObject    handle to Text3_ConMenu (see GCBO)
  897. % eventdata  reserved - to be defined in a future version of MATLAB
  898. % handles    structure with handles and user data (see GUIDATA)
  899. if get(handles.checkbox1,'value')==1
  900.     set(handles.Text3Save_ConMenu,'enable','off');
  901.     set(handles.Text3Clear_ConMenu,'enable','off');
  902. end

  903. % --------------------------------------------------------------------
  904. function Text4_ConMenu_Callback(hObject, eventdata, handles)
  905. % hObject    handle to Text4_ConMenu (see GCBO)
  906. % eventdata  reserved - to be defined in a future version of MATLAB
  907. % handles    structure with handles and user data (see GUIDATA)
  908. if get(handles.checkbox1,'value')==1
  909.     set(handles.Text4Save_ConMenu,'enable','off');
  910.     set(handles.Text4Clear_ConMenu,'enable','off');
  911. end

  912. % --------------------------------------------------------------------
  913. function Text5_ConMenu_Callback(hObject, eventdata, handles)
  914. % hObject    handle to Text5_ConMenu (see GCBO)
  915. % eventdata  reserved - to be defined in a future version of MATLAB
  916. % handles    structure with handles and user data (see GUIDATA)
复制代码




已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:52:06 |显示全部楼层

一个改编的MATLAB线性规划的例子:by fantuanxiaot

本帖最后由 fantuanxiaot 于 2014-12-27 14:17 编辑

一个改编的MATLAB线性规划的例子:by fantuanxiaot
  1. clc;clear
  2. jsj=[25.7 25.6 28.0 26.0 24.7 25.6 27.0 22.9 25.9 24.5];
  3. jg=[26.4 27.3 24.8 27.5 23.5 23.5 25.7 32.9 31.5 35.3];
  4. sx=[21.0 20.2 24.4 19.6 18.6 23.3 13.1 18.4 22.5 25.7];
  5. yy=[34.4 18.7 33.0 20.6 27.9 21.4 11.4 31.3 23.5 32.3];
  6. lk=[14.7 18.8 26.6 16.7 13.4 14.8 17.5 23.2];
  7. jn=[22.5 32.3 20.8 21.6 23.0 35.4];
  8. hg=[20.0 23.6 25.3 28.0 18.9 25.7];
  9. dl=[21.5 32.4 24.1 23.8 18.2 22.7];
  10. hj=[37.5 22.2 20.7 22.5 24.3 32.2];
  11. %各课程的书号个数百分比
  12. w1=[0.159090909 0.161931818 0.039772727 0.048295455 0.048295455 0.153409091 0.122159091 0.048295455 0.147727273 0.071022727];
  13. w2=[0.135678392 0.08040201 0.090452261 0.070351759 0.090452261 0.090452261 0.095477387 0.16080402 0.095477387 0.090452261];
  14. w3=[0.044354839 0.173387097 0.239247312 0.036290323 0.120967742 0.143817204 0.059139785 0.024193548 0.102150538 0.056451613];
  15. w4=[0.355971897 0.044496487 0.016393443 0.18969555 0.06088993 0.121779859 0.049180328 0.044496487 0.072599532 0.044496487];
  16. w5=[0.065789474 0.149122807 0.105263158 0.157894737 0.074561404 0.140350877 0.157894737 0.149122807];
  17. w6=[0.296482412 0.060301508 0.110552764 0.27638191 0.100502513 0.155778894];
  18. w7=[0.076190476 0.152380952 0.2 0.123809524 0.123809524 0.323809524];
  19. w8=[0.175 0.183333333 0.191666667 0.216666667 0.116666667 0.116666667];
  20. w9=[0.174603175 0.238095238 0.222222222 0.214285714 0.087301587 0.063492063];
  21. c1=[249.4166666 363.8375 118.3333333 238.4375 162.4166667 276.5818181 503.5486111 83.08333333 387.975 414.9375];
  22. c2=[1425.5 1875.75 386.6666666 1106.208333 286.4166667 544.9166667 812.9375 575.2916666 2589.9375 1356.0625];
  23. c3=[746.5416667 847.3785714 8609.903374 1297.5 1462.357843 2617.573864 628.4214286 498.8125 465.6760417 2863.553572];
  24. c4=[747.8024039 525.45 907.9166667 268.5805555 447.8583334 274.1125 59.91666667 632.3875 347.4464286 301.8333333];
  25. c5=[5297.5625 7532.660714 8622.2 4606.104167 16071.85416 7891.25 4104.633929 1241.875];
  26. c6=[681.3461539 266.875 480.25 816.175 1204.683334 1000.75];
  27. c7=[1038.75 863.95 406.975 162.9166667 782.0833333 427.4583333];
  28. c8=[226.6666667 644.875 600.125 810.5 295.1666667 1543.666666];
  29. c9=[593.1875 476.7916667 629.1666666 414.25 718.625 1460.75];
  30. m=0.7;vlb=[70 35 145 78 40 38 17 21 21];vub=[78 44 155 90 50 42 25 27 27];
  31. my=[3.25 3.24 3.24 3.24 3.2 3.2 3.3 3.27 3.2];
  32. pf=0.2*sin(pi/4*(my-2));
  33. f=m*[sum(jsj.*w1.*c1) sum(jg.*w2.*c2) sum(sx.*w3.*c3) sum(yy.*w4.*c4) sum(lk.*w5.*c5)...
  34.      sum(jn.*w6.*c6) sum(hg.*w7.*c7) sum(dl.*w8.*c8) sum(hj.*w9.*c9)]+(1-m)*...
  35.      [pf(1)*sum(jsj.*w1.*c1) pf(2)*sum(jg.*w2.*c2) pf(3)*sum(sx.*w3.*c3) pf(4)*sum(yy.*w4.*c4)...
  36.       pf(5)*sum(lk.*w5.*c5) pf(6)*sum(jn.*w6.*c6) pf(7)*sum(hg.*w7.*c7) pf(8)*sum(dl.*w8.*c8) pf(9)*sum(hj.*w9.*c9)];
  37. Aeq=[ones(1,9);0 -1 0 0 1 0 0 0 0;0 0 0 0 0 0 0 -1 1;0 0 0 0 0 0 -1 1 0];Beq=[500;0;0;0];
  38. A=[0 0 -1 1 0 0 0 0 0;1 0 0 -1 0 0 0 0 0;-1 0 0 0 1 0 0 0 0;0 -1 0 0 0 0 0 0 1];B=-0.01*ones(4,1);
  39. [x,fval,exitflag,out]=linprog(f,A,B,Aeq,Beq,vlb,vub)
复制代码


另外一个例子:by fantuanxiaot

  1. option1=optimset('MaxIter',1000,'TolX',1e-10,'TolFun',1e-10,'LargeScale','on','TolCon',1e-8);
  2. c1=[-2 -3 5]';AEQ1=[1 1 1];BEQ1=7;
  3. A1=[-2 5 -1];
  4. b1=-10;x01=[2 2 2]';
  5. [beta1,fval1,exit1,out1]=linprog(c1,A1,b1,AEQ1,BEQ1,zeros(3,1),[],x01,option1);
  6. option2=optimset('MaxIter',1000,'TolX',1e-8,'TolFun',1e-8,'LargeScale','on','TolCon',1e-8);
  7. AEQ2=[0 0 0 0 0 1];BEQ2=1;
  8. c2=[-170 17 -41 -2 -131 500000];
  9. A2=[1 -0.17 -0.53 0 1 0;0 0.17 0.5 0 0 0;1 0.32 1 0 0 0;0 1 0 0 0 0;0 0 1 1 0 0;0 0 0 -1 -1 0];
  10. b2=[0 888110 166800 521265 683400 -660000]';
  11. x02=[100 100 100 100 100 100]';
  12. [beta2,fval2,exit2,out2]=linprog(c2,A2,b2,AEQ2,BEQ2,zeros(6,1),[],x01,option2)
复制代码




已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 对论坛有贡献

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:52:38 |显示全部楼层

一个自己原来编写的灰色预测函数:by fantuanxiaot

本帖最后由 fantuanxiaot 于 2014-12-27 14:41 编辑

主程序:[a,b]=GM2(rand(1,10),2),a得到的是GM的参数,b得到的是包括10个拟合值和2个预测值
第一行为一次预测,第二行为二次预测
    主程序的结果:by fantuanxiaot
  1. [a,b]=GM2(rand(1,10),2)
  2. a =
  3.     0.0428    0.8083

  4. b =
  5.   Columns 1 through 8
  6.     0.9692    0.7507    0.7192    0.6891    0.6602    0.6326    0.6061    0.5807
  7.     0.9692    0.7915    0.7044    0.6749    0.6466    0.6195    0.5936    0.5687
  8.   Columns 9 through 12
  9.     0.5564    0.5331    0.5108    0.4894
  10.     0.5449    0.5221    0.5002    0.4793
复制代码
GM灰色预测函数:by fantuanxiaot
  1. function [pa,gmcal]=GM2(x,forecast)
  2. sizexd2=size(x,2);k=0;%********************一次拟合
  3. x1=cumsum(x);
  4. yn1=x;yn1(1)=[];
  5. for i=1:sizexd2-1
  6.     z1(i)=-0.5*(x1(i+1)+x1(i));
  7. end
  8. sizez1=size(z1,2);B=[z1' ones(sizez1,1)];au0=inv(B'*B)*B'*yn1';au=au0';afor=au(1);ufor=au(2);ua=ufor/afor;constant1=x(1)-ua;afor1=-afor;
  9. %****************************************************************二次拟合
  10. k2=0;
  11. for y2=x1
  12.     k2=k2+1;
  13.     if k2>sizexd2
  14.     else
  15.         ze1(k2)=exp(afor1*(k2-1));
  16.     end
  17. end
  18. sizeze1=size(ze1,2);G=[ze1' ones(sizeze1,1)];au20=inv(G'*G)*G'*x1';au2=au20';Aval=au2(1);Bval=au2(2);nfinal=sizexd2;
  19. %****************************************************************一次预计
  20. for k3=1:nfinal+forecast-1%预测多了一个数字,是k+1个数字
  21.     x3fcast(k3)=constant1*exp(afor1*k3)+ua;
  22. end
  23. for k31=nfinal+forecast-1:-1:0
  24.     if k31>1
  25.         x31fcast(k31+1)=x3fcast(k31)-x3fcast(k31-1);
  26.     else
  27.         if k31>0
  28.             x31fcast(k31+1)=x3fcast(k31)-x(1);
  29.         else
  30.             x31fcast(k31+1)=x(1);
  31.         end
  32.     end
  33. end
  34. for k4=1:nfinal+forecast-1%预测多了一个数字,是k+1个数字
  35.     x4fcast(k4)=Aval*exp(afor1*k4)+Bval;
  36. end
  37. for k41=nfinal+forecast-1:-1:0
  38.     if k41>1
  39.         x41fcast(k41+1)=x4fcast(k41)-x4fcast(k41-1);
  40.     else
  41.         if k41==1
  42.             x41fcast(k41+1)=x4fcast(k41)-x(1);
  43.         else
  44.             x41fcast(k41+1)=x(1);
  45.         end
  46.     end
  47. end
  48. gmcal(1,:)=x31fcast;
  49. gmcal(2,:)=x41fcast;
  50. pa(1)=afor;pa(2)=ufor;
复制代码









已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:54:08 |显示全部楼层

我的原来的一些资源帖子

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:54:23 |显示全部楼层

一个基于最速下降法的代码:by fantuanxiaot

本帖最后由 fantuanxiaot 于 2014-12-28 17:45 编辑

一个基于最速下降法的代码例子:by fantuanxiaot

Gradient_Method.jpg

  1. %  在这个例子中,算法需要20000到30000次的迭代才能到达全局最小值!
  2. %  copyright by fantuanxiaot
  3. clc;
  4. clear all
  5. x=zeros(1,2);
  6. %  x1和x2的初始值
  7. x(1)=-2.3;
  8. x(2)=3.5;
  9. fval=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
  10. g1=100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2;
  11. g2=100*(2*x(2)-2*x(1)^2);
  12. torrence=1e-3;
  13. Ofanshu=sqrt(g1^2+g2^2);
  14. Maxinteration=50000;
  15. a=-0.001;
  16. Alpha=0.03;
  17. sumOfanshu=[];
  18. %  最速下降法最核心的部分
  19. for i=1:Maxinteration
  20.     %  求解导数
  21.     g1=100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2;
  22.     g2=100*(2*x(2)-2*x(1)^2);
  23.     %  导数的求值
  24.     Ofanshu=sqrt(g1^2+g2^2);
  25.     %  导数须小于指定的阈值才能够跳出循环
  26.     if Ofanshu<torrence;
  27.         break;
  28.     end
  29.     %  针对参数x进行不断调整
  30.     dg1=a*g1;
  31.     dg2=a*g2;
  32.     x=x+[dg1 dg2];
  33.     sumOfanshu=[sumOfanshu Ofanshu];
  34. end
  35. %  给收敛的情况做个图
  36. plot(sumOfanshu,'ro','markersize',5,'markerfacecolor','b');
  37. %  不断迭代后的的结果
复制代码

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 13:55:35 |显示全部楼层

基于谢菲尔德大学遗传算法的神经网络应用代码:by fantuanxiaot

本帖最后由 fantuanxiaot 于 2014-12-27 15:02 编辑

基于谢菲尔德大学遗传算法的神经网络应用代码:by fantuanxiaot
首先需要装谢菲尔德大学遗传算法工具箱
主程序:
  1. sqrs=[20.55 22.44 25.37 27.13 29.45 30.10 30.96 34.06 36.42 38.09 39.13 39.99 ...
  2.        41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63];%机动车数(单位:万辆)
  3. sqjdcs=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6...
  4.         2.7 2.85 2.95 3.1];%公路面积(单位:万平方公里)
  5. sqglmj=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 ...
  6.          0.56 0.59 0.59 0.67 0.69 0.79];%公路客运量(单位:万人)
  7. glkyl=[5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 ...
  8.         22598 25107 33442 36836 40548 42927 43462];%公路货运量(单位:万吨)
  9. glhyl=[1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 ...
  10.         13320 16762 18673 20724 20803 21804];
  11. p=[sqrs;sqjdcs;sqglmj];t=[glkyl;glhyl];

  12. [BestError,BestW1,BestW2,BestB1,BestB2,SampleyFit,...
  13. GlobalError,GlobalW1,GlobalW2,GlobalB1,GlobalB2,GlobalSampleyFit]=GEATNewNetworkfun(p,t,[1;3])
复制代码

函数代码:by fantuanxiaot



  1. function [BestError,BestW1,BestW2,BestB1,BestB2,SampleyFit,...
  2. GlobalError,GlobalW1,GlobalW2,GlobalB1,GlobalB2,GlobalSampleyFit]=GEATNewNetworkfun(Samplex,Sampley,MM)

  3. %%%%%%%%Numberofindividuals=50;%%染色体的数目.............举例设置参数值,这拥有九个自变量
  4. %%%%%%%%Maxgeneration=500;%%%最大的迭代数目...........举例设置参数值
  5. %%%%%%%%IndependentNum=5;%%自变量的个数............举例设置参数值
  6. %%%%%%%%SelectProbability=0.9;%%选择概率..........举例设置参数值
  7. %%%%%%%%RecombinationProbability=0.7;%%重组概率...........举例设置参数值
  8. %%%%%%%%PrecisionofVariables=15;%%每个染色体到底有多长..........举例设置参数值
  9. %%%%%%%%Mutationprobability=0.01; %%变异的概率..........举例设置参数值
  10. %%%%%%%%vlb=[-5 -5 -5 -5 -5];vub=[5 5 5 5 5];函数自变量的上下界......举例参数设置
  11. %%%%W1=0.5*rand(H,M)-0.1;
  12. %%%%W2=0.5*rand(N,H)-0.1;
  13. %%%%B1=0.5*rand(H,1)-0.1;
  14. %%%%B2=0.5*rand(N,1)-0.1;

  15. [M,number]=size(Samplex);
  16. [N,number]=size(Sampley);
  17. HiddenNum=ceil(sqrt(M+N))+3;%%隐藏层的个数
  18. H=HiddenNum;
  19. Numberofindividuals=50;
  20. Maxgeneration=200;
  21. IndependentNum=H*M+N*H+H+N;
  22. SelectProbability=0.9;
  23. RecombinationProbability=0.7;
  24. Mutationprobability=0.01;
  25. PrecisionofVariables=10;
  26. vlb=repmat(-0.5,1,IndependentNum);
  27. vub=repmat(0.5,1,IndependentNum);
  28. ub=[vlb;vub];
  29. trace=zeros(Maxgeneration,2);%遗传算法的性能跟踪
  30. Field=[REP(PrecisionofVariables,[1,IndependentNum]);ub;REP([1;0;1;1],[1,IndependentNum])];
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%以上为基础参数的设置

  32. Chorome=CRTBP(Numberofindividuals,PrecisionofVariables*IndependentNum);
  33. %%(Numberofindividuals*(PrecisionofVariables*(H*M+N*H+H+N)))的初始染色体矩阵Chorome
  34. generation=0;
  35. P=BS2RV(Chorome,Field);
  36. ObjV=NewNNWorkfun(P,Samplex,Sampley,MM);
  37. TraceBariables=[];
  38. while generation<Maxgeneration
  39.     f=RANKING(ObjV);
  40.     FitV=GEATfitfun(f,P);
  41.     SelCh=SELECT('SUS',Chorome,FitV,SelectProbability);
  42.     SelCh=RECOMBIN('XOVSP',SelCh,RecombinationProbability);
  43.     SelCh=MUT(SelCh,Mutationprobability);
  44.     P=BS2RV(SelCh,Field);
  45.     ObjSelV=NewNNWorkfun(P,Samplex,Sampley,MM);
  46.     [Chorome,ObjV]=REINS(Chorome,SelCh,1,1,ObjV,ObjSelV);
  47.     P=BS2RV(Chorome,Field);
  48.     generation=generation+1;
  49.     trace(generation,1)=min(ObjV);
  50.     trace(generation,2)=sum(ObjV)/length(ObjV);
  51.     [~,Index]=min(ObjV);
  52.     TraceBariables=[TraceBariables;P(Index,:)];
  53. end
  54. figure(1)
  55. plot(trace(:,1),'b','linewidth',2)
  56. hold on
  57. plot(trace(:,1),'r*','markersize',10)
  58. plot(trace(:,2),'g','linewidth',2)
  59. plot(trace(:,2),'r*','markersize',10)
  60. legend('Error的变化','Error均值的变化');
  61. hold off
  62. [BestError,index]=min(ObjV);
  63. BestP=P(index,:);
  64. W11=BestP(1:H*M);
  65. BestW1=reshape(W11,M,H)';
  66. W22=BestP(H*M+1:H*M+N*H);
  67. BestW2=reshape(W22,H,N)';
  68. B11=BestP(H*M+N*H+1:H*M+N*H+H);
  69. BestB1=B11';
  70. B22=BestP(H*M+N*H+H+1:H*M+N*H+H+N);
  71. BestB2=B22';
  72. [examplein,pmin,pmax,exampleout,tmin,tmax]=premnmx(Samplex,Sampley);
  73. Hiddenin=BestW1*examplein+repmat(BestB1,1,number);
  74. switch MM(1)
  75.     case 1
  76.         Hiddenout=logsig(Hiddenin);
  77.     case 2
  78.         Hiddenout=tansig(Hiddenin);
  79.     case 3
  80.         Hiddenout=purelin(Hiddenin);
  81. end
  82. Netin=BestW2*Hiddenout+repmat(BestB2,1,number);
  83. switch MM(2)
  84.     case 1
  85.         Netout=logsig(Netin);
  86.     case 2
  87.         Netout=tansig(Netin);
  88.     case 3
  89.         Netout=purelin(Netin);
  90. end
  91. SampleyFit=postmnmx(Netout,tmin,tmax);
  92. [GlobalError,Indexx]=min(trace(:,1));
  93. GlobalP=TraceBariables(Indexx,:);
  94. GW11=GlobalP(1:H*M);
  95. GlobalW1=reshape(GW11,M,H)';
  96. GW22=GlobalP(H*M+1:H*M+N*H);
  97. GlobalW2=reshape(GW22,H,N)';
  98. GB11=GlobalP(H*M+N*H+1:H*M+N*H+H);
  99. GlobalB1=GB11';
  100. GB22=GlobalP(H*M+N*H+H+1:H*M+N*H+H+N);
  101. GlobalB2=GB22';
  102. HiddeninG=GlobalW1*examplein+repmat(GlobalB1,1,number);
  103. switch MM(1)
  104.     case 1
  105.         HiddenoutG=logsig(HiddeninG);
  106.     case 2
  107.         HiddenoutG=tansig(HiddeninG);
  108.     case 3
  109.         HiddenoutG=purelin(HiddeninG);
  110. end
  111. NetinG=GlobalW2*HiddenoutG+repmat(GlobalB2,1,number);
  112. switch MM(2)
  113.     case 1
  114.         NetoutG=logsig(NetinG);
  115.     case 2
  116.         NetoutG=tansig(NetinG);
  117.     case 3
  118.         NetoutG=purelin(NetinG);
  119. end
  120. GlobalSampleyFit=postmnmx(NetoutG,tmin,tmax);
  121. end

  122. function MSError=NewNNWorkfun(P,Samplex,Sampley,MM)
  123. M=size(P,1);
  124. Error=zeros(M,1);
  125. [M,number]=size(Samplex);
  126. [N,number]=size(Sampley);
  127. HiddenNum=ceil(sqrt(M+N))+3;%%隐藏层的个数,IndependentNum=H*M+N*H+H+N;
  128. H=HiddenNum;
  129. for i=1:M
  130.     PP=P(i,:);
  131.     W11=PP(1:H*M);
  132.     W1=reshape(W11,M,H)';
  133.     W22=PP(H*M+1:H*M+N*H);
  134.     W2=reshape(W22,H,N)';
  135.     B11=PP(H*M+N*H+1:H*M+N*H+H);
  136.     B1=B11';
  137.     B22=PP(H*M+N*H+H+1:H*M+N*H+H+N);
  138.     B2=B22';
  139.     Errors=NewNNWork(Samplex,Sampley,MM,W1,W2,B1,B2);
  140.     Error(i)=Errors;
  141. end
  142. MSError=Error;
  143. end

  144. function [MSError,pmin,pmax,tmin,tmax]=NewNNWork(Samplex,Sampley,MM,W1,W2,B1,B2)
  145. %%%%MM的第一个数是隐藏层的函数选择,MM的第二个数是输出层的函数选择
  146. %%%1、2、3分别代表logsig和tansig、purelin函数
  147. %%%Samplex为M*number的矩阵,Sampley为N*number的矩阵
  148. [~,number1]=size(Samplex);
  149. [N,number2]=size(Sampley);
  150. if (number1~=number2)
  151.     error('BP Network is wrong!');
  152. end
  153. number=number1;
  154. %%%%W1=0.5*rand(H,M)-0.1;
  155. %%%%W2=0.5*rand(N,H)-0.1;
  156. %%%%B1=0.5*rand(H,1)-0.1;
  157. %%%%B2=0.5*rand(N,1)-0.1;
  158. [examplein,pmin,pmax,exampleout,tmin,tmax]=premnmx(Samplex,Sampley);
  159. Hiddenin=W1*examplein+repmat(B1,1,number);
  160. switch MM(1)
  161.     case 1
  162.         Hiddenout=logsig(Hiddenin);
  163.     case 2
  164.         Hiddenout=tansig(Hiddenin);
  165.     case 3
  166.         Hiddenout=purelin(Hiddenin);
  167. end
  168. Netin=W2*Hiddenout+repmat(B2,1,number);
  169. switch MM(2)
  170.     case 1
  171.         Netout=logsig(Netin);
  172.     case 2
  173.         Netout=tansig(Netin);
  174.     case 3
  175.         Netout=purelin(Netin);
  176. end
  177. Error=exampleout-Netout;
  178. MSError=sumsqr(Error)/(N*number);
  179. end

  180. function z=GEATfitfun(x,y)
  181. %%%%%%%x是函数值,y是种群的十进制值
  182. N=length(x);
  183. for i=1:length(N)
  184.     if abs(sum(y(i,:)-500))>5%%%%%按照y的每一行测度自变量的总和是不是大于500,如果大于500则x(i)=0.001*x(i);
  185.         x(i)=0.001*x(i);
  186.     end
  187. end
  188. z=x;
  189. end
复制代码







已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

风雨山晨 在职认证  发表于 2014-12-27 14:07:32 |显示全部楼层
谢谢分享
回复

使用道具 举报

fantuanxiaot 发表于 2014-12-27 14:07:55 |显示全部楼层

一些关于MATLAB的varargin的实例,大家自己操作操作

本帖最后由 fantuanxiaot 于 2014-12-27 15:14 编辑

一些关于varargin的实例,大家自己操作操作,并且慢慢理解:by fantuanxiaot


  1. function B = Varaginfunexample(varargin)
  2. % varargin是matlab内部变量,cell型,依次存储函数的所有输入变量
  3. % 如add(A1, A2, A3), 则 varargin{1} = A1, varargin{2} = A2, varargin{3} = A3
  4. % nargin同样也是matlab内部变量, 为函数输入变量的个数  
  5. %如果变量个数小于2大于3,则报错
  6. error(nargchk(2,3,nargin));
  7. if nargin == 2         
  8. A1= varargin{1};
  9. A2= varargin{2};
  10. B=A1+A2;
  11. else
  12. A1= varargin{1};A2= varargin{2};
  13. A3=varargin{3};
  14. B=A1*A2*A3;
  15. end ;
复制代码


  1. function f=VararginexampleI(varargin)
  2. error(nargchk(2,4,nargin));
  3. if nargin==2
  4.     A1=varargin{1};
  5.     A2=varargin{2};
  6.     f=A1+A2;
  7. elseif nargin==3
  8.     A1=varargin{1};
  9.     A2=varargin{2};
  10.     A3=varargin{3};
  11.     f=-A1-A2-A3;
  12. else
  13.     A1=varargin{1};
  14.     A2=varargin{2};
  15.     A3=varargin{3};
  16.     A4=varargin{4};
  17.     f=A1.*A2.*A3.*A4;
  18. end
复制代码

计算核密度的关于varargin的输入


  1. function P=VararginexampleIII(xi,x,h1,varargin)
  2. n=numel(varargin);
  3. if n==0
  4.     f=@(u)(1/(sqrt(2*pi))*exp(-u.^2/2));
  5. elseif n==1
  6.     if isa(varargin{1},'function_handle')
  7.         f=varargin{1};
  8.     else
  9.         disp('this function is not a function_handle');
  10.         P=NAN;
  11.         return;
  12.     end
  13. else
  14.     disp('too many variables')
  15.     P=NAN;
  16.     return;
  17. end
  18. N=length(xi);
  19. hn=h1/sqrt(N);
  20. [X,Xi]=meshgrid(x,xi);
  21. P=sum(f((X-Xi)./hn))/hn/N;
  22.    
  23.    
  24.         
复制代码

以上的资源分享基于fantuanxiaot的MATLAB的编程,代码没有问题,均在R2010b的MATLAB版本作为平台,大家自己研究。

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
zbin7451f + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 我要注册

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2019-9-19 19:20