1、MATLAB学习笔记1.调入图片,图片转换clear all,clc%x,map=imread(preas.png);figure(1)imshow(x) x1=rgb2gray(x);figure(2)imshow(x1) x2=flipud(x1);figure(3)imshow(x2)2.读数据d=xlsread(2023年B试题数据.xls);3.读取EXCL数据表格中的数据d=xlsread(2023年B试题数据.xls); c=xlsread(2023年B试题数据.xls,L15:M19)c = 0 0 0 0 0 8 4 010 64.载入数据 load 020619.six d
2、=X020619; d图形的绘制1、一般二维图像的绘制【plot(X,Y)】例一:plot(X,Y)x=0 2 1 5 7; y=1 -4 5.1 7 9;figure(1)plot(x,y,ro)hold onaxis(-2 8 -6 12) 【定义区间】plot(x,y)例二:plot(x1,y1,x2,y2,)x=linspace(0,2*pi,50);plot(x,cos(x),bo,x,sin(x),r*)例三:(参数方程绘图)t=0:pi/500:2*pi;x=10*cos(t);y=2*sin(t);plot(x,y);grid on 【显示网格线】例四:(隐函数的绘图)ezpl
3、ot(f(x),Xmin,Xmax,Ymin,max)(默认区间为:-2*pix2*pi,-2*piy x=0 1 2; y=2 3 4; xx,yy=meshgrid(x,y)【生成网格数据】xx = 0 1 2 0 1 2 0 1 2yy = 2 2 2 3 3 3 4 4 4例二:(mesh()和surf()的绘图比较)x,y=meshgrid(0:31);z=1./(1+(sqrt(x-16).2+(y-16).2).4./200);mesh(x,y,z);axis(0,31,0 31,0,1)x,y=meshgrid(0:31);z=1./(1+(sqrt(x-16).2+(y-16
4、).2).4./200);surf(x,y,z);axis(0,31,0 31,0,1)例三:水道测量数据示意图x=129 140 103.5 88 185.5 195 105.5 157.5 107.5 77 81 162 162 117.5;y=7.5 141.5 23 147 22.5 137.5 88.5 -6.5 -81 3 56.5 -66.5 84 -33.5;z=1.34 2.67 2 2.67 2 2.67 2.67 3 3 2.67 2.67 3 1.34 3figure(1)plot3(x,y,z,o)figure(2)x1=linspace(min(x),max(x),
5、100);y1=linspace(min(y),max(y),100);xx,yy=meshgrid(x1,y1);zz=griddata(x,y,z,xx,yy)【数据插值】mesh(xx,yy,zz)例四:降雨量数据画图clear all,clc%load 020716.sixdata=X020716;jd=data(:,2);wd=data(:,3);rl=data(:,4);plot3(jd,wd,rl,ro)hold onj=linspace(min(jd),max(jd),100);w=linspace(min(wd),max(wd),100);jj,ww=meshgrid(j,w
6、);rr=griddata(jd,wd,rl,jj,ww);mesh(jj,ww,rr)rn=griddata(jd,wd,rl,30.9,119.1)优化问题1、求最优解问题 x=0:2*pi/100:2*pi; y=cos(x); plot(x,y) 【 help fzero 此函数是求某函数到0的x值】 x = fzero(cos,1 2) x = 1.5708(cos表达调用函数文献中的cos (x)函数)2.无约束条件的最优化求解x,fval=fminunc(fun,x0)x,fval=fminsearch(fun,x0)例一:fun=2*x(1)3+4*x(1)*x(2)3-10*
7、x(1)*x(2)+x(2)2; x,fval=fminsearch(fun,0 0)x = 1.0016 0.8335fval = -3.3241例二: fun=3*x(1)2+2*x(1)*x(2)+x(2)2; x0=1 1; x,fval=fminunc(fun,x0)x = 1.0e-006 * 0.2541 -0.2029fval = 1.3173e-0132、有约束的一元函数的最小值求解x,fval=fminbnd(fun,a,b)例一: fun=(x3+cos(x)+x*log(x)/exp(x); x,fval=fminbnd(fun,0,1)x = 0.5223fval =
8、 0.3974例二: fun=(x-3)2-1; x,fval=fminbnd(fun,-10,10)x = 3fval = -13、有约束的多元函数的最小值求解(线性规划、0-1规划问题)X=linprog(f,A,b,Aeq,beq,LB,UB,X0,OPTIONS) (后面的参数可以省中间的或前面的 要用空矩阵代替)例一:f(x)=-5x1-4x2-6x3条件:x1-x2+x3=20;3x1+2x2+4x3=42;3x1+2x2=0,x2=0,x3=0;解为:f=-5; -4; -6;A=1 -1 1;3 2 4; 3 2 0;b=20 42 30;lb=0; 0; 0;x,fval=l
9、inprog(f,A,b,lb);结果x = 0.0000 15.0000 3.0000fval = -78.0000例二:f= -9 -5 -6 -4;A=6 3 5 2; 0 0 1 1; -1 0 1 0;0 -1 0 1;b=9 1 0 0;x,fval=linprog(f,A,b)解得:x = 1.0e+008 * 0.0000 2.0961 -2.0961 2.0961fval = -6.2882e+0084、非线性规划问题的求解x,fval=fmincon(fun,x0,A,b,Aeq,beq,lu,ub,nonlcon,options)其中,fun为目的函数,X0为初始值,A,
10、b满足线性不等式约束,Aeq,beq满足等式约束,lu,ub满足lu=X=ub.例一:先定义函数文献function y=objectf2(x)y=-x(1)*x(2)*x(3);在用脚本文献clear all, clc%x0=10; 10; 10;A=1 2 3; -1 -2 -3;b=72;0;x,fval=fmincon(objectf2,x0,A,b)解得:x = 24.0000 12.00008.0000fval = -2.3040e+0035、非线性的二次型规划问题的求解x,fval=quadprog(H,f,A,B,Aep,Bep,Xmin,Xmax,X0,opt,p1,p2,)
11、例一:f(x)=0.5x12+x22-x1x2-2x1-6x2条件 x1+x2=2;-x1+x2=0;x2=0;解为:H=1 -1; -1 2; 【 二次型转化成矩阵里的系数;第一个0.5提出就行了;】f=-2;-6; 【一次的x1 x2 的系数A=1 1;-1 2; 【约束条件的系数】b=2;3; 【方程的右边】lb=0;0;x,fval=quadprog(H,f,A,b,lb);结果是x = 0.4000 1.6000fval = -8.40006、三种方法的综合比较应用题目f(x)=0.5x(1)2+(x)22-x(1)x2-2x(1)-6x(2)条件 x(1)+x(2)=2;-x(1)
12、+x(2)=0;x(2)=0;解法:先定义并保存文献function y=objectf2(x);y=0.5*x(1)2+x(2)2-x(1)*x(2)-2*x(1)-6*x(2)求解:clear all,clcA=1 1;-1 2;2 1;b=2;2;3;lb=0;0;x1 fval1=ga(objectf2,2,A,b,lb)x2 favl2=fmincon(objectf2,11,A,b,lb)H=1 -1;-1 2;f=-2;-6;x3 favl3=quadprog(H,f,A,b,lb)解得:x1 = 0.7311 1.2648fval1 = -8.1085x2 = 0.6667 1
13、.3333favl2 = -8.2222x3 = 0.6667 1.3333favl3 = -8.22227、试题求解:y=-(2*x(1)+3*x(1)2+3*x(2)+x(2)2+x(3);的最小值条件: x(1)+2*x(1)2+x(2)+2*x(2)2+x(3)=10;x(1)+x(1)2+x(2)+x(2)2-x(3)=50;2*x(1)+x(1)2+2*x(2)+x(3)1;x(1)=0解法:function y=objectf3(x)%y=-(2*x(1)+3*x(1)2+3*x(2)+x(2)2+x(3);function c,ceq=mycon1(x)%c(1)=x(1)+2
14、*x(1)2+x(2)+2*x(2)2+x(3)-10;c(2)=x(1)+x(1)2+x(2)+x(2)2-x(3)-50;c(3)=2*x(1)+x(1)2+2*x(2)+x(3)-40;ceq=x(1)2+x(3)-2;clear all,clc%A=-1 -2 0;b=-1;lb=0; x1,fval1=ga(objectf3,3,A,b,lb,mycon1)x2,fval2=fmincon(objectf3,2 0.7 -2.2,A,b,lb,mycon1)解得;x1 = 1.0319 1.4861 0.9352fval1 = -12.8599x2 = 2.3333 0.1667 -
15、3.4444fval2 = -18.0833插值与拟合1、一维插值(在数据点之间插值)Yi=interp1(x,y,xi,methed) (默认为线性插值) Methed可以的选择:nearest-nearest neighbor interpolation(最临近插值) linear - linear interpolation(线性插值) spline - piecewise cubic spline interpolation (三次样条插值) pchip-shape-preserving piecewise cubic interpolation cubic-same as pchip
16、(三次插值)例一: x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi); plot(x,y,o,xi,yi)图像如下:例二: t= 1900:10:1990; p = 75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212 226.505 249.633; y=interp1(t,p,1975)y = 214.8585 x = 1900:1:2023; y = interp1(t,p,x,spline); plot(t,p,o,x,y)例三: x = 0:.2:p
17、i; y = sin(x); pp = interp1(x,y,cubic,pp); xi = 0:.1:pi; yi = ppval(pp,xi);plot(x,y,ko),hold on,plot(xi,yi,r:), hold off例四:几种样条插值的比较x1=0:0.112:1;x=0:0.001:1;y1=(x1.2-3*x1+5).*exp(-5*x1).*sin(x1);y2=interp1(x1,y1,x);y3=interp1(x1,y1,x,spline);y4=interp1(x1,y1,x,nearest);y5=interp1(x1,y1,x,cubic);figu
18、replot(x1,y1,ro,x,y2)1、线性插值2、三次样条插值3、最临近插值4、三次插值2、二维插值 方法一:Zi = interp2(X,Y,Z,XI,YI methed)(用于对二维网格数据的插值) 方法二:Zi = griddata(X,Y,Z,XI,YI methed)(用于对二维随机数据点的插值) Methed可以的选择:nearest - nearest neighbor interpolationlinear - bilinear interpolation(默认设立)cubic - bicubic interpolationspline - spline interpo
19、lation例一:X,Y = meshgrid(-3:.25:3);Z = peaks(X,Y);XI,YI = meshgrid(-3:.125:3);ZI = interp2(X,Y,Z,XI,YI);mesh(X,Y,Z), hold, mesh(XI,YI,ZI+15)hold offaxis(-3 3 -3 3 -5 20)例二:x=-3+6*rand(1991,1);y=-2+4*rand(1991,1);z=(x.2-2*x).*exp(-x.2-y.2-x.*y);x1,y1=meshgrid(-3:0.2:3,-2:0.2:2);z1=griddata(x,y,z,x1,y1
20、,cubic);surf(x1,y1,z1);axis(-3,3,-2,2,-0.7,1.5);figure;z2=griddata(x,y,z,x1,y1,v4);surf(x1,y1,z2);axis(-3,3,-2,2,-0.7,1.5);1、立方插值法2、v4插值法3、多项式拟合P=polyfit(x,y,n)多项式拟合,返回的是N次多项式的N+1个序数,按次数由高到底排列,例一:(各阶多项式拟合效果比较)x0=-1+2*0:10/10;y0=1./(1+25*x0.2);x=-1:0.01:1;ya=1./(1+25*x.2);p3=polyfit(x0,y0,3);y1=polyv
21、al(p3,x);p5=polyfit(x0,y0,5);y2=polyval(p5,x);p8=polyfit(x0,y0,8);y3=polyval(p8,x);p10=polyfit(x0,y0,10);y4=polyval(p10,x);figure(1)plot(x,ya)figure(2)plot(x,y1,c,x,y2,g+,x,y3,kp,x,y4,r)4、lsqcurvefit函数(非线性最小二乘拟合)X=lsqucurvefit(fun,x0,xdata,ydata)X=lsqucurvefit(fun,x0,xdata,ydata,lb,ub)例一: clear all
22、x=3 6 9 12 15 18 21 24; y=57.6 41.9 31.0 22.7 16.6 12.2 8.9 6.5; figure(1); plot(x,y,+r)function y=huchao(c,x);y=c(1)*exp(c(2)*x)x=3:3:24;y=57.6 41.9 31.0 22.7 16.6 12.2 8.9 6.5;x0=1 1;f=lsqcurvefit(huchao,x0,x,y)f = 78.4500 -0.1036所以拟合得到的曲线方程为:Y=78.4500*exp(-0.1036*x)画出图形如下: x=0:0.125:25; y=78.4500
23、*exp(-0.1036*x); figure(2); plot(x,y);拟合前后两图像比较:5、其他相关函数lscov(),lsqnonlin()Lsqnonlin函数的用法x,resnorm= lsqnonlin(fun,x0,lb,ub,options)例一:求解x使之:逐项求和f(x)最小,k是从1到10 ,f(x)=(2+2*k-exp(k*x(1)-exp(k*x(2)2,起始点(0.3,0.4)解得:function F = myfun(x)k = 1:10;F = 2 + 2*k-exp(k*x(1)-exp(k*x(2);x0 = 0.3 0.4 x,resnorm = l
24、sqnonlin(myfun,x0) x = 0.2578 0.2578resnorm = 124.3622刀具磨损问题的多项式拟合用法举例: x=0 1 2 3 4 5 6 7; y=27.0 26.8 26.5 26.3 26.1 25.7 25.3 24.8; figure(1); plot(x,y,o)描出各点: x=0 1 2 3 4 5 6 7; y=27.0 26.8 26.5 26.3 26.1 25.7 25.3 24.8; figure(2); plot(x,y)描出大体折线: x=0 1 2 3 4 5 6 7; y=27.0 26.8 26.5 26.3 26.1 25
25、.7 25.3 24.8; p=polyfit(x,y,1)p = -0.3036 27.1250拟合的图形曲线: x=0:0.01:7; y=-0.3036*x+27.125; figure(1); plot(x,y)拟合前后两曲线比较: tt=min(x):0.1:max(x); yy=polyval(p,tt); plot(tt,yy)Polyval的用法:多项式 求p(x)=3*x2+2*x+1 在x = 5, 7, 9时p = 3 2 1;polyval(p,5 7 9)ans = 86 162 262海浪高度数据的拟合问题详解第一组数据绘图: x=2.4000 1.2023-0.1
26、000-0.5000-2.5000-3.0000-2.7000-1.6000 0.20232.10003.40003.60002.90001.60000.2023-1.2023 -2.4000-3.0000-3.1000-2.3000-0.70001.30002.90003.6000; y=0.0000:1.0000:23.0000; figure(2); plot(y,x,+r)第二组数据绘图: x=0.0000:1.0000:23.0000; y=3.10002.00000.60000.6000-2.2023-3.6000-3.2023-2.5000 -0.9000-1.10002.900
27、03.90003.60002.50001.0000 0.0000 -2.4000-3.0000-3.4000-3.0000-1.70000.20232.20233.5000; figure(3); plot(x,y,*r)两组数据图像比较:x1=2.4000 1.2023 -0.1000 -0.5000 -2.5000 -3.0000 -2.7000 -1.6000 0.2023 2.1000 3.4000 3.6000 2.9000 1.6000 0.2023 -1.2023 -2.4000 -3.0000 -3.1000 -2.3000 -0.7000 1.3000 2.9000 3.60
28、00;x2=3.1000 2.0000 0.6000 0.6000 -2.2023 -3.6000 -3.2023 -2.5000 -0.9000 -1.1000 2.9000 3.9000 3.6000 2.5000 1.0000 0.0000 -2.4000 -3.0000 -3.4000 -3.0000 -1.7000 0.2023 2.2023 3.5000; y1=0.0000:1.0000:23.0000;figure(3);plot(y1,x1,*r)plot(y1,x1)hold onplot(y1,x2,+b)plot(y1,x2)对图像进行拟合得到的函数及数据是:数据一进行
29、拟合:function y=huchaof(c,x);y=c(1)*sin(2*pi/12*x)+c(2)*cos(2*pi/12*x)x=0.0000:1.0000:23.0000;y=2.4000 1.2023 -0.1000 -0.5000 -2.5000 -3.0000 -2.7000 -1.6000 0.2023 2.1000 3.4000 3.6000 2.9000 1.6000 0.2023 -1.2023 -2.4000 -3.0000 -3.1000 -2.3000 -0.7000 1.3000 2.9000 3.6000;x0=1 1f=lsqcurvefit(huchao
30、f,x0,x,y)得到数据为:f = -1.4608 2.8528图像为:y=-1.4608*sin(2*pi/12*x)+2.8528*cos(2*pi/12*x);x=0.0000:0.1000:23.0000;figure(1);plot(x,y)数据二进行拟合为:function y=huchaof(c,x);y=c(1)*sin(2*pi/12*x)+c(2)*cos(2*pi/12*x)x=0.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.
31、0000 14.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000 23.0000;y=3.1000 2.0000 0.6000 0.6000 -2.2023 -3.6000 -3.2023 -2.5000 -0.9000 -1.1000 2.9000 3.9000 3.6000 2.5000 1.0000 -2.4000 -3.0000 -3.4000 -3.0000 -1.7000 0.2023 2.2023 3.5000;x0=1 1;f=lsqcurvefit(huchaof,x0,x,y)得到数据为:f = -0.
32、4785 3.4196图像为:y=-0.4785*sin(2*pi/12*x)+3.4196*cos(2*pi/12*x);x=0.0000:0.1000:23.0000;figure(2);plot(x,y,r)两图像进行比较得到为两组数据进行合并拟合function y=huchaof(c,x);y=c(1)*sin(2*pi/12*x)+c(2)*cos(2*pi/12*x)x=0:1:38 40:47;y=2.4000 1.2023 -0.1000 -0.5000 -2.5000 -3.0000 -2.7000 -1.6000 0.2023 2.1000 3.4000 3.6000 2
33、.9000 1.6000 0.2023 -1.2023 -2.4000 -3.0000 -3.1000 -2.3000 -0.7000 1.3000 2.9000 3.6000 3.1000 2.0000 0.6000 0.6000 -2.2023 -3.6000 -3.2023 -2.5000 -0.9000 -1.1000 2.9000 3.9000 3.6000 2.5000 1.0000 -2.4000 -3.0000 -3.4000 -3.0000 -1.7000 0.2023 2.2023 3.5000;x0=1 1;f=lsqcurvefit(huchaof,x0,x,y)得到的
34、数据为:f= -0.9910 3.1362函数图象:clear all,clcx=0.0000:0.1000:47.0000;y=-0.9910*sin(2*pi/12*x)+3.1362*cos(2*pi/12*x)figure(1);plot(x,y)三个函数图象进行比较得到:clear all,clc x=0.0000:0.1000:23.0000;y=-1.4608*sin(2*pi/12*x)+2.8528*cos(2*pi/12*x);figure(1);plot(x,y,r);hold on x=24.0000:0.1000:47.0000;y=-0.4785*sin(2*pi/
35、12*x)+3.4196*cos(2*pi/12*x);figure(1);plot(x,y,r);hold on x=0.0000:0.1000:47.0000;y=-0.9910*sin(2*pi/12*x)+3.1362*cos(2*pi/12*x);figure(1);plot(x,y)国土面积问题的计算clear all,clc%x=7 10.5 13.0 17.5 34.0 40.5 44.5 48.0 56.0 61.0 68.5 76.5 80.5 91.0 96.0 101.0 104.0 106.5 111.5 118.0 123.5 136.5 142.0 146.0 1
36、50.0 157.0 158.0;y1=44 45 47 50 50 38 30 30 34 36 34 41 45 46 43 37 33 28 32 65 55 54 52 50 66 66 68;y2=44 59 70 72 93 100 110 110 110 117 118 116 118 118 121 124 121 121 121 122 116 83 81 82 86 85 68; plot(x,y1,+r)hold onplot(x,y2,og)dx=diff(x) s=0;for i=1:1:length(x)-1 s=s+dx(i)*0.5*(y2(i)+y2(i+1)
37、-y1(i+1)-y1(i)ends得到:s = 8.5888e+003clear all,clc%x=7 10.5 13.0 17.5 34.0 40.5 44.5 48.0 56.0 61.0 68.5 76.5 80.5 91.0 96.0 101.0 104.0 106.5 111.5 118.0 123.5 136.5 142.0 146.0 150.0 157.0 158.0;y1=44 45 47 50 50 38 30 30 34 36 34 41 45 46 43 37 33 28 32 65 55 54 52 50 66 66 68;y2=44 59 70 72 93 10
38、0 110 110 110 117 118 116 118 118 121 124 121 121 121 122 116 83 81 82 86 85 68; plot(x,y1,+r)hold onplot(x,y2,og) dx=diff(x) s=0;for i=1:1:length(x)-1 s=s+dx(i)*0.5*(y2(i)+y2(i+1)-y1(i+1)-y1(i)ends xx=linspace(min(x),max(x),200);yy1=interp1(x,y1,xx);yy2=interp1(x,y2,xx);figure(1)plot(xx,yy1,+)hold
39、onplot(xx,yy1)plot(xx,yy2,+)plot(xx,yy2)for j=1:1:length(xx) plot(xx(j),xx(j),yy1(j),yy2(j)enddxx=diff(xx);ss=0for i=1:1:length(xx)-1 ss=ss+dxx*0.5*(yy2(i)+yy2(i+1)-yy1(i)-yy1(i+1)endss得到: 1.0e+003 *8.5876图像如下:遗传算法ga函数的应用(有约束问题)x=ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options)其中各项解释如下:fitnessfcnFitness functionnvarsnvarsNumber of design variablesoptionsOptionsstructure created usin