zoukankan      html  css  js  c++  java
  • MATLAB在数学建模中的应用(三)

    optimset函数

    功能:创建或编辑优化选项参数结构。
    语法:

    1 options = optimset(‘param1’,value1,’param2’,value2,…) 
    2 options = optimset 
    3 options = optimset(oldopts,’param1’,value1,…) 



    描述:
    options = optimset(‘param1’,value1,’param2’,value2,…) 创建一个称为options的优化选项参数,其中指定的参数具有指定值。所有未指定的参数都设置为空矩阵[](将参数设置为[]表示当options传递给优化函数时给参数赋缺省值)。赋值时只要输入参数前面的字母就行了。

    optimset函数没有输入输出变量时,将显示一张完整的带有有效值的参数列表。

    options = optimset (with no input arguments) 创建一个选项结构options,其中所有的元素被设置为[]。

    eg 2.7

     1 function f= fun1(x)
     2 f=x(1)^2+x(2)^2+8;
     3 end
     4 function [g,h]=fun2(x)
     5 g=-x(1)^2-x(2)^2;
     6 h=-x(1)-x(2)^2+2;
     7 end
     8 
     9 %命令以及函数调用
    10 options=optimset;
    11 [x,y]=fmincon('fun1',rand(2,1),[],[],[],[],zeros(2,1),[],...
    12     'fun2',options)
    13 x =
    14 
    15     0.5000
    16     1.2247
    17 
    18 
    19 y =
    20 
    21     9.7500

    eg 2.8

     1 function f= fun1(x)
     2 f=-(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)));
     3 end
     4 function [g,h]=fun2(x)
     5 g(1)=x(1)-400;
     6 g(2)=1.1*x(1)+x(2)-440;
     7 g(3)=1.21*x(1)+1.1*x(2)+x(3)-484;
     8 g(4)=1.331*x(1)+1.21*x(2)+1.1*x(3)+x(4)-532.4;
     9 h=0;
    10 end
    11 
    12 x0=[1;1;1;1];
    13 %options=optimset;
    14 [x,y]=fmincon('fun1',x0,[],[],[],[],zeros(4,1),[],...
    15     'fun2')
    16 x =
    17 
    18    1.0e+02 *
    19 
    20    0.861883268508773
    21    1.042878554277346
    22    1.261883183737369
    23    1.526878812873891
    24 
    25 
    26 y =
    27 
    28  -43.085960574428569

    罚函数法:
    eg 2.10
    1)普通方法

     1 function f= fun1(x)
     2 f=x(1)^2+x(2)^2+8;
     3 end
     4 function [g,h]=fun2(x)
     5 g=-x(1)^2+x(2);
     6 h=-x(1)-x(2)^2+2;
     7 end
     8 
     9 [x,y]=fmincon('fun1',rand(2,1),[],[],[],[],zeros(2,1),[],...
    10     'fun2
    11 x =
    12 
    13    1.000001999995532
    14    0.999999000001734
    15 
    16 
    17 y =
    18 
    19   10.000001999999531

    2)罚函数

     1 function g= fun1(x)
     2 M=50000;
     3 f=x(1)^2+x(2)^2+8;%产生的中间函数
     4 g=f-M*min(x(1),0)-M*min(x(2),0)-M*min(x(1)^2-x(2),0)+...
     5     M*abs(-x(1)-x(2)^2+2);
     6 end
     7 [x,y]=fminunc('fun1',rand(2,1))
     8 x =
     9 
    10    0.508134596752331
    11    1.221419421512379
    12 
    13 
    14 y =
    15 
    16      4.817068272095967e+04

    rand(‘state’,sum(clock))
    作用是定义一个随时间变化的初值x_0。

    tic和toc
    用来记录matlab命令执行的时间。
    tic用来保存当前时间,而后使用toc来记录程序完成时间。
    两者往往结合使用,用法如下:
    tic
    operations
    toc
    显示时间单位:秒。

    y = floor(x)
    函数将x中元素取整,值y为不大于本身的最小整数。

    ceil
    是向离它最近的大整数取整

    云模型

     1 N=1500;
     2 Y=[9.5 10.3 10.1 8.1
     3     10.3 9.7 10.4 10.1
     4     10.6 8.6 9.2 10
     5     10.5 10.4 10.1 10.1 
     6     10.9 9.8 10 10.1
     7     10.6 9.8 9.7 10
     8     10.4 10.5 10.6 10.3
     9     10.1 10.2 10.8 8.4
    10     9.3 10.2 9.6 10
    11     10.5 10 10.7 9.9]';
    12 
    13 for i=1:size(Y,1)
    14     subplot(size(Y,1)/2,2,i);
    15     [x,y,Ex,En,He]=cloud_transform(Y(i,:),N);
    16     plot(x,y,'r.');
    17     xlabel('射击成绩分布/环');
    18     ylabel('确定度');
    19     title(strcat('',num2str(i),'人射击云模型还原图'));
    20     axis([8,12,0,1]);
    21 end
    22 function [x,y,Ex,En,He]= cloud_transform(y,n)
    23 Ex=mean(y);%y是一个向量,代表每一个人的射击数据
    24 En=mean(abs(y-Ex)).*sqrt(pi/2);%向量
    25 He=sqrt(var(y)-En.^2);%向量
    26 for q=1:n
    27     Enn=randn(1).*He+En;
    28     %randn(1)产生一个随数,但He为向量
    29     x(q)=randn(1).*Enn+Ex;
    30     y(q)=exp(-(x(q)-Ex).^2./(2.*Enn.^2));
    31 end
    32 x;
    33 y;
    这里写图片描述
    

    logistic模型

     1 syms a b;
     2 c=[a b]';
     3 A=[174 179 183 189 207 234 220.5 256 270 285];
     4 B=cumsum(A);
     5 n=length(A);
     6 for i=1:(n-1)
     7     C(i)=(B(i)+B(i+1))/2;
     8 end
     9 D=A;
    10 D(1)=[];
    11 D=D';
    12 E=[-C;ones(1,n-1)];
    13 c=inv(E*E')*E*D;
    14 c=c';
    15 a=c(1);b=c(2);
    16 %计算待定参数a,b的值
    17 F=[];F(1)=A(1);
    18 for i=2:(n+10)
    19     F(i)=(A(1)-b/a)/exp(a*(i-1))+b/a;
    20 end
    21 G=[];G(1)=A(1);
    22 for i=2:(n+10)
    23     G(i)=F(i)-F(i-1);%为预测出来的20组数据
    24 end
    25 t1=1995:2004;
    26 t2=1995:2014;
    27 G;a,b
    28 plot(t1,A,'o',t2,G)
    这里写图片描述
    

    归一化函数mapminmax
    [pn,ps]=mapminmax(P)或=mapstd(P) %P是输入向量,pn,ps是什么?
    pn表示你归一化后的返回数据
    ps是进行归一化时所用的参数。
    下面的实例更能帮助理解:

     1 x=[100,200,300,400]; 
     2 [pn,px]=mapminmax(x); 
     3 pn = 
     4 -1.0000 -0.3333 0.3333 1.0000 
     5 px = 
     6 name: ‘mapminmax’ 
     7 xrows: 1 
     8 xmax: 400 
     9 xmin: 100 
    10 xrange: 300 
    11 yrows: 1 
    12 ymax: 1 
    13 ymin: -1 
    14 yrange: 2 
    15 no_change: 0 
    16 gain: 0.0067 
    17 xoffset: 100

    figure
    是建立图形的意思,系统自动从1,2,3,4…来建立图形,数字代表第几幅图形,figure(1),figure(2)就是第一第二副图的意思,在建立图形的时候,您注意一下它的标题就是figure1或figure2等等,对应到程序中就是您的例子语句
    一般建立新图只需要一个figure就行,系统自动建立新图,可以简单一点,当然要加上也可以。

    subplot(m,n,k)
    多子图,就是一张图中有好多小图,也是有标号的
    subplot(‘Position’,[left bottom width height])
    m表示画几行
    n表示画几列
    k表示现在画的是第几幅图

    基于MATLAB工具箱公路运量预测 P137 源代码

     1 clc;
     2 p=[sqrs;sqjdcs;sqglmj];
     3 t=[glkyl;glhyl];
     4 [pn,input_str]=mapminmax(p);
     5 [tn,output_str]=mapminmax(t);
     6 net=newff(pn,tn,[3,7,2],{'purelin','logsig','purelin'}); %建立模型,并用梯度下降法训练.
     7 net.trainParam.show=10;             
     8 net.trainParam.Lr=0.05;                 %学习速度为0.05
     9 net.trainParam.epochs=5000;           %最大训练轮回为50000次
    10 net.trainParam.goal=0.65*10^(-3);   
    11 net.divideFcn='';   
    12 net=train(net,pn,tn);                   %开始训练,其中pn,tn分别为输入输出样本
    13 %利用原始数据对BP网络仿真
    14 an=sim(net,pn);           %用训练好的模型进行仿真
    15 a=mapminmax('reverse',an,output_str); % 把仿真得到的数据还原为原始的数量级
    16 x=1990:2009;
    17 newk=a(1,:);
    18 newh=a(2,:);
    19 figure(2);
    20 subplot(2,1,1);
    21 plot(x,newk,'r-o',x,glkyl,'b--+');
    22 legend('网络输出客运量','实际客运量');
    23 xlabel('年份');
    24 ylabel('客运量/万人');
    25 title('运用工具箱客运量学习和测试对比图');
    26 subplot(2,1,2);
    27 plot(x,newh,'r-o',x,glhyl,'b--+');
    28 legend('网络输出货运量','实际货运量');
    29 xlabel('年份');
    30 ylabel('货运量/万人');
    31 title('运用工具箱货运量学习和测试对比图');
    32 
    33 pnew=[73.39 75.55
    34     3.9635 4.0975
    35     0.9880 1.0268];
    36 pnewn=mapminmax('apply',pnew,input_str);
    37 anewn=sim(set,pnewn);
    38 anew=mapminmax('reverse',anewn,output_str);

    但是不造为毛,出错 haha (line 21)

    1 plot(x,newk,’r-o’,x,glkyl,’b–+’);
  • 相关阅读:
    /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:在函数‘_start’中: (.text+0x20):对‘main’未定义的引用
    W: 仓库 “http://ppa.launchpad.net/levi-armstrong/qt-libraries-trusty/ubuntu xenial Release” 没有 Release 文件。
    sudo apt-get常用命令
    Windows 下 MQTT 服务器搭建之Apollo
    sudo apt-get update报错E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
    struct.error: cannot convert argument to integer解决办法
    PHP MySQL 创建数据库和表 之 Create
    PHP MySQL 连接数据库 之 Connect
    飞行的架构师和奔跑的程序员
    hdu 5610 Baby Ming and Weight lifting
  • 原文地址:https://www.cnblogs.com/twomeng/p/9476366.html
Copyright © 2011-2022 走看看