大连理工大学上机作业
- 格式:doc
- 大小:104.50 KB
- 文档页数:9
工科数学分析上机作业说明:以下两道题均是使用Matlab 语言,且在Matlab 7.0中运行通过。
1.(两个重要极限)计算下列函数的函数值并画出图形,观察两个重要极限值。
(1)y=f(x)=; (2)y=f(x)=.sin x x (1+x)1x 解:(1)求解过程如下:>> syms x>> y=limit(sin(x)/x)y =1>> ezplot(sin(x)/x,[-10*pi,10*pi])>> ezplot(sin(x)/x,[-1*pi,1*pi])其图形如下:(2)求解过程如下:>> syms x>> y=(1+x)^(1/x)y =(1+x)^(1/x)>> y=limit((1+x)^(1/x))y =exp(1)>> ezplot((1+x)^(1/x),[-1000,1000]) >> ezplot((1+x)^(1/x),[-10,10]) >> ezplot((1+x)^(1/x),[-1,1])其图像如下:分析如下:(1)当x 取值为[-30,30]时,由该题的第一个图像可以看到,函数值在不断震荡,一会为正数,一会为负数。
而当x 取值为[-3,3]时,函数值始终大于0。
当x 趋近于0时,由该题的第二个图像可以得到函数值为1。
另外,该结论也可以由夹逼法则证明,结果不变,当x 趋近于0时,函数值仍为1。
(2)由该题的三个图像可以知道,该函数在定义域内为单调递减函数。
且由该题的第一和二个图像知道,当x 在[0,10]区间内,函数递减趋势非常迅速。
由该题的第三个图像知道,当x 趋于0 时,函数值为自然对数的底数 e ,即约为2.71828.3.计算f(x)=,12+1√2π∫x 0e ‒t 2/2dt 1≪x ≪3的函数值{f (0.1k );k=1,2,…,30}.计算结果取7位有效数字。
2016年理工大学优化方法上机大作业学院:专业:班级:学号::上机大作业1:1.最速下降法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = steepest(x0,eps)gk = grad(x0);res = norm(gk);k = 0;while res > eps && k<=1000dk = -gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=steepest(x0,eps)2.牛顿法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad2(x)g = zeros(2,2);g(1,1)=2+400*(3*x(1)^2-x(2));g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = newton(x0,eps)gk = grad(x0);bk = [grad2(x0)]^(-1);res = norm(gk);k = 0;while res > eps && k<=1000dk=-bk*gk;xk=x0+dk;k = k+1;x0 = xk;gk = grad(xk);bk = [grad2(xk)]^(-1);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x1=newton(x0,eps)--The 1-th iter, the residual is 447.213595--The 2-th iter, the residual is 0.000000x1 =1.00001.00003.BFGS法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = bfgs(x0,eps)g0 = grad(x0);gk=g0;res = norm(gk);Hk=eye(2);k = 0;while res > eps && k<=1000dk = -Hk*gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;fa0=xk-x0;x0 = xk;go=gk;gk = grad(xk);y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*( y0)))+(fa0*(fa0)')/((fa0)'*(y0));res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;End>> clear>> x0=[0,0]';>> eps=1e-4;>> x=bfgs(x0,eps)4.共轭梯度法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star =CG(x0,eps)gk = grad(x0);res = norm(gk);k = 0;dk = -gk;while res > eps && k<=1000ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + 0.1*ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;g0=gk;gk = grad(xk);res = norm(gk);p=(gk/g0)^2;dk1=dk;dk=-gk+p*dk1;fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk; end>> clear>> x0=[0,0]'; >> eps=1e-4; >> x=CG(x0,eps)上机大作业2:function f= obj(x)f=4*x(1)-x(2)^2-12;endfunction [h,g] =constrains(x) h=x(1)^2+x(2)^2-25;g=zeros(3,1);g(1)=-10*x(1)+x(1)^2-10*x(2)+x(2)^2+34;g(2)=-x(1);g(3)=-x(2);endfunction f=alobj(x) %拉格朗日增广函数%N_equ等式约束个数?%N_inequ不等式约束个数N_equ=1;N_inequ=3;global r_al pena;%全局变量h_equ=0;h_inequ=0;[h,g]=constrains(x);%等式约束部分?for i=1:N_equh_equ=h_equ+h(i)*r_al(i)+(pena/2)*h(i).^2;end%不等式约束部分for i=1:N_inequh_inequ=h_inequ+(0.5/pena)*(max(0,(r_al(i)+pena*g(i))).^2-r_al(i).^2) ;end%拉格朗日增广函数值f=obj(x)+h_equ+h_inequ;function f=compare(x)global r_al pena N_equ N_inequ;N_equ=1;N_inequ=3;h_inequ=zeros(3,1);[h,g]=constrains(x);%等式部分for i=1:1h_equ=abs(h(i));end%不等式部分for i=1:3h_inequ=abs(max(g(i),-r_al(i+1)/pena));endh1 = max(h_inequ);f= max(abs(h_equ),h1); %sqrt(h_equ+h_inequ);function [ x,fmin,k] =almain(x_al)%本程序为拉格朗日乘子算法示例算法%函数输入:% x_al:初始迭代点% r_al:初始拉格朗日乘子N-equ:等式约束个数N_inequ:不等式约束个数?%函数输出% X:最优函数点FVAL:最优函数值%============================程序开始================================ global r_al pena ; %参数(全局变量)pena=10; %惩罚系数r_al=[1,1,1,1];c_scale=2; %乘法系数乘数cta=0.5; %下降标准系数e_al=1e-4; %误差控制围max_itera=25;out_itera=1; %迭代次数%===========================算法迭代开始============================= while out_itera<max_iterax_al0=x_al;r_al0=r_al;%判断函数?compareFlag=compare(x_al0);%无约束的拟牛顿法BFGS[X,fmin]=fminunc(alobj,x_al0);x_al=X; %得到新迭代点%判断停止条件?if compare(x_al)<e_aldisp('we get the opt point');breakend%c判断函数下降度?if compare(x_al)<cta*compareFlagpena=1*pena; %可以根据需要修改惩罚系数变量elsepena=min(1000,c_scale*pena); %%乘法系数最大1000disp('pena=2*pena');end%%?更新拉格朗日乘子[h,g]=constrains(x_al);for i=1:1%%等式约束部分r_al(i)= r_al0(i)+pena*h(i);endfor i=1:3%%不等式约束部分r_al(i+1)=max(0,(r_al0(i+1)+pena*g(i)));endout_itera=out_itera+1;end%+++++++++++++++++++++++++++迭代结束+++++++++++++++++++++++++++++++++ disp('the iteration number');k=out_itera;disp('the value of constrains'); compare(x_al)disp('the opt point');x=x_al;fmin=obj(X);>> clear>> x_al=[0,0];>> [x,fmin,k]=almain(x_al)上机大作业3:1、>> clear alln=3; c=[-3,-1,-3]'; A=[2,1,1;1,2,3;2,2,1;-1,0,0;0,-1,0;0,0,-1];b=[2,5,6,0,0,0]'; cvx_beginvariable x(n)minimize( c'*x)subject toA*x<=bcvx_endCalling SDPT3 4.0: 6 variables, 3 equality constraints------------------------------------------------------------num. of constraints = 3dim. of linear var = 6*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 0.000 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime-------------------------------------------------------------------0|0.000|0.000|1.1e+01|5.1e+00|6.0e+02|-7.000000e+01 0.000000e+00| 0:0:00| chol 1 11|0.912|1.000|9.4e-01|4.6e-02|6.5e+01|-5.606627e+00 -2.967567e+01| 0:0:01| chol 1 12|1.000|1.000|1.3e-07|4.6e-03|8.5e+00|-2.723981e+00 -1.113509e+01| 0:0:01| chol 1 13|1.000|0.961|2.3e-08|6.2e-04|1.8e+00|-4.348354e+00 -6.122853e+00| 0:0:01| chol 1 14|0.881|1.000|2.2e-08|4.6e-05|3.7e-01|-5.255152e+00 -5.622375e+00| 0:0:01| chol 1 15|0.995|0.962|1.6e-09|6.2e-06|1.5e-02|-5.394782e+00 -5.409213e+00| 0:0:01| chol 1 16|0.989|0.989|2.7e-10|5.2e-07|1.7e-04|-5.399940e+00 -5.400100e+00| 0:0:01| chol 1 17|0.989|0.989|5.3e-11|5.8e-09|1.8e-06|-5.399999e+00 -5.400001e+00| 0:0:01| chol 1 18|1.000|0.994|2.8e-13|4.3e-11|2.7e-08|-5.400000e+00 -5.400000e+00| 0:0:01| stop: max(relative gap, infeasibilities) < 1.49e-08-------------------------------------------------------------------number of iterations = 8primal objective value = -5.39999999e+00dual objective value = -5.40000002e+00gap := trace(XZ) = 2.66e-08relative gap = 2.26e-09actual relative gap = 2.21e-09rel. primal infeas (scaled problem) = 2.77e-13rel. dual " " " = 4.31e-11rel. primal infeas (unscaled problem) = 0.00e+00rel. dual " " " = 0.00e+00norm(X), norm(y), norm(Z) = 4.3e+00, 1.3e+00, 1.9e+00norm(A), norm(b), norm(C) = 6.7e+00, 9.1e+00, 5.4e+00Total CPU time (secs) = 0.71CPU time per iteration = 0.09termination code = 0DIMACS: 3.6e-13 0.0e+00 5.8e-11 0.0e+00 2.2e-09 2.3e-09-------------------------------------------------------------------------------------------------------------------------------Status: SolvedOptimal value (cvx_optval): -5.42、>> clear alln=2; c=[-2,-4]'; G=[0.5,0;0,1]; A=[1,1;-1,0;0,-1]; b=[1,0,0]'; cvx_beginvariable x(n)minimize( x'*G*x+c'*x)subject toA*x<=bcvx_endCalling SDPT3 4.0: 7 variables, 3 equality constraintsFor improved efficiency, SDPT3 is solving the dual problem.------------------------------------------------------------num. of constraints = 3dim. of socp var = 4, num. of socp blk = 1dim. of linear var = 3******************************************************************* SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 0.000 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime-------------------------------------------------------------------0|0.000|0.000|8.0e-01|6.5e+00|3.1e+02| 1.000000e+01 0.000000e+00| 0:0:00| chol 1 1 1|1.000|0.987|4.3e-07|1.5e-01|1.6e+01| 9.043148e+00 -2.714056e-01| 0:0:00| chol 1 1 2|1.000|1.000|2.6e-07|7.6e-03|1.4e+00| 1.234938e+00 -5.011630e-02| 0:0:00| chol 1 1 3|1.000|1.000|2.4e-07|7.6e-04|3.0e-01| 4.166959e-01 1.181563e-01| 0:0:00| chol 1 1 4|0.892|0.877|6.4e-08|1.6e-04|5.2e-02| 2.773022e-01 2.265122e-01| 0:0:00| chol 1 1 5|1.000|1.000|1.0e-08|7.6e-06|1.5e-02| 2.579468e-01 2.427203e-01| 0:0:00| chol 1 1 6|0.905|0.904|3.1e-09|1.4e-06|2.3e-03| 2.511936e-01 2.488619e-01| 0:0:00| chol 1 1 7|1.000|1.000|6.1e-09|7.7e-08|6.6e-04| 2.503336e-01 2.496718e-01| 0:0:00| chol 1 1 8|0.903|0.903|1.8e-09|1.5e-08|1.0e-04| 2.500507e-01 2.499497e-01| 0:0:00| chol 1 19|1.000|1.000|4.9e-10|3.5e-10|2.9e-05| 2.500143e-01 2.499857e-01| 0:0:00| chol 1 1 10|0.904|0.904|4.7e-11|1.3e-10|4.4e-06| 2.500022e-01 2.499978e-01| 0:0:00| chol 2 2 11|1.000|1.000|2.3e-12|9.4e-12|1.2e-06| 2.500006e-01 2.499994e-01| 0:0:00| chol 2 2 12|1.000|1.000|4.7e-13|1.0e-12|1.8e-07| 2.500001e-01 2.499999e-01| 0:0:00| chol 2 2 13|1.000|1.000|2.0e-12|1.0e-12|4.2e-08| 2.500000e-01 2.500000e-01| 0:0:00| chol 2 2 14|1.000|1.000|2.6e-12|1.0e-12|7.3e-09| 2.500000e-01 2.500000e-01| 0:0:00|stop: max(relative gap, infeasibilities) < 1.49e-08-------------------------------------------------------------------number of iterations = 14primal objective value = 2.50000004e-01dual objective value = 2.49999996e-01gap := trace(XZ) = 7.29e-09relative gap = 4.86e-09actual relative gap = 4.86e-09rel. primal infeas (scaled problem) = 2.63e-12rel. dual " " " = 1.00e-12rel. primal infeas (unscaled problem) = 0.00e+00rel. dual " " " = 0.00e+00norm(X), norm(y), norm(Z) = 3.2e+00, 1.5e+00, 1.9e+00norm(A), norm(b), norm(C) = 3.9e+00, 4.2e+00, 2.6e+00Total CPU time (secs) = 0.36CPU time per iteration = 0.03termination code = 0DIMACS: 3.7e-12 0.0e+00 1.3e-12 0.0e+00 4.9e-09 4.9e-09------------------------------------------------------------------------------------------------------------------------------- Status: SolvedOptimal value (cvx_optval): -3。
大连理工大学概率上机作业第一次上机作业1.利用Matlab自带命令产生1000个均匀随机变量服从U(0,1)。
>> unifrnd(0,1,20,50)ans =Columns 1 through 100.8147 0.6557 0.4387 0.7513 0.3517 0.1622 0.1067 0.8530 0.7803 0.54700.9058 0.0357 0.3816 0.2551 0.8308 0.7943 0.9619 0.6221 0.3897 0.29630.1270 0.8491 0.7655 0.5060 0.5853 0.3112 0.0046 0.3510 0.2417 0.74470.9134 0.9340 0.7952 0.6991 0.5497 0.5285 0.7749 0.5132 0.4039 0.18900.6324 0.6787 0.1869 0.8909 0.9172 0.1656 0.8173 0.4018 0.0965 0.68680.0975 0.7577 0.4898 0.9593 0.2858 0.6020 0.8687 0.0760 0.1320 0.18350.2785 0.7431 0.4456 0.5472 0.7572 0.2630 0.0844 0.2399 0.9421 0.36850.5469 0.3922 0.6463 0.1386 0.7537 0.6541 0.3998 0.1233 0.9561 0.62560.9575 0.6555 0.7094 0.1493 0.3804 0.6892 0.2599 0.1839 0.5752 0.78020.9649 0.1712 0.7547 0.2575 0.5678 0.7482 0.8001 0.2400 0.0598 0.08110.1576 0.7060 0.2760 0.8407 0.0759 0.4505 0.4314 0.4173 0.2348 0.92940.9706 0.0318 0.6797 0.2543 0.0540 0.0838 0.9106 0.0497 0.3532 0.77570.9572 0.2769 0.6551 0.8143 0.5308 0.2290 0.1818 0.9027 0.8212 0.48680.4854 0.0462 0.1626 0.2435 0.7792 0.9133 0.2638 0.9448 0.0154 0.43590.8003 0.0971 0.1190 0.9293 0.9340 0.1524 0.1455 0.4909 0.0430 0.44680.1419 0.8235 0.4984 0.3500 0.1299 0.8258 0.1361 0.4893 0.1690 0.30630.4218 0.6948 0.9597 0.1966 0.5688 0.5383 0.8693 0.3377 0.6491 0.50850.9157 0.3171 0.3404 0.2511 0.4694 0.9961 0.5797 0.9001 0.7317 0.51080.7922 0.9502 0.5853 0.6160 0.0119 0.0782 0.5499 0.3692 0.6477 0.81760.9595 0.0344 0.2238 0.4733 0.3371 0.4427 0.1450 0.1112 0.4509 0.7948Columns 11 through 200.6443 0.3111 0.0855 0.0377 0.0305 0.0596 0.1734 0.9516 0.0326 0.25180.3786 0.9234 0.2625 0.8852 0.7441 0.6820 0.3909 0.9203 0.5612 0.29040.8116 0.4302 0.8010 0.9133 0.5000 0.0424 0.8314 0.0527 0.8819 0.61710.5328 0.1848 0.0292 0.7962 0.4799 0.0714 0.8034 0.7379 0.6692 0.26530.3507 0.9049 0.9289 0.0987 0.9047 0.5216 0.0605 0.2691 0.1904 0.82440.9390 0.9797 0.7303 0.2619 0.6099 0.0967 0.3993 0.42280.3689 0.98270.8759 0.4389 0.4886 0.3354 0.6177 0.8181 0.5269 0.5479 0.4607 0.73020.5502 0.1111 0.5785 0.6797 0.8594 0.8175 0.4168 0.9427 0.9816 0.3439 0.6225 0.2581 0.2373 0.1366 0.8055 0.7224 0.6569 0.4177 0.1564 0.5841 0.5870 0.4087 0.4588 0.7212 0.5767 0.1499 0.6280 0.9831 0.8555 0.1078 0.2077 0.5949 0.9631 0.1068 0.1829 0.6596 0.2920 0.3015 0.6448 0.9063 0.3012 0.2622 0.5468 0.6538 0.2399 0.5186 0.4317 0.7011 0.3763 0.8797 0.4709 0.6028 0.5211 0.4942 0.8865 0.9730 0.0155 0.6663 0.1909 0.8178 0.2305 0.7112 0.2316 0.7791 0.0287 0.6490 0.9841 0.5391 0.4283 0.2607 0.8443 0.2217 0.4889 0.7150 0.4899 0.8003 0.1672 0.6981 0.4820 0.5944 0.1948 0.1174 0.6241 0.9037 0.1679 0.4538 0.1062 0.6665 0.1206 0.0225 0.2259 0.2967 0.6791 0.8909 0.9787 0.4324 0.3724 0.1781 0.5895 0.4253 0.1707 0.3188 0.3955 0.3342 0.7127 0.8253 0.1981 0.1280 0.2262 0.3127 0.2277 0.4242 0.3674 0.6987 0.5005 0.0835 0.4897 0.9991 0.3846 0.1615 0.4357 0.5079 0.9880 0.1978 0.4711 0.1332 0.3395 0.1711 0.5830 0.1788Columns 21 through 300.4229 0.7788 0.2548 0.1759 0.6476 0.5822 0.4046 0.3477 0.8217 0.5144 0.0942 0.4235 0.2240 0.7218 0.6790 0.5407 0.4484 0.1500 0.4299 0.8843 0.5985 0.0908 0.6678 0.4735 0.6358 0.8699 0.3658 0.5861 0.8878 0.5880 0.4709 0.2665 0.8444 0.1527 0.9452 0.2648 0.7635 0.2621 0.3912 0.1548 0.6959 0.1537 0.3445 0.3411 0.2089 0.3181 0.6279 0.0445 0.7691 0.1999 0.6999 0.2810 0.7805 0.6074 0.7093 0.1192 0.7720 0.7549 0.3968 0.4070 0.6385 0.4401 0.6753 0.1917 0.2362 0.9398 0.9329 0.2428 0.8085 0.7487 0.0336 0.5271 0.0067 0.7384 0.1194 0.6456 0.9727 0.4424 0.7551 0.8256 0.0688 0.4574 0.6022 0.2428 0.6073 0.4795 0.1920 0.6878 0.3774 0.7900 0.3196 0.8754 0.3868 0.9174 0.4501 0.6393 0.1389 0.35920.2160 0.3185 0.5309 0.5181 0.9160 0.2691 0.4587 0.5447 0.6963 0.7363 0.7904 0.5341 0.6544 0.9436 0.0012 0.7655 0.6619 0.6473 0.0938 0.3947 0.9493 0.0900 0.4076 0.6377 0.4624 0.1887 0.7703 0.5439 0.5254 0.6834 0.3276 0.1117 0.8200 0.9577 0.4243 0.2875 0.3502 0.7210 0.5303 0.7040 0.6713 0.1363 0.7184 0.2407 0.4609 0.0911 0.6620 0.5225 0.8611 0.4423 0.4386 0.6787 0.9686 0.6761 0.7702 0.5762 0.4162 0.9937 0.4849 0.0196 0.8335 0.4952 0.5313 0.2891 0.3225 0.6834 0.8419 0.2187 0.3935 0.3309 0.7689 0.1897 0.3251 0.6718 0.7847 0.5466 0.8329 0.1058 0.6714 0.4243 0.1673 0.4950 0.1056 0.6951 0.4714 0.4257 0.2564 0.1097 0.7413 0.2703 0.8620 0.1476 0.6110 0.0680 0.0358 0.6444 0.6135 0.0636 0.5201 0.1971 0.9899 0.0550Columns 31 through 400.8507 0.7386 0.5523 0.1239 0.7378 0.5590 0.1781 0.8949 0.6311 0.6925 0.5606 0.5860 0.6299 0.4904 0.0634 0.8541 0.3596 0.0715 0.0899 0.5567 0.9296 0.2467 0.0320 0.8530 0.8604 0.3479 0.0567 0.2425 0.0809 0.3965 0.6967 0.6664 0.6147 0.8739 0.9344 0.4460 0.5219 0.0538 0.7772 0.0616 0.5828 0.0835 0.3624 0.2703 0.9844 0.0542 0.3358 0.4417 0.9051 0.78020.8154 0.6260 0.0495 0.2085 0.8589 0.1771 0.1757 0.0133 0.5338 0.33760.8790 0.6609 0.4896 0.5650 0.7856 0.6628 0.2089 0.8972 0.1092 0.60790.9889 0.7298 0.1925 0.6403 0.5134 0.3308 0.9052 0.1967 0.8258 0.74130.0005 0.8908 0.1231 0.4170 0.1776 0.8985 0.6754 0.0934 0.3381 0.10480.8654 0.9823 0.2055 0.2060 0.3986 0.1182 0.4685 0.3074 0.2940 0.12790.6126 0.7690 0.1465 0.9479 0.1339 0.9884 0.9121 0.45610.7463 0.54950.9900 0.5814 0.1891 0.0821 0.0309 0.5400 0.1040 0.1017 0.0103 0.48520.5277 0.9283 0.0427 0.1057 0.9391 0.7069 0.7455 0.9954 0.0484 0.89050.4795 0.5801 0.6352 0.1420 0.3013 0.9995 0.7363 0.3321 0.6679 0.79900.8013 0.0170 0.2819 0.1665 0.2955 0.2878 0.5619 0.2973 0.6035 0.73430.2278 0.1209 0.5386 0.6210 0.3329 0.4145 0.1842 0.0620 0.5261 0.05130.4981 0.8627 0.6952 0.5737 0.4671 0.4648 0.5972 0.2982 0.7297 0.07290.9009 0.4843 0.4991 0.0521 0.6482 0.7640 0.2999 0.0464 0.7073 0.08850.5747 0.8449 0.5358 0.9312 0.0252 0.8182 0.1341 0.5054 0.7814 0.79840.8452 0.2094 0.4452 0.7287 0.8422 0.1002 0.2126 0.7614 0.2880 0.9430Columns 41 through 500.6837 0.7894 0.1123 0.6733 0.0986 0.9879 0.5975 0.7593 0.8092 0.75190.1321 0.3677 0.7844 0.4296 0.1420 0.1704 0.3353 0.7406 0.7486 0.22870.7227 0.2060 0.2916 0.4517 0.1683 0.2578 0.2992 0.7437 0.1202 0.06420.1104 0.0867 0.6035 0.6099 0.1962 0.3968 0.4526 0.1059 0.5250 0.76730.1175 0.7719 0.9644 0.0594 0.3175 0.0740 0.4226 0.6816 0.3258 0.67120.6407 0.2057 0.4325 0.3158 0.3164 0.6841 0.3596 0.4633 0.5464 0.71520.3288 0.3883 0.6948 0.7727 0.2176 0.4024 0.5583 0.2122 0.3989 0.64210.6538 0.5518 0.7581 0.6964 0.2510 0.9828 0.7425 0.0985 0.4151 0.41900.7491 0.2290 0.4326 0.1253 0.8929 0.4022 0.4243 0.8236 0.1807 0.39080.5832 0.6419 0.6555 0.1302 0.7032 0.6207 0.4294 0.1750 0.2554 0.81610.7400 0.4845 0.1098 0.0924 0.5557 0.1544 0.1249 0.1636 0.0205 0.31740.2348 0.1518 0.9338 0.0078 0.1844 0.3813 0.0244 0.6660 0.9237 0.81450.7350 0.7819 0.1875 0.4231 0.2120 0.1611 0.2902 0.8944 0.6537 0.78910.9706 0.1006 0.2662 0.6556 0.0773 0.7581 0.3175 0.5166 0.9326 0.85230.8669 0.2941 0.7978 0.7229 0.9138 0.8711 0.6537 0.7027 0.1635 0.50560.0862 0.2374 0.4876 0.5312 0.7067 0.3508 0.9569 0.1536 0.9211 0.63570.3664 0.5309 0.7690 0.1088 0.5578 0.6855 0.9357 0.9535 0.7947 0.95090.3692 0.0915 0.3960 0.6318 0.3134 0.2941 0.4579 0.5409 0.5774 0.44400.6850 0.4053 0.2729 0.1265 0.1662 0.5306 0.2405 0.6797 0.4400 0.06000.5979 0.1048 0.0372 0.1343 0.6225 0.8324 0.7639 0.0366 0.2576 0.8667 2.参考课本综合例题2.5.4和2.5.5中的方法,模拟产生1000个随机变量,使其服从参数为2的指数分布,进而计算这1000个随机数的均值和方差。
大连理工大学C++程序设计大作业班级:111111111姓名:暗暗暗暗暗暗暗暗学号:1111111111邮箱:1111111111111111任课教师:赵国辉上交时间:2013.7.22目录1.第一次上机作业 (3)2.第二次上机作业 (6)3.第三次上机作业 (10)4.第四次上机作业 (15)5.结课大作业 (18)6.课堂总结 (48)1. 第一次上机作业1.1作业要求1.整型和浮点型的二进制表示2.一个整数的二进制(64位)转换成十进制表示。
1.2 核心代码说明整型和浮点型的二进制表示#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <stack>using namespace std;void getint(int x){stack<int>s;while(x){s.push(x&1);x>>=1;}while(!s.empty()){cout<<s.top();s.pop();}} //将整型转换成二进制函数void getfloat(float y){queue<int>q;int x=(int)y;getint(x);y-=x; //将浮点型转换成二进制函数if(!y) return ;putchar('.');while(y){y*=2.0;if(y>=1.0){q.push(1);y-=1.0;}else q.push(0);}while(!q.empty()){cout<<q.front();q.pop();}}int main(){int x;float y;cin>>x;//输入一个整数getint(x);cout<<endl;cin>>y;//输入一个浮点数getfloat(y);cout<<endl;return 0;}一个整数的二进制(64位)转换成十进制表示。
第一题Lagrange插值函数function y=lagrange(x0,y0,x);n=length(x0);m=length(x);for i=1:mz=x(i);s=0.0;for k=1:np=1.0;for j=1:nif j~=kp=p*(z-x0(j))/(x0(k)-x0(j));endends=p*y0(k)+s;endy(i)=s;endx0=[1:10];y0=[67.052,68.008,69.803,72.024,73.400,72.063,74.669,74.487,74.065,76 .777];lagrange(x0,y0,17)ans=-1.9516e+12x=[1:0.1:10];x=x';plot(x0,y0,'r');hold onplot(x,y,'k');legend('原函数','拟合函数')拟合图像如下拟合函数出现了龙格现象,运用多项式进行插值拟合时,效果并不好,高次多项式会因为误差的不断累积,导致龙格现象的发生。
第二题function fun =nihe(n)m=[67.052*10^6,68.008*10^6,69.803*10^6,72.024*10^6,73.400*10^6,72.063 *10^6,74.669*10^6,74.487*10^6,74.065*10^6,76.777*10^6];w=[1,2,3,4,5,6,7,8,9,10];d1=0;d2=0;d3=0;y1=polyfit(m,w,1);y2=polyfit(m,w,2);y3=polyfit(m,w,3);y2=poly2sym(s2);y3=poly2sym(s3);y4=poly2sym(s4);f1=subs(y1,17);f2=subs(y2,17);f3=subs(y3,17);for p=1:10;d1=d1+(subs(y1,w(p))-m(p))^2;d2=d2+(subs(y2,w(p))-m(p))^2;d3=d3+(subs(y3,w(p))-m(p))^2;endd1=sqrt(d1);d2=sqrt(d2);d3=sqrt(d3);fun=[f1 f2 f3;d2 d3 d4];return;结果三次函数的均方误差最小,拟合的最好。
大连理工大学优化方法上机作业本页仅作为文档页封面,使用时可以删除This document is for reference only-rar21year.March优化方法上机大作业学院:电子信息与电气工程学部姓名:学号:指导老师:上机大作业(一)%目标函数function f=fun(x)f=100*(x(2)-x(1)^2)^2+(1-x(1))^2;end%目标函数梯度function gf=gfun(x)gf=[-400*x(1)*(x(2)-x(1)^2)-2*(1-x(1));200*(x(2)-x(1)^2)]; End%目标函数Hess矩阵function He=Hess(x)He=[1200*x(1)^2-400*x(2)+2,-400*x(1);-400*x(1), 200;];end%线搜索步长function mk=armijo(xk,dk)beta=0.5; sigma=0.2;m=0; maxm=20;while (m<=maxm)if(fun(xk+beta^m*dk)<=fun(xk)+sigma*beta^m*gfun(xk)'*dk) mk=m; break;endm=m+1;endalpha=beta^mknewxk=xk+alpha*dkfk=fun(xk)newfk=fun(newxk)%最速下降法function [k,x,val]=grad(fun,gfun,x0,epsilon)%功能:梯度法求解无约束优化问题:minf(x)%输入:fun,gfun分别是目标函数及其梯度,x0是初始点,% epsilon为容许误差%输出:k是迭代次数,x,val分别是近似最优点和最优值maxk=5000; %最大迭代次数beta=0.5; sigma=0.4;k=0;while(k<maxk)gk=feval(gfun,x0); %计算梯度dk=-gk; %计算搜索方向if(norm(gk)<epsilon), break;end%检验终止准则m=0;mk=0;while(m<20) %用Armijo搜索步长if(feval(fun,x0+beta^m*dk)<=feval(fun,x0)+sigma*beta^m*gk'*dk) mk=m;break;endm=m+1;endx0=x0+beta^mk*dk;k=k+1;endx=x0;val=feval(fun,x0);>> x0=[0;0];>> [k,x,val]=grad('fun','gfun',x0,1e-4)迭代次数:k =1033x =0.99990.9998val =1.2390e-008%牛顿法x0=[0;0];ep=1e-4;maxk=10;k=0;while(k<maxk)gk=gfun(x0);if(norm(gk)<ep)x=x0miny=fun(x)k0=kbreak;elseH=inv(Hess(x0));x0=x0-H*gk;k=k+1;endendx =1.00001.0000miny =4.9304e-030迭代次数k0 =2%BFGS方法function [k,x,val]=bfgs(fun,gfun,x0,varargin) %功能:梯度法求解无约束优化问题:minf(x)%输入:fun,gfun分别是目标函数及其梯度,x0是初始点,% epsilon为容许误差%输出:k是迭代次数,x,val分别是近似最优点和最优值N=1000;epsilon=1e-4;beta=0.55;sigma=0.4;n=length(x0);Bk=eye(n);k=0;while(k<N)gk=feval(gfun,x0,varargin{:});if(norm(gk)<epsilon), break;enddk=-Bk\gk;m=0;mk=0;while(m<20)newf=feval(fun,x0+beta^m*dk,varargin{:});oldf=feval(fun,x0,varargin{:});if(newf<=oldf+sigma*beta^m*gk'*dk)mk=m;break;endm=m+1;endx=x0+beta^mk*dk;sk=x-x0;yk=feval(gfun,x,varargin{:})-gk;if(yk'*sk>0)Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);endk=k+1;x0=x;endval=feval(fun,x0,varargin{:});>> x0=[0;0];>> [k,x,val]=bfgs('fun','gfun',x0)k =20x =1.00001.0000val =2.2005e-011%共轭梯度法function [k,x,val]=frcg(fun,gfun,x0,epsilon,N)if nargin<5,N=1000;endif nargin<4, epsilon=1e-4;endbeta=0.6;sigma=0.4;n=length(x0);k=0;while(k<N)gk=feval(gfun,x0);itern=k-(n+1)*floor(k/(n+1));itern=itern+1;if(itern==1)dk=-gk;elsebetak=(gk'*gk)/(g0'*g0);dk=-gk+betak*d0; gd=gk'*dk;if(gd>=0),dk=-gk;endendif(norm(gk)<epsilon),break;endm=0;mk=0;while(m<20)if(feval(fun,x0+beta^m*dk)<=feval(fun,x0)+sigma*beta^m*gk'*dk) mk=m;break;endm=m+1;endx=x0+beta^m*dk;g0=gk; d0=dk;x0=x;k=k+1;endval=feval(fun,x);>> x0=[0;0];[k,x,val]=frcg('fun','gfun',x0,1e-4,1000)k =122x =1.00011.0002val =7.2372e-009上机大作业(二)%目标函数function f_x=fun(x)f_x=4*x(1)-x(2)^2-12;%等式约束条件function he=hf(x)he=25-x(1)^2-x(2)^2;end%不等式约束条件function gi_x=gi(x,i)switch icase 1gi_x=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34;case 2gi_x=x(1);case 3gi_x=x(2);otherwiseend%求目标函数的梯度function L_grad=grad(x,lambda,cigma)d_f=[4;2*x(2)];d_g(:,1)=[-2*x(1);-2*x(2)];d_g(:,2)=[10-2*x(1);10-2*x(2)];d_g(:,3)=[1;0];d_g(:,4)=[0;1];L_grad=d_f+(lambda(1)+cigma*hf(x))*d_g(:,1);for i=1:3if lambda(i+1)+cigma*gi(x,i)<0L_grad=L_grad+(lambda(i+1)+cigma*gi(x,i))*d_g(:,i+1);continueendend%增广拉格朗日函数function LA=lag(x,lambda,cee)LA=fun(x)+lambda(1)*hf(x)+0.5*cee*hf(x)^2;for i=1:3LA=LA+1/(2*cee)*(min(0,lambda(i+1)+cee*gi(x,i))^2-lambda(i+1)^2); endfunction xk=BFGS(x0,eps,lambda,cigma)gk=grad(x0,lambda,cigma);res_B=norm(gk);k_B=0;a_=1e-4;rho=0.5;c=1e-4;length_x=length(x0);I=eye(length_x);Hk=I;while res_B>eps&&k_B<=10000dk=-Hk*gk;m=0;while m<=5000if lag(x0+a_*rho^m*dk,lambda,cigma)-lag(x0,lambda,cigma)<=c*a_*rho^m*gk'*dkmk=m;break;endm=m+1;endak=a_*rho^mk;xk=x0+ak*dk;delta=xk-x0;y=grad(xk,lambda,cigma)-gk;Hk=(I-(delta*y')/(delta'*y))*Hk*(I-(y*delta')/(delta'*y))+(delta*delta')/(delta'*y);k_B=k_B+1;x0=xk;gk=y+gk;res_B=norm(gk);end%增广拉格朗日法function val_min=ALM(x0,eps)lambda=zeros(4,1);cigma=5;alpha=10;k=1;res=[abs(hf(x0)),0,0,0];for i=1:3res(1,i+1)=norm(min(gi(x0,i),-lambda(i+1)/cigma)); endres=max(res);while res>eps&&k<1000xk=BFGS(x0,eps,lambda,cigma);lambda(1)=lambda(1)+cigma*hf(xk);for i=1:3lambda(i+1)=lambda(i+1)+min(0,lambda(i+1)+gi(x0,1)); endk=k+1;cigma=alpha*cigma;x0=xk;res=[norm(hf(x0)),0,0,0];for i=1:3res(1,i+1)=norm(min(gi(x0,i),-lambda(i+1)/cigma)); endres=max(res);endval_min=fun(xk);fprintf('k=%d\n',k);fprintf('fmin=%.4f\n',val_min);fprintf('x=[%.4f;%.4f]\n',xk(1),xk(2));>> x0=[0;0];>> val_min=ALM(x0,1e-4)k=10fmin=-31.4003x=[1.0984;4.8779]val_min =-31.4003上机大作业(三)A=[1 1;-1 0;0 -1];n=2;b=[1;0;0];G=[0.5 0;0 2];c=[2 4];cvx_solver sdpt3cvx_beginvariable x(n)minimize (x'*G*x-c*x)subject toA*x<=bcvx_enddisp(x)Status: SolvedOptimal value (cvx_optval): -2.40.40000.6000A=[2 1 1;1 2 3;2 2 1;-1 0 0;0 -1 0;0 0 -1]; n=3;b=[2;5;6;0;0;0];C=[-3 -1 -3];cvx_solver sdpt3cvx_beginvariable x(n)minimize (C*x)subject toA*x<=bcvx_enddisp(x)Status: SolvedOptimal value (cvx_optval): -5.40.20000.00001.600011。
大连理工大学软件学院数据库第二次上机答案1.select course_idfrom sectiongroup by course_idhaving count (distinct year)>1select course.course_id , titlefrom section join course on section.course_id = course.course_id where year = '2010' and semester = 'spring' and section.course_id in (select course_idfrom sectiongroup by course_idhaving count (distinct year)>1)with c_2010 as (select course_idfrom sectionwhere year = '2010' and semester = 'spring'and course_id in (select course_idfrom sectiongroup by course_idhaving count (distinct year)>1)),ins_sla as(select course_id ,sum(salary)as sum_sal , count (teaches.id) as ins_cntfrom teaches join instructor on teaches.id = instructor.idwhere semester = 'spring' and year = '2010'and course_id in (select course_id from c_2010)group by course_id,sec_credit_hr as (select time_slot_id ,sum (((end_hr - start_hr)*60+ end_min-start_min)/50)as cred_hrfrom time_slotgroup by time_slot_id ),course_hr as (select course_id , cred_hrfrom section join sec_credit_hr on section.time_slot_id = sec_credit_hr.time_slot_idwhere semester = 'spring' and year = '2010'and course_id in (select course_id from c_2010))select course.course_id, title , cred_hr,sum_salfrom ins_sla join course_hr on ins_sla.course_id = course_hr.course_idjoin course on ins_sla.course_id= course.course_id2,course takes student sectionselect * from sectionselect name ,takes.course_id , takes.semester ,takes.year , takes.sec_id ,titlefrom student left join takes on student.id = takes.idleft join section on takes.course_id = section.course_idand takes.sec_id= section.sec_idand takes.semester= section.semesterand takes.year= section.yearfull join course on section.course_id = course.course_idselect * from takesselect count(*)from takeswhere grade = 'F' and id = '45678'select count (*)from takeswhere grade = 'F' and id in (select idfrom studentwhere dept_name in (select dept_name from student where id = '45678' ))select count (*)from takeswhere exists (select *from student as s join student as ton s.dept_name= t.dept_namewhere s.id = takes. id and t.id = '45678') and grade = 'F'select id ,name ,dept_name ,(select count(*)from takeswhere grade = 'F' and id = student.id)as fail ,(select count (*)from takeswhere grade = 'F' and id in (select idfrom student as s1where dept_name in (select dept_name from student as s2 where id = student.id ))) as all_failfrom student4.2??ˉ?éò?ó?not in ±íê?select distinct takes.idfrom instructor join teaches on instructor.id = teaches.idjoin takes on takes.course_id = teaches.course_idand takes.sec_id=teaches.sec_idand takes.semester = teaches.semesterand takes.year = teaches.yearwhere instructor.dept_name= '?ú1|?§?o'μ?μDT?ú1|?§?oà?ê|?ù?ìè?23ìμ??§éúwith stu_all_id as (select idfrom studentwhere not exists ((select course_idfrom teaches join instructor on instructor.id = teaches.id where dept_name= '?ú1|?§?o')except(select course_idfrom takeswhere takes.id = student.id and grade is not null )))select distinct student.id ,/doc/c612295375.html, , count (takes.course_id) from instructor join teaches on instructor.id = teaches.idjoin takes on takes.course_id = teaches.course_idand takes.sec_id=teaches.sec_idand takes.semester = teaches.semesterand takes.year = teaches.yearjoin student on student.id= takes.idwhere instructor.dept_name= '?ú1|?§?o'and student.id not in (select id from stu_all_id)group by student.id ,/doc/c612295375.html,5.select * from courseselect section.course_id, course.title,section.sec_id,section.semester,section.year from section join course on section.course_id = course.course_id where not exists (select * from takes where takes.course_id = section.course_idand takes.sec_id = section.sec_idand takes.semester = section.semesterand takes.year = section.year)and not exists (select * from teaches where teaches.course_id = section.course_idandteaches.sec_id = section.sec_idandteaches.semester = section.semesterand teaches.year = section.year)上一页下一页。
概率论与数理统计上机报告姓名:学号:院系:班级:一.某人写了n封信,又写了n个信封,然后将这n封信随机地装入n个信封中,用p(n)表示至少有一封信装对的概率。
1.编制程序,用随机数模拟至少20000次,求当n=10是,p(n)的值。
2.重复第一步,画出n=2,3,. . .,50时,p(n)的散点图。
解:1.假设n封信是数组a[],n个信封是数组b[],将a[]数组随机的与b[]数组形成映射,若a[]的下脚标与b[]的下脚标相等则输出1,否则输出0,重复20000次,计算输出值的和,并除以20000,结果既为概率运用C语言编程:#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int a[10],b[10],i,j,index,cishu=0;double percent;srand((unsigned)time(0));for(j=0;j<20000;j++){for(i=0;i<10;i++)a[i]=i;i=0;while(i<10){index=rand()%10;if(a[index]!=-1){b[i]=a[index];if(b[i]==i){cishu++;break;}a[index]=-1;i++;}}}percent=(double)(cishu)/20000;printf("%f\n",percent);}计算结果为2.思路同1:运用C语言编程:#include<stdio.h>#include<stdlib.h>#include<time.h>main(){int n;for(n=2;n<50;n++){int a[55],b[55],i,j,index,cishu=0;double percent;srand((unsigned)time(0));for(j=0;j<20000;j++){for(i=0;i<n;i++)a[i]=i;i=0;while(i<n){index=rand()%n;if(a[index]!=-1){b[i]=a[index];if(b[i]==i){cishu++;break;}a[index]=-1;i++;}}}percent=(double)(cishu)/20000;printf("%f\n",percent);}}运算结果为:0.496600 0.668400 0.630050 0.632700 0.6333500.630050 0.631450 0.640400 0.630500 0.6267000.636050 0.632700 0.623050 0.638350 0.6272000.626750 0.633050 0.631250 0.633700 0.6324000.632550 0.629900 0.634200 0.630050 0.6301000.638900 0.631000 0.632450 0.631250 0.6256000.631250 0.628750 0.633250 0.633450 0.6436500.631400 0.631100 0.634800 0.631900 0.6374000.632550 0.635100 0.636450 0.633850 0.6342000.628900 0.630700 0.628650 0.636900运用Excel作散点图:二.设X1,X2,. . .,Xn相互独立且都服从区间[0,1]的均匀分布,f(x)为区间[0,1]上的一个可积数,由大数定律可知依概率收敛于,编制程序,用随机数模拟至少40000次,近似地求下列两个积分的值:,设1~10十个数,a[0]~a[9]是个数组,然后随机的赋值,从a[0]开始盘多,如果数组值与组名对应的值相等则记1,否则记0,做20000次,最后用和除以20000,就得概率解题思路:取0~1内的随机数,赋值到x,在计算f(x),求40000个随机数对应的f(x)的和除以40000就可以了运用C语言编程:#include <stdio.h>#include <stdlib.h>#include <math.h>#include<time.h>main(){int i,j;double r,he=0;srand((unsigned)time(0));for(j=0;j<2;j++){i=0;while (i < 20000 ){r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );he=exp(r*r)+he;i = i + 1;}}printf("%f\n",he/40000);}运用c语言编程:#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>main(){int i,j;double r,he=0;srand((unsigned)time(0));for(j=0;j<2;j++){i=0;while (i < 20000 ){r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );if(r==0)r=0.000001;he=sin(r)/r+he;i = i++;}}printf("%f\n",he/40000);}三.假设男,女婴的出生率均为0.5,每个家庭只要有一个男婴出生就不再生下一胎,如果没有男婴,不论已经出生多少女婴,都可以继续要下一胎.假设某地有100万个家庭,按照上述生育政策,自行编制程序回答下一问题:1.每个家庭大约有几个小孩2.男女比例大约为多少3.如果男婴与女婴的出生率为0.51与0.49,重新回答前两问第一问和第二问程序如下:#include <stdio.h>#include <stdlib.h>void main(){int r;int B=0;int G=0;int i;float u,m;for(i=1;i<=200000;i++){while(B<i){int x=rand();r=x%100;if(r<50)B++;else G++;}}u=(B+G)/200000.0;printf("平均每对夫妻要有%.4f个孩子\n",u);m=(float)B/G;printf("男女比例为%.4f:1\n",m);}运算结果:第三问程序:#include <stdio.h>#include <stdlib.h>void main(){int r;int B=0;int G=0;int i;float u,m;for(i=1;i<=200000;i++){while(B<i){int x=rand();r=x%100;if(r<51)B++;else G++;}}u=(B+G)/200000.0;printf("平均每对夫妻要有%.4f个孩子\n",u);m=(float)B/G;printf("男女比例为%.4f:1\n",m);}运行结果如下:。
1.(1)题目描述:排序:使用起泡法对10个整数实现递减排序。
题目分析:将两个相邻数比较,大的调到前头。
采用两重循环。
有10个数,则要进行9趟比较,第i趟比较中要进行10-i次两两比较。
流程图:程序代码:#include<stdio.h>main(){int a[10],i,j,k,t;printf("please input ten numbers:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);for(k=1;k<10;k++)for(j=0;j<10-k;j++)if(a[j]<a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}printf("The sorted numbers:\n");for(i=0;i<10;i++)printf("%d ",a[i]);}程序截屏:1.(2)题目描述:排序:使用选择法对10个整数实现递减排序。
题目分析:以冒泡排序法为基础,在两两比较后并不马上进行交换,而是在找到最大的数之后,记住最大数的位置(数组中的下标),待一轮比较完毕后,再将最大的数一次交换到位。
编程时使用二重循环。
外循环控制进行N-1轮排序,内循环找出第i 轮的最大值。
流程图:程序代码:#include<stdio.h>main(){int a[10],i,j,k,m,t;printf("please input ten numbers:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);for(k=0;k<9;k++){t=k;for(j=k+1;j<10;j++)if(a[t]<a[j])t=j;{m=a[k];a[k]=a[t];a[t]=m;}}printf("The sorted numbers:\n");for(i=0;i<10;i++)printf("%d ",a[i]);}程序截屏:2.题目描述:统计:从键盘输入10个学生的数学(MT)、英语(EN)和物理(PH)成绩,并按照如下统计形式输出,包括学生学号(NO)、各科成绩、总成绩(SUM)、平均分(AVE)及是否每科都超过90分(‘Y’or ‘N’)题目分析:利用二维数组,输入十个学生的成绩。
共享知识分享快乐大连理工大学矩阵与数值分析上机作业课程名称:矩阵与数值分析研究生姓名:12 交作业日时间:日20 月年2016卑微如蝼蚁、坚强似大象.共享知识分享快乐第1题1.1程序:Clear ;all n=input('请输入向量的长度n:') for i=1:n;v(i)=1/i;endY1=norm(v,1)Y2=norm(v,2)Y3=norm(v,inf)1.2结果n=10 Y1 =2.9290Y2 =1.2449Y3 =1n=100 Y1 =5.1874Y2 =1.2787Y3 =1n=1000 Y1 =7.4855Y2 =1.2822Y3 =1N=10000 Y1 =9.7876Y2 =1.2825Y3 =11.3 分析一范数逐渐递增,随着n的增加,范数的增加速度减小;二范数随着n的增加,逐渐趋于定值,无群范数都是1.卑微如蝼蚁、坚强似大象.共享知识分享快乐第2题2.1程序;clear all x(1)=-10^-15;dx=10^-18;L=2*10^3; i=1:L fory1(i)=log(1+x(i))/x(i); d=1+x(i); d == 1ify2(i)=1;elsey2(i)=log(d)/(d-1);endx(i+1)=x(i)+dx;end x=x(1:length(x)-1););'r'plot(x,y1,on holdplot(x,y2);卑微如蝼蚁、坚强似大象.共享知识分享快乐2.2 结果2.3 分析红色的曲线代表未考虑题中算法时的情况,如果考虑题中的算法则数值大小始终为1,这主要是由于大数加小数的原因。
第3题3.1 程序;clear all A=[1 -18 144 -672 2016 -4032 5376 -4608 2304 -512];x=1.95:0.005:2.05; i=1:length(x);for y1(i)=f(A,x(i)); y2(i)=(x(i)-2)^9;end figure(3);plot(x,y1);;on hold卑微如蝼蚁、坚强似大象.共享知识分享快乐);'r'plot(x,y2,F.m文件y=f(A,x)function y=A(1); i=2:length(A);for y=x*y+A(i);;end3.2 结果第4题卑微如蝼蚁、坚强似大象.共享知识分享快乐4.1 程序;clear all n=input('请输入向量的长度n:')A=2*eye(n)-tril(ones(n,n),0); i=1:n for A(i,n)=1;end n=length(A);U=A; e=eye(n);for i=1:n-1[max_data,max_index]=max(abs(U(i:n,i))); e0=eye(n);max_index=max_index+i-1; U=e0*U; e1=eye(n); j=i+1:n fore1(j,i)=-U(j,i)/U(i,i);endU=e1*U;中把变换矩阵存到P P{i}=e0;% L{i}=e1; e=e1*e0*e;endk=1:n-2for Ldot{k}=L{k}; i=k+1:n-1forLdot{k}=P{i}*Ldot{k}*P{i};endend Ldot{n-1}=L{n-1};LL=eye(n);PP=eye(n); i=1:n-1for PP=P{i}*PP;LL=Ldot{i}*LL;endb=ones(n,2);解方程 %b=e*b;x=zeros(n,1);x(n)=b(n)/U(n,n); i=n-1:-1:1for卑微如蝼蚁、坚强似大象.共享知识分享快乐x(i)=(b(i)-U(i,:)*x)/U(i,i);end计算逆矩阵%X=U^-1*e^-1*eye(n);AN=X'; result2{n-4,1}=AN;result1{n-4,1}=x;,n)'%d:\n'fprintf(fprintf('%d ',AN);4.2 结果n=51.0625 -0.875 -0.75 -0.5 -0.0625-0.0625 0.0625 -0.75 1.125 -0.5-0.0625 0.125 0.0625 1.25 -0.5-0.0625 0.1250.25 0.06251.50.0625-0.5-0.25-0.0625 -0.125n=101.0625 -0.875 -0.75 -0.5 -0.0625 1.0625 -0.875 -0.75 -0.5 -0.0625 -0.0625 1.125 0.0625 -0.75 -0.5 -0.5 0.0625 1.125 -0.75 -0.0625 -0.0625 0.0625 0.125 1.25 1.25 -0.0625 -0.5 0.0625 0.125 -0.5-0.0625 0.250.250.0625 0.1251.5 1.5 -0.0625 0.1250.06250.0625 -0.0625 -0.125 -0.25 0.0625 -0.5 -0.0625 -0.125 -0.25 -0.5 -0.0625 -0.75 1.0625 -0.5 -0.0625 -0.875 -0.5 -0.75 1.0625 -0.875 -0.0625 -0.5 0.0625 1.125 -0.5 0.0625 1.125 -0.75 -0.0625 -0.75 1.25 0.125 0.0625 -0.0625 -0.0625 -0.5 -0.5 0.0625 0.125 1.250.25-0.0625 -0.0625 1.50.1250.0625 0.0625 0.250.1251.5-0.0625 -0.125 -0.25 0.0625-0.5 0.0625 -0.0625 -0.125 -0.25-0.5同样的方法可以算出n=20,n=30时的结果,这里就不罗列了。
学院:专业:班级:学号:姓名:上机大作业1:1.最速下降法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = steepest(x0,eps) gk = grad(x0);res = norm(gk);k = 0;while res > eps && k<=1000dk = -gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=steepest(x0,eps)2.牛顿法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad2(x)g = zeros(2,2);g(1,1)=2+400*(3*x(1)^2-x(2));g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = newton(x0,eps)gk = grad(x0);bk = [grad2(x0)]^(-1);res = norm(gk);k = 0;while res > eps && k<=1000dk=-bk*gk;xk=x0+dk;k = k+1;x0 = xk;gk = grad(xk);bk = [grad2(xk)]^(-1);res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x1=newton(x0,eps)--The 1-th iter, the residual is--The 2-th iter, the residual isx1 =法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star = bfgs(x0,eps) g0 = grad(x0);gk=g0;res = norm(gk);Hk=eye(2);k = 0;while res > eps && k<=1000dk = -Hk*gk;ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slopeak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;fa0=xk-x0;x0 = xk;go=gk;gk = grad(xk);y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa 0)')/((fa0)'*(y0));res = norm(gk);fprintf('--The %d-th iter, the residual is %f\n',k,res);endx_star = xk;End>> clear>> x0=[0,0]';>> eps=1e-4;>> x=bfgs(x0,eps)4.共轭梯度法:function f = fun(x)f = (1-x(1))^2 + 100*(x(2)-x(1)^2)^2; endfunction g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2)); g(2) = 200*(x(2)-x(1)^2);endfunction x_star =CG(x0,eps) gk = grad(x0);res = norm(gk);k = 0;dk = -gk;while res > eps && k<=1000 ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk);while f1 > f0 + *ak*slope ak = ak/4;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;g0=gk;gk = grad(xk);res = norm(gk);p=(gk/g0)^2;dk1=dk;dk=-gk+p*dk1;fprintf('--The %d-th iter, the residual is %f\n',k,res); endx_star = xk;end>> clear>> x0=[0,0]';>> eps=1e-4;>> x=CG(x0,eps)上机大作业2:function f= obj(x)f=4*x(1)-x(2)^2-12;endfunction [h,g] =constrains(x) h=x(1)^2+x(2)^2-25;g=zeros(3,1);g(1)=-10*x(1)+x(1)^2-10*x(2)+x(2)^2+34;g(2)=-x(1);g(3)=-x(2);endfunction f=alobj(x) %拉格朗日增广函数%N_equ等式约束个数?%N_inequ不等式约束个数N_equ=1;N_inequ=3;global r_al pena;%全局变量h_equ=0;h_inequ=0;[h,g]=constrains(x);%等式约束部分?for i=1:N_equh_equ=h_equ+h(i)*r_al(i)+(pena/2)*h(i).^2;end%不等式约束部分for i=1:N_inequh_inequ=h_inequ+pena)*(max(0,(r_al(i)+pena*g(i))).^2-r_al(i).^2); end%拉格朗日增广函数值f=obj(x)+h_equ+h_inequ;function f=compare(x)global r_al pena N_equ N_inequ;N_equ=1;N_inequ=3;h_inequ=zeros(3,1);[h,g]=constrains(x);%等式部分for i=1:1h_equ=abs(h(i));end%不等式部分for i=1:3h_inequ=abs(max(g(i),-r_al(i+1)/pena));endh1 = max(h_inequ);f= max(abs(h_equ),h1); %sqrt(h_equ+h_inequ);function [ x,fmin,k] =almain(x_al)%本程序为拉格朗日乘子算法示例算法%函数输入:% x_al:初始迭代点% r_al:初始拉格朗日乘子N-equ:等式约束个数N_inequ:不等式约束个数?%函数输出% X:最优函数点FVAL:最优函数值%============================程序开始================================ global r_al pena ; %参数(全局变量)pena=10; %惩罚系数r_al=[1,1,1,1];c_scale=2; %乘法系数乘数cta=; %下降标准系数e_al=1e-4; %误差控制范围max_itera=25;out_itera=1; %迭代次数%===========================算法迭代开始============================= while out_itera<max_iterax_al0=x_al;r_al0=r_al;%判断函数?compareFlag=compare(x_al0);%无约束的拟牛顿法BFGS[X,fmin]=fminunc(@alobj,x_al0);x_al=X; %得到新迭代点%判断停止条件?if compare(x_al)<e_aldisp('we get the opt point');breakend%c判断函数下降度?if compare(x_al)<cta*compareFlagpena=1*pena; %可以根据需要修改惩罚系数变量elsepena=min(1000,c_scale*pena); %%乘法系数最大1000disp('pena=2*pena');end%%?更新拉格朗日乘子[h,g]=constrains(x_al);for i=1:1%%等式约束部分r_al(i)= r_al0(i)+pena*h(i);endfor i=1:3%%不等式约束部分r_al(i+1)=max(0,(r_al0(i+1)+pena*g(i)));endout_itera=out_itera+1;end%+++++++++++++++++++++++++++迭代结束+++++++++++++++++++++++++++++++++ disp('the iteration number');k=out_itera;disp('the value of constrains'); compare(x_al)disp('the opt point');x=x_al;fmin=obj(X);>> clear>> x_al=[0,0];>> [x,fmin,k]=almain(x_al)上机大作业3: 1、>> clear alln=3; c=[-3,-1,-3]'; A=[2,1,1;1,2,3;2,2,1;-1,0,0;0,-1,0;0,0,-1];b=[2,5,6,0,0,0]';cvx_beginvariable x(n)minimize( c'*x)subject toA*x<=bcvx_endCalling SDPT3 : 6 variables, 3 equality constraints------------------------------------------------------------num. of constraints = 3dim. of linear var = 6*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime -------------------------------------------------------------------0|||+01|+00|+02|+01 +00| 0:0:00| chol 1 11|||||+01|+00 +01| 0:0:01| chol 1 12|||||+00|+00 +01| 0:0:01| chol 1 13|||||+00|+00 +00| 0:0:01| chol 1 14||||||+00 +00| 0:0:01| chol 1 15||||||+00 +00| 0:0:01| chol 1 16||||||+00 +00| 0:0:01| chol 1 17||||||+00 +00| 0:0:01| chol 1 18||||||+00 +00| 0:0:01|stop: max(relative gap, infeasibilities) <------------------------------------------------------------------- number of iterations = 8primal objective value = +00dual objective value = +00gap := trace(XZ) =relative gap =actual relative gap =rel. primal infeas (scaled problem) =rel. dual " " " =rel. primal infeas (unscaled problem) = +00rel. dual " " " = +00norm(X), norm(y), norm(Z) = +00, +00, +00norm(A), norm(b), norm(C) = +00, +00, +00Total CPU time (secs) =CPU time per iteration =termination code = 0DIMACS: +00 +00-------------------------------------------------------------------------------------------------------------------------------Status: SolvedOptimal value (cvx_optval):2、>> clear alln=2; c=[-2,-4]'; G=[,0;0,1]; A=[1,1;-1,0;0,-1]; b=[1,0,0]'; cvx_beginvariable x(n)minimize( x'*G*x+c'*x)subject toA*x<=bcvx_endCalling SDPT3 : 7 variables, 3 equality constraintsFor improved efficiency, SDPT3 is solving the dual problem.------------------------------------------------------------num. of constraints = 3dim. of socp var = 4, num. of socp blk = 1dim. of linear var = 3*******************************************************************SDPT3: Infeasible path-following algorithms*******************************************************************version predcorr gam expon scale_dataNT 1 1 0it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime -------------------------------------------------------------------0||||+00|+02| +01 +00| 0:0:00| chol 1 11|||||+01| +00 | 0:0:00| chol 1 12|||||+00| +00 | 0:0:00| chol 1 13|||||| | 0:0:00| chol 1 14|||||| | 0:0:00| chol 1 15|||||| | 0:0:00| chol 1 16|||||| | 0:0:00| chol 1 17|||||| | 0:0:00| chol 1 18|||||| | 0:0:00| chol 1 19|||||| | 0:0:00| chol 1 110|||||| | 0:0:00| chol 2 211|||||| | 0:0:00| chol 2 212|||||| | 0:0:00| chol 2 213|||||| | 0:0:00| chol 2 214|||||| | 0:0:00|stop: max(relative gap, infeasibilities) <------------------------------------------------------------------- number of iterations = 14primal objective value =dual objective value =gap := trace(XZ) =relative gap =actual relative gap =rel. primal infeas (scaled problem) =rel. dual " " " =rel. primal infeas (unscaled problem) = +00rel. dual " " " = +00norm(X), norm(y), norm(Z) = +00, +00, +00norm(A), norm(b), norm(C) = +00, +00, +00Total CPU time (secs) =CPU time per iteration =termination code = 0DIMACS: +00 +00-------------------------------------------------------------------------------------------------------------------------------Status: SolvedOptimal value (cvx_optval): -3。