MATLAB实验上机作业
clearx = input('x=');if x>=100 | x<=0disp('jujueyoudi')elseif x<=2y = 10elseif x<=70y = 3.75*xelse x<= 100;y = (x-70)+70*3.75disp(['yun fei shi:' num2str(y)]); end结果:x=-2jujueyoudix=120jujueyoudix=2y =10x=45y =168.7500x=60y =225x=90y =282.5000yun fei shi:282.5x=input('x=');y=input('y=');if x>=0 & y>=0fun=x+yelseif x>=0 & y<0fun=x+y.^2elseif x<0 & y>=0fun=x.^2+yelse x<0 & y<0;fun=x.^2+y.^2;disp(['the jie guo is ' num2str(fun)]); end结果:x=5y=5fun =10x=5y=-5fun =30x=-5y=5fun =30x=-5y=-5fun =50the jie guo is 50>>value=input('value=')switch(value)case'Sunday',disp('di yi tian');case'Monday',disp('di er tian');case'Tuesday'disp('di san tian');case'Wednesday',disp('di si tian');case'Thursday',disp('di wu tian');case'Friday',disp('di liu tian');case'Saturday',disp('di qi tian');otherwise,disp('The value is out of range.'); endvalue=input('value=')结果:value='Sunday'value =Sundaydi yi tian第十二题:ebuseno=[0,0.25,0.5];p=1E+6;sieta=0:0.1:2*pi;r1=p./(1-ebuseno(1)*cos(sieta));r2=p./(1-ebuseno(2)*cos(sieta));r3=p./(1-ebuseno(3)*cos(sieta));minjuli1=min(r1)maxjuli1=max(r1)minjuli2=min(r2)maxjuli2=max(r2)minjuli3=min(r3)maxjuli3=max(r3)figuresubplot(1,3,1);plot(sieta,r1);subplot(1,3,2);plot(sieta,r2);subplot(1,3,3);plot(sieta,r3);figuresubplot(1,3,1);polar(sieta,r1);title('sieta=0')subplot(1,3,2);polar(sieta,r2);title('sieta=0.25') subplot(1,3,3);polar(sieta,r3);title('sieta=0.5')结果:minjuli1 =1000000 maxjuli1 =1000000 minjuli2 =8.0014e+005 maxjuli2 =1.3333e+006 minjuli3 =6.6686e+005 maxjuli3 =2000000图像:第十三题:disp('This program calculates the day of year given the ');51011111111111x 1065100.80.911.11.21.31.4x 10605100.60.811.21.41.61.82x 106sieta=0sieta=0.25sieta=0.5disp('current date.');month = input('Enter current month (1-12):');day = input('Enter current day(1-31):');year = input('Enter current year(yyyy): ');if mod(year,400) == 0leap_day = 1; % Years divisible by 400 are leap yearselseif mod(year,100) == 0leap_day = 0; % Other centuries are not leap yearselseif mod(year,4) == 0leap_day = 1; % Otherwise every 4th year is a leap yearelseleap_day = 0; % Other years are not leap yearsend% Calculate day of year by adding current day to the% days in previous months.day_of_year = day;for ii = 1:month - 1% Add days in months from January to last monthswitch (ii)case {1,3,5,7,8,10,12},day_of_year = day_of_year + 31;case {4,6,9,11},day_of_year = day_of_year + 30;case 2,day_of_year = day_of_year + 28 + leap_day;endend% Tell userfprintf('The date %2d/%2d/%4d is day of year %d.\n', ...month, day, year, day_of_year);结果:This program calculates the day of year given the current date.Enter current month (1-12):5Enter current day(1-31):23Enter current year(yyyy): 2012The date 5/23/2012 is day of year 144.>>第十四题:clearjd = pi / 180;g = -9.81;vo = 20;spwy = zeros(1,91);for ii = 1:91;theta = ii - 1;vxo=vo * cos(theta*jd);vyo=vo * sin(theta*jd);ydsj = -2 * vyo / g;spwy(ii) = vxo * ydsj;endfprintf ('spwy versus angle theta:\n');for ii = 1:91;theta = ii - 1;fprintf (' %2d %8.4f\n',theta, spwy(ii));end[maxspwy,xb]=max(spwy);maxangle=xb-1;fprintf ('\nMax spwy is %8.4f at %2d degrees.\n',maxspwy, maxangle); for ii=5:10:85;theta=ii;vxo=vo * cos(theta*jd);vyo=vo * sin(theta*jd);ydsj = -2 * vyo / g;x=zeros(1,21);y=zeros(1,21);for jj=1:21time=(jj-1)*ydsj/20;x(jj)=vxo*time;y(jj)=vyo*time+0.5*g*time^2;endplot(x,y,'b')if ii==5hold onendendtitle ('\bfTrajectory of Ball vs Initial Angle \theta');xlabel ('\bf\itx \rm\bf(meters)');ylabel ('\bf\ity \rm\bf(meters)');axis ([0 45 0 25]);grid on;vxo = vo * cos(maxangle*jd);vyo = vo * sin(maxangle*jd);ydsj = -2 * vyo / g;x = zeros(1,21);y = zeros(1,21);for jj = 1:21time = (jj - 1) * ydsj/20;x(jj) = vxo * time;y(jj) = vyo * time + 0.5 * g * time^2; endplot(x,y,'r','LineWidth',3.0);hold off结果:spwy versus angle theta:0 0.00001 1.42302 2.84433 4.26214 5.67475 7.08056 8.47757 9.86438 11.23909 12.600110 13.945811 15.274512 16.584613 17.874514 19.142615 20.387416 21.607317 22.800918 23.966819 25.103420 26.209521 27.283622 28.324523 29.330924 30.301525 31.235226 32.130927 32.987428 33.803829 34.578930 35.311931 36.001932 36.648133 37.249634 37.805735 38.315736 38.779137 39.195238 39.563539 39.883740 40.155341 40.377942 40.551443 40.675444 40.749945 40.774746 40.749947 40.675448 40.551449 40.377950 40.155351 39.883752 39.563553 39.195254 38.779155 38.315756 37.805757 37.249658 36.648159 36.001960 35.311961 34.578962 33.803863 32.987464 32.130965 31.235266 30.301567 29.330968 28.324569 27.283670 26.209571 25.103472 23.966873 22.800974 21.607375 20.387476 19.142677 17.874578 16.584679 15.2745 80 13.9458 81 12.6001 82 11.2390 83 9.8643 84 8.4775 85 7.0805 86 5.6747 87 4.2621 88 2.8443 89 1.4230 90 0.0000Max spwy is 40.7747 at 45 degrees.第十五题: 第十六题:function r=rayleigh(n,m) r=zeros(n,m); for i=1:n; for j=1:m;r(i,j)=sqrt(randn^2+randn^2); end endpjs=mean(r);051015202530354045510152025Trajectory of Ball vs Initial Anglex (meters)y (m e t e r s)bzc=std(r);disp(pjs);disp(bzc);bar(r)end结果:r=rayleigh(5,5)1.237008544208337 1.2210028556248091.894261809458986 0.653624461565775 1.8512612446233900.769641776477824 0.5078782482954100.888916492607048 0.562346849843642 0.768577159863762 r =0.603316330745527 0.9725983124165462.612901934895278 0.130785404121743 1.229697074077176 2.009103596566964 1.8204226973486482.706325214453846 0.5124592131146313.119426550594204 0.282501143923952 1.1425531027326362.112694********* 1.047041785416552 1.705010275996277 1.392474641232152 1.6173794226206361.470996331580991 1.413522532552441 1.934022829940053 1.897647008573091 0.5520607430055800.568391135249283 0.164313372623507 1.268149492509238第十七题:function [mag,theta]=topolar(x)mag=abs(x);theta=angle(x)/pi*180;fprintf('mo %2d jiaodu %4f\n',mag,theta); end结果:topolar(3+4*i)mo 5 jiaodu 53.130102第十八题:s=-1:0.01:1;t=[-1:0.01:1]*pi;[x,y]=meshgrid(s,t);z=abs(exp(x+i*y));figure;mesh(x,y,z);figure;meshz(x,y,z)figure;contour(x,y,z)figure;contourf(x,y,z)figure;stem3(x,y,z)结果;s=-1:0.01:1;t=[-1:0.01:1]*pi; [x,y]=meshgrid(s,t); z=abs(exp(x+i*y)); figure;mesh(x,y,z);figure;meshz(x,y,z)figure;contour(x,y,z) figure;contourf(x,y,z) figure;stem3(x,y,z)。
MATLAB上机内容及作业
MATLAB上机内容及作业无约束优化求解函数fminsearch和fminunc求解无约束非线性优化问题的函数有fminsearch 函数、fminunc 函数。
函数fminsearch和fminunc功能相同,但fminunc函数可以得到目标函数在最优解处的梯度和Hessian矩阵值。
无约束优化数学模型为:min f(X) X∈R n求解无约束非线性优化问题的步骤为:第一步:先编写目标函数的M文件;第二步:再在命令窗口中调用相应的优化函数。
1、fminsearch函数调用格式为[x, fval]=fminsearch(@myfun, x0)输出参数的含义:x:返回最优解的设计变量的值;fval:在最优设计变量值时,目标函数的最小值;exitflag:返回算法终止的标志,有以下几种情况,>0 表示算法收敛于最优解处;=0 表示算法已经达到迭代的最大次数;<0 表示算法不收敛。
output:返回优化算法信息的一个数据结构,有以下信息:output.iteration 表示迭代次数output.algorithm 表示所采用的算法output.funcCount 表示函数评价次数输入参数的含义:@myfun:目标函数的M文件,在其前要加“@”,或表示为'myfun' ,myfun自己可以任意命名;x0:在调用该优化函数时,需要先对设计变量赋一个初始值;2、fminunc函数的调用格式[x, fval]=fminunc (@myfun, x0)grad:返回目标函数在最优解处的梯度信息;hessian:返回目标函数在最优解处的hessian矩阵信息。
其余含义同上。
3、实例已知某一优化问题的数学模型为:min f(X)=3x12+2x1x2+x22X∈R n用MA TLAB程序编写的代码为:第一步:首先编写目标函数的.m文件,并保存为examplefsearch.m文件(先单击file菜单,后点击New 命令中的M—file,即可打开M文件编辑窗口进行代码的编辑,在英文状态下输入程序代码),代码为:function f=examplefsearch(x)f=3*x(1)^ 2+2*x(1)*x(2)+x(2)^2;第二步:在Command窗口中调用fminsearch函数,代码为:x0=[1;1]; %赋初值[x,fval]=fminsearch(@examplefsearch,x0) %回车即可调用fminsearch函数,得到结果输出最优解结果为:x=1.0e-0.08* -0.7914 0.2260 %分别为x1和x2的最优点的值(近似为0)fval=1.5722e-016 %对应最优点的最优目标函数值(近似为0)4、作业已知几个优化问题的数学模型分别为:(1)min f(X)=0.1935x1 x22 x32(4+6x4) X∈R4(2)min f(X)= (x13+cos x2+log x3)/ e x1 X∈R3(3)min f(X)=2x13+4x1x23 -10x1x2+x33X∈R3试用MATLAB编程分别求解上述优化问题的最优解。
Matlab上机实验(第一次和第二次)
Matlab 上机实验一、二1.安装Matlab 软件。
2.验证所学内容和教材上的例子。
3.求下列联立方程的解⎪⎩⎪⎪⎨⎧=+-+-=-+=++-=--+41025695842475412743w z y x w z x w z y x w z y x >> a=[3 4 -7 -12;5 -7 4 2;1 0 8 -5;-6 5 -2 10];>> b=[4;4;9;4];>> c=a\bc =5.22264.45701.47181.59944.设⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡------=81272956313841A ,⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡-----=793183262345B ,求C1=A*B’;C2=A’*B;C3=A.*B,并求上述所有方阵的逆阵。
>> A=[1 4 8 13;-3 6 -5 -9;2 -7 -12 -8];>> B=[5 4 3 -2;6 -2 3 -8;-1 3 -9 7];>> C1=A*B'C1 =19 -82 3012 27 3-38 54 29>> C2=A'*BC2 =-15 16 -24 3663 -17 93 -10522 6 117 -6019 46 84 -10>> C3=A.*BC3 =5 16 24 -26-18 -12 -15 72-2 -21 108 -56>> inv(C1)ans =0.0062 0.0400 -0.0106-0.0046 0.0169 0.00300.0168 0.0209 0.0150>> inv(C2)Warning: Matrix is close to singular or badly scaled.Results may be inaccurate. RCOND = 8.997019e-019.ans =1.0e+015 *-0.9553 -0.2391 -0.1997 0.27000.9667 0.2420 0.2021 -0.2732-0.4473 -0.1120 -0.0935 0.1264-1.1259 -0.2818 -0.2353 0.31825.设 ⎥⎦⎤⎢⎣⎡++=)1(sin 35.0cos 2x x x y ,把x=0~2π间分为101点,画出以x 为横坐标,y 为纵坐标的曲线。
MATLAB课后上机实验报告
《MATLAB程序设计与应用》实验指导书实验一 matlab 集成环境使用与运算基础1,先求下列表达式的值,然后显示matlab 工作空间的使用情况并保存全部变量。
(1)0122sin851z e =+程序:.>> z1=2*sin(85*pi/180)/(1+exp(2)) 结果: z1 =0.2375(2)22121(0.4552i z In x x +⎡⎤=+=⎢⎥-⎣⎦其中 程序:>> x=[2,1+2*i;-0.45,5];>> z2=0.5*log(x+sqrt(1+x*x)) 结果: z2 =0.7114 - 0.0253i 0.8968 + 0.3658i 0.2139 + 0.9343i 1.1541 - 0.0044i(3)0.3,9.2,8.2,...,8.2,9.2,0.3,23.0)3.0sin(23.03.03---=+++-=-a aIn a e e z a a 提示:利用冒号表达式生成a 向量,求各点函数值时用点乘运算。
程序:>> a=-3.0:0.1:30;>> z3=(exp(0.3*a)-exp(-0.3*a))/2.*sin((a+0.3)*pi/180)+log((0.3+a)/2) 结果: z3 =1.0e+003 *Columns 1 through 40.0003 + 0.0031i 0.0003 + 0.0031i 0.0003 + 0.0031i 0.0002 + 0.0031iColumns 5 through 80.0002 + 0.0031i 0.0001 + 0.0031i 0.0001 + 0.0031i 0.0000 + 0.0031i Columns 9 through 12-0.0000 + 0.0031i -0.0001 + 0.0031i -0.0001 + 0.0031i -0.0002 + 0.0031i Columns 13 through 16-0.0003 + 0.0031i -0.0003 + 0.0031i -0.0004 + 0.0031i -0.0005 + 0.0031i Columns 17 through 20-0.0006 + 0.0031i -0.0007 + 0.0031i -0.0008 + 0.0031i -0.0009 + 0.0031i Columns 21 through 24-0.0010 + 0.0031i -0.0012 + 0.0031i -0.0014 + 0.0031i -0.0016 + 0.0031i Columns 25 through 28-0.0019 + 0.0031i -0.0023 + 0.0031i -0.0030 + 0.0031i -0.0370 Columns 29 through 32-0.0030 -0.0023 -0.0019 -0.0016 Columns 33 through 36-0.0014 -0.0012 -0.0010 -0.0009 Columns 37 through 40-0.0008 -0.0007 -0.0006 -0.0005 Columns 41 through 44-0.0004 -0.0003 -0.0003 -0.0002 Columns 45 through 48-0.0001 -0.0001 -0.0000 0.0000 Columns 49 through 520.0001 0.0001 0.0002 0.0002 Columns 53 through 560.0003 0.0003 0.0003 0.0004 Columns 57 through 600.0004 0.0005 0.0005 0.0005 Columns 61 through 640.0006 0.0006 0.0006 0.0007 Columns 65 through 680.0007 0.0007 0.0008 0.0008 Columns 69 through 720.0008 0.0008 0.0009 0.0009 Columns 73 through 760.0009 0.0010 0.0010 0.0010 Columns 77 through 800.0011 0.0011 0.0011 0.0011 Columns 81 through 840.0012 0.0012 0.0012 0.0013 Columns 85 through 880.0013 0.0013 0.0013 0.0014 Columns 89 through 920.0014 0.0014 0.0015 0.0015 Columns 93 through 960.0015 0.0016 0.0016 0.0016 Columns 97 through 1000.0017 0.0017 0.0017 0.0018 Columns 101 through 1040.0018 0.0018 0.0019 0.0019 Columns 105 through 1080.0020 0.0020 0.0020 0.0021 Columns 109 through 1120.0021 0.0022 0.0022 0.0023 Columns 113 through 1160.0023 0.0024 0.0024 0.0025 Columns 117 through 1200.0025 0.0026 0.0026 0.0027 Columns 121 through 1240.0027 0.0028 0.0029 0.0029 Columns 125 through 1280.0030 0.0031 0.0031 0.0032 Columns 129 through 1320.0033 0.0034 0.0034 0.0035 Columns 133 through 1360.0036 0.0037 0.0038 0.0039 Columns 137 through 1400.0040 0.0041 0.0042 0.0043 Columns 141 through 1440.0044 0.0045 0.0046 0.0047 Columns 145 through 1480.0049 0.0050 0.0051 0.0053 Columns 149 through 1520.0054 0.0056 0.0057 0.0059 Columns 153 through 1560.0060 0.0062 0.0064 0.0066 Columns 157 through 1600.0068 0.0069 0.0071 0.0074 Columns 161 through 1640.0076 0.0078 0.0080 0.0083 Columns 165 through 1680.0085 0.0088 0.0090 0.0093 Columns 169 through 1720.0096 0.0099 0.0102 0.0105 Columns 173 through 1760.0108 0.0112 0.0115 0.0119 Columns 177 through 1800.0123 0.0127 0.0131 0.0135 Columns 181 through 1840.0139 0.0144 0.0148 0.0153 Columns 185 through 1880.0158 0.0163 0.0168 0.0174 Columns 189 through 1920.0180 0.0185 0.0191 0.0198 Columns 193 through 1960.0204 0.0211 0.0218 0.0225 Columns 197 through 2000.0233 0.0241 0.0249 0.0257 Columns 201 through 2040.0265 0.0274 0.0284 0.0293 Columns 205 through 2080.0303 0.0313 0.0324 0.0335 Columns 209 through 2120.0346 0.0358 0.0370 0.0382 Columns 213 through 2160.0395 0.0409 0.0423 0.0437 Columns 217 through 2200.0452 0.0467 0.0483 0.0500 Columns 221 through 2240.0517 0.0534 0.0552 0.0571 Columns 225 through 2280.0591 0.0611 0.0632 0.0654 Columns 229 through 2320.0676 0.0699 0.0723 0.0748 Columns 233 through 2360.0773 0.0800 0.0827 0.0856 Columns 237 through 2400.0885 0.0915 0.0947 0.0979 Columns 241 through 2440.1013 0.1047 0.1083 0.1121 Columns 245 through 2480.1159 0.1199 0.1240 0.1282 Columns 249 through 2520.1326 0.1372 0.1419 0.1467 Columns 253 through 2560.1518 0.1570 0.1624 0.1679 Columns 257 through 2600.1737 0.1796 0.1858 0.1921 Columns 261 through 2640.1987 0.2055 0.2125 0.2198 Columns 265 through 2680.2273 0.2351 0.2431 0.2514 Columns 269 through 2720.2600 0.2689 0.2781 0.2876 Columns 273 through 2760.2974 0.3076 0.3180 0.3289 Columns 277 through 2800.3401 0.3517 0.3637 0.3761 Columns 281 through 2840.3889 0.4021 0.4158 0.4299 Columns 285 through 2880.4446 0.4597 0.4753 0.4915 Columns 289 through 2920.5082 0.5254 0.5433 0.5617 Columns 293 through 2960.5807 0.6004 0.6208 0.6418 Columns 297 through 3000.6636 0.6861 0.7093 0.7333 Columns 301 through 3040.7581 0.7838 0.8103 0.8377 Columns 305 through 3080.8660 0.8952 0.9254 0.9567 Columns 309 through 3120.9890 1.0223 1.0568 1.0924 Columns 313 through 3161.1292 1.1673 1.2066 1.2472Columns 317 through 3201.2892 1.3326 1.3774 1.4237Columns 321 through 3241.4715 1.5210 1.5721 1.6249Columns 325 through 3281.6794 1.7357 1.7940 1.8541Columns 329 through 3311.9163 1.98052.0468(4)⎪⎩⎪⎨⎧=<≤<≤<≤+--=5.2:5.0:0,322110,121,2224t t t t t t t t z 其中提示:用逻辑表达式求分段函数值。
MATLAB上机作业
MA TLAB上机作业学院名称:专业班级:学生姓名:学生学号:年月作业11.用MATLAB可以识别的格式输入下面两个矩阵12342357135732391894A ⎛⎫⎪⎪ ⎪= ⎪⎪ ⎪⎝⎭,144367723355422675342189543i iB i +⎛⎫⎪+⎪= ⎪+ ⎪ ⎪⎝⎭再求出它们的乘积矩阵C ,并将C 矩阵的右下角23⨯子矩阵赋给D 矩阵。
赋值完成后,调用相应的命令查看MATLAB 工作空间的占有情况。
解:A=[1 2 3 4;2 3 5 7;1 3 5 7 ;3 2 3 9 ;1 8 9 4;]B=[1+4i 4 3 6 7 7;2 3 3 5 5 4+2i;2 6+7i 5 3 4 2;1 8 9 5 4 3;] B=[1+4i 4 3 6 7 7;2 3 3 5 5 4+2i;2 6+7i 5 3 4 2;1 8 9 5 4 3;] C=A*B D=C(4:5,4:6); whos; 2.设矩阵16231351110897612414152⎛⎫ ⎪⎪ ⎪ ⎪⎝⎭,求A ,1A -,3A ,12A A -+,1'3A A --,并求矩阵A 的特征值和特征向量。
解:A=[16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 2;] det(A) inv(A) A.^3 2*A+inv(A) 3*A-A' [V,D]=eig(A)abs (A)3.解下列矩阵方程: 01010014310001201001010120X -⎛⎫⎛⎫⎛⎫⎪ ⎪ ⎪=-⎪ ⎪⎪⎪ ⎪ ⎪-⎝⎭⎝⎭⎝⎭ 解:A=[0 1 0;1 0 0;0 0 1;]; B=[1 0 0;0 0 1;0 1 0;]; C=[1 -4 3; 2 0 -1;1 -2 0;]; X=inv(A)*C*inv(B)4.一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。
MATLAB上机实验报告
Matlab上机实验报告实验二读入MATLAB下自带图像pout.tif1)利用亮度变换函数,调整图像亮度。
a)调整范围设定[0 1],[1 0],观测显示效果;b)调整范围设定[0.5 0.75],[1 0],观测显示效果。
解:a, I=imread('pout.tif');colormap;imshow(I);j=imadjust(I,[0 1],[1 0],1.5);figure;subimage(j);b, >> I=imread('pout.tif');colormap;imshow(I);j=imadjust(I,[0.5 0.75],[1 0],1.5);figure;subimage(j);2)利用对比度拉伸函数,压缩高值灰度(c值自行设定)。
解:I=imread('pout.tif');colormap;subplot(1,2,1);imshow(I);xlabel('a)原始图像');J=double(I);J=100*log(J+1);I=uint8(J);subplot(1,2,2);subimage(J);xlabel('b)非线性变换');3)利用直方图函数,生成并绘制图像直方图。
解:I=imread('pout.tif');subplot(1,2,1);imshow(I);title('原始图像');subplot(1,2,2);imhist(I);4)利用直方图修正函数,生成均衡化后的图像直方图(n值自定设定)。
解:I=imread('pout.tif');figure(1);subplot(1,2,1);imshow(I);xlabel('a)原始图像');J=histeq(I);figure(1);subplot(1,2,2);imshow(J);xlabel('b)直方图均衡');figure(2);imhist(I,100);figure(3);imhist(J,100);实验三1.运行例3、4,显示并分析输出结果,说明逆滤波和维纳滤波的区别。
Matlab第一次上机作业
输入: >>tic, n=9;[u,k]=xsj(n), toc,surf(u)
计算Байду номын сангаас果如下:
u=
0 0 0 0 0 0 0 0 0 0 0 0 0.0256 0.0413 0.0508 0.0560 0.0577 0.0560 0.0508 0.0413 0.0256 0 0 0.0413 0.0686 0.0859 0.0955 0.0986 0.0955 0.0859 0.0686 0.0413 0 0.0508 0.0859 0.1088 0.1216 0.1258 0.1216 0.1088 0.0859 0.0508 0 0 0.0560 0.0955 0.1216 0.1364 0.1412 0.1364 0.1216 0.0955 0.0560 0 0 0.0577 0.0986 0.1258 0.1412 0.1462 0.1412 0.1258 0.0986 0.0577 0 0 0.0560 0.0955 0.1216 0.1364 0.1412 0.1364 0.1216 0.0955 0.0560 0 0 0.0508 0.0859 0.1088 0.1216 0.1258 0.1216 0.1088 0.0859 0.0508 0 0 0.0413 0.0686 0.0859 0.0955 0.0986 0.0955 0.0859 0.0686 0.0413 0 0 0.0256 0.0413 0.0508 0.0560 0.0577 0.0560 0.0508 0.0413 0.0256 0 0 0 0 0 0 0 0 0 0 0 0 0
程序一: function [u,k]=xsbj(n) % xsbj:用块 Jacobi 迭代法求解线性方程组 A*u=f % u:方程组的解; k 迭代次数; n:非边界点数 % a:方程组系数矩阵 Aii 的下对角线元素;b:方程系数矩阵 Aii 的主对角线元素 % c:方程组系数矩阵 Aii 的上对角线元素;d:追赶法所求方程的右端向量 % e:允许误差界;er:迭代误差 f=2*1/(n+1)^(2)*ones(n+2,n+2); a=-1*ones(1,n);b=4*ones(1,n);c=-1*ones(1,n);u=zeros(n+2,n+2);e=0.000000001; for k=1:2000 er=0; ub=u; for j=2:n+1 d(1:n)=f(2:n+1,j)+ub(2:n+1,j-1)+ub(2:n+1,j+1); x=zg(a,b,c,d); u(2:n+1,j)=x'; er=er+norm(ub(:,j)-u(:,j),1); end if er/n^2<e,break; end end 程序二: function x=zg(a,b,c,d) % zg:用追赶法求解线性方程组 n=length(b); % LU 分解 u(1)=b(1); for k=2:n if u(k-1)==0,D=0,return; end l(k)=a(k)/u(k-1); u(k)=b(k)-l(k)*c(k-1); end % 追赶法求解之追过程,求解 Ly=d y(1)=d(1); for k=2:n y(k)=d(k)-l(k)*y(k-1); end % 追赶法求解之追过程,求解 Ux=y if u(n)==0,D=0,return;end x(n)=y(n)/u(n); for k=n-1:-1:1 x(k)=(y(k)-c(k)*x(k+1))/u(k); end 输入:
matlab上机实验报告
MATLAB上机指导书上机一 MATLAB编程环境一、上机目的1.熟悉MATLAB的操作环境及基本操作方法2.熟悉MATLAB的通用参数设置3.熟悉 MATLAB的搜索路径及设置方法4.熟悉MATLAB帮助信息的查阅方法二、上机内容和结果1.利用菜单设置MATLAB的Command Window中字体大小,并更改输出格式。
示例:结果:2.在硬盘上创建以自己名字命名的文件夹,将当前路径修改为此文件夹示例:结果:4.完成下列操作:(1)在MATLAB命令窗口输入以下命令>> x=0:pi/10:2*pi;>> y=sin(x);(2)在工作空间选择变量y,在在工作空间窗口选择绘图菜单命令或在工具栏中单击绘图命令按钮,绘制变量y的图形。
结果:5.利用帮助学习save,load命令的用法,将在工作区中变量全部保存在mypath.mat中,清空工作区,重载变量x,y查看变量信息,并把它们保存在mypath.mat中结果:6. 计算y=1.3^3*sin(pi/3)*sqrt(26)(1)结果用format命令按不同格式输出。
(2)观察在进行上述命令计算后历史窗口的变化,用功能将实现回调刚才的计算语句。
(3)回调计算语句,把sin改为sn运行,观察反馈信息,若回调语句在语句后面加“;”,看输出有何不同结果:上机二 Matlab的计算可视化一、上机目的1、理解MATLAB绘图方法2、掌握绘制二维数据曲线图的方法3、掌握用plot函数和fplot函数绘制曲线的方法4、通过练习掌握绘制二维数据曲线图的方法和plot 函数和fplot 函数的使用5、通过练习熟悉三维曲线和曲面图的绘制方法二、上机内容和步骤1.上机内容(1)绘制下列曲线:①33x x y -= ②2221x e y π= ③64222=+y x1.>> x=0:5; >> y=x-x.^3/3; >> plot(y); >>2.>> x=0:0.3:1;>> y=1/(2*pi)*exp(x.^2/2); >> plot(y);3.>> ezplot('x.^2-2*y.^2-64',[-50 50]);(2)通过用plot 和fplot 函数绘制xy 1sin 的曲线,并分析其区别。
Matlab上机实验题及参考解答
Matlab上机实验题及参考解答目录实验一Matlab初步实验 (2)一matlab基本功能介绍 (2)二Matlab扩展功能 (2)三练习 (2)四练习题参考解答 (3)实验二概率模型实验 (5)一复习 (5)二事件的响应 (5)三Matlab中随机数字的生成与处理 (5)四练习 (5)五练习题参考解答 (5)实验三插值与拟合 (7)实验四线性规划与非线性规划 (8)4.1 实验目的 (8)4.2 实验内容 (9)4.3 综合练习 (10)4.4 课外作业 (11)实验五数值计算 (12)5.1 实验目的 (12)5.2 实验内容 (12)4.3 综合练习 (15)4.4 课外作业 (15)实验六计算机图像处理 (16)6.1 实验目的 (16)6.2 实验内容 (16)6.3 综合练习 (17)6.4 课外作业 (19)实验七综合练习 (19)7.1 实验目的 (19)7.2 实验内容 (19)7.3 综合练习 (20)7.4 课外作业 (21)实验一 Matlab 初步实验 一 matlab 基本功能介绍1 编程环境2语法规范:for … end; if …else if …end; 3 矩阵运算 4 图形绘制二 Matlab 扩展功能1 编程练习:(1) 绘出序列kk x x r r 0(1),0.2083=+=;(2) 绘出曲线rtx t x e t 0(),0=>2 扩展功能(1) 矩阵中全部数据、部分数据的截取、更改; (2) 矩阵的初始化与赋值如:A=zeros(5,5); A(2:2:)=[1,2 3 4 5] 3 微积分基础(见实验4) 符号计算三 练习(课上编程完成下列练习,课后上机验证) 1 求和S=1+2+3+…+100; 2 求和e 1111!2!10!1...=++++3求和S 1112310!1...=++++4设A 234576138⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦, 求A 的逆、特征值和特征向量;验证Ax=λx 5 画函数图()011mrtm x x t x e x -=⎛⎫+- ⎪⎝⎭6 展开 (x-1)(x-2)…(x-100)7 因式分解 x 8—y 8; 因数分解200520068 求极限312lim +∞→⎪⎭⎫⎝⎛++n n n n9 )](sin[cos 22x x y += 求dxdy10 求积分x xdx 10ln ⎰11 求积分3⎰并且画出所求的平面区域12 设x+2y=1, 2x+3y=6, y=2x 2, 画出各个方程图形,求出曲线交点.四 练习题参考解答%MatlabTrain1.m clear all % 2nd e=1; temp=1; for I=1:1:10temp=temp*I; e=e+1/temp; end e%%%%%%%%%%% clear all % 3nd S=0; temp=1;for I=1:1:100temp=temp*I; endfor J=1:1:temp S=S+1/J; end S%%%%%%%%%%%%%% clear all % 11ndx=linspace(0,4); y=1./sqrt(x.^5+1); plot(x,y) for t=1:0.1:3yt=1./sqrt(t.^5+1);hold online([t,t],[0,yt]);end%fill(t,yt,'b') %%%%%%%%%%%%% clear all% 12ndx=linspace(-2,2);y=[0.5-0.5*x; 2-2/3.*x; 2*x.^2]; plot(x,y)grid实验二概率模型实验一复习1 小结上次编程练习中存在的问题,讲述部分习题答案2 画图命令介绍:line二事件的响应(1) 获取鼠标的位置%MatlabTrain2.mclear all% 鼠标响应p=ginput(3)plot(p(:,1),p(:,2),'r*')(2) 键盘输入相应t=input('How many apples? t=');m=t+3三Matlab中随机数字的生成与处理1 随机数的生成2 产生随机数字3 产生某区间的整数4 生日模拟问题的Montecaro法设计技术、思路学生尝试编程四练习(1) 编程验证人数在不同年龄段的生日的概率计算(2) 编程实现游戏”聪明伶俐100分”(3) 编程实现两家电影院的座位数问题(4) 编程实现某图形面积的计算五练习题参考解答(1) 生日问题程序示例:%birthPro.mn=0;nStudents=30;for I=1:1000 %how many times testy=0;x=1+floor(365*rand(1,nStudents));%get nStudents random numbersfor J=1:nStudents-1for K=J+1:nStudentsif x(J)==x(K)y=1;break;endendendn=n+y;%count, n times of that there are two people's dirthday in the same dayendfreq=n/I % caculating the frequently(2) 编程实现游戏”聪明伶俐100分”参考答案%MatlabTrain2.mclear all% 鼠标响应x=floor(10*rand(1,4))t=input('填入四个数字[n1 n2 n3 n4]=');flag=0;A=0;B=0;for I=1:1:8flag=flag+1;A=0;B=0;if t==xswitch flagcase 1disp('聪明绝顶!');case 2disp('聪明!');case 3disp('有点聪明!');case 4disp('还可以!');case 5disp('聪明伶俐100分!');case 6disp('聪明伶俐90分!');case 7disp('聪明伶俐85分!');case 8disp('聪明伶俐80分!');otherwisedisp('赫赫!');endbreak;endfor J=1:1:4for K=1:1:4if x(J)==t(K) & J==KA=A+1;else if x(J)==t(K) & J~=KB=B+1;endendendends='AABB';s(1)=INT2STR(A);s(3)=INT2STR(B);disp(s);t=input('不重复填入四个数字[n1 n2 n3 n4]=');endif flag>0disp('太烂了! 正确答案是:');xend实验三插值与拟合一复习讲述聪明伶俐100分的编程中的问题二插值三拟合课堂练习2 某之股票价格from 2003 09 01 to 2004 01 02,试进行插值、拟合%TimerS.m%from 2003 09 01 to 2003 01 02clear all;dataST=[15.09 14.7514.95 14.722.88 21.8619.82 19.09];plot(dataST)四课外练习112)进行多项式拟合,求出拟合多项式,并求出多项式在t=4, 5处的值.实验四线性规划与非线性规划4.1 实验目的1 用Matlab求解线性规划2 用Matlab求解非线性规划4.2 实验内容4.2.1 线性规划求解实用格式:x=lp(c, A, b, xLB,xUB,x0,nEq)可以求解下列线性规划模型:min f=c’xs.t. Ax=<=b(其中前nEq个约束为等式约束,即等式约束的个数,其余是不等式约束<=) xLB<=x<=xUB函数中x0参数是算法迭代的初始点,任意取值例1 求解下列线性规划1)123123123123min2..360210200,1,2,3jz x x xs t x x xx x xx x xx j=--+⎧⎪++≤⎪⎪-+≤⎨⎪+-≤⎪≥=⎪⎩,2)1235635623416367min..3621060,1,,7jz x x x x xs t x x xx x xx xx x xx j=-++-⎧⎪++=⎪⎪+-=⎪⎨-+=⎪⎪++=⎪≥=⎪⎩例1求解示例c=[-2 -1 1]';%book page 72 Number 16-1A=[3 1 1;1 -1 2;1 1 -1];b=[60 10 20]';xlb=[0 0 0]';xub=[inf inf inf]';x0=[0 0 0]'; x=lp(c,A,b,xlb,xub,x0,0)% x=(15 5 0)'例2 求解示例c2=[1 -1 1 0 1 -1 0]';%book page 72 Number 16-3A2=[0 0 3 0 1 1 0;...0 1 2 -1 0 0 0;...-1 0 0 0 0 1 0;...0 0 1 0 0 1 1];b2=[6 10 0 6]';xlb2=[0 0 0 0 0 0 0]';xub2=[inf inf inf inf inf inf inf]';x02=[0 0 0 0 0 0 0]';x2=lp(c2,A2,b2,xlb2,xub2,x02,4)% unbounded4.2.2 非线性规划1)命令格式1:[X, OPTIONS]=constr(‘FUN’, X, OPTIONS,VLB,VUB)2)命令格式2:X=FMINCON(FUN,X0,A,B,Aeq,Beq)% minimizes FUN subject to the linear equalities% Aeq*X = Beq as well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.)例2 求解非线性规划y x x x x s t x3211221min22 ..1=++-≤-求解示例%unconop.mfunction y=unconop(x)y=x(1).^3+2*x(1).*x(2)+2*x(2).^2;%book page 148 ex.7-1 后建立调用函数xx=fmincon('unconop',[0 0]',[-1 0],-1,[],[])%book page 148 ex.7-1 4.3 综合练习学生独立编写程序,求解一个含有2个变量的线性规划问题,要求:1)编写程序,把可行域画上阴影;2)求出最优解,在可行域上标出最优解;3)求出基本解,并在上图中表示出来;4)求出基本可行解,观察单纯形方法迭代时,顶点的变化.可行域画图与表出阴影示例:syms x y[u(1),v(1)]=solve('y=x+2','y=2*x');%求出交点坐标[u(2),v(2)]=solve('y=-x+2','y=2*x');[u(3),v(3)]=solve('y=x+2','y=-x+2');x=linspace(0,3,5); %直线作图y=[2*x;-x+2;x+2];line(x,y); gridpatch(double(u),double(v),'b'); 运行结果:4.4 课外作业1 求解线性规划131223min ..250.530,1,2,3i x x s t x x x x x i +⎧⎪+≤⎪⎨+=⎪⎪≥=⎩ (1) 求解线性规划;x *=()(2) 目标函数中c 1由1变为(-1.25)时求最优解;(3) 目标函数中c 1由1变为(-1.25),c 3由1变为2时求最优解;(4) 约束条件中53b ⎛⎫= ⎪⎝⎭变为21b -⎛⎫'= ⎪⎝⎭时,求解;(5) 约束条件中53b ⎛⎫= ⎪⎝⎭变为23b ⎛⎫'= ⎪⎝⎭时,求解[刁在筠,运筹学(第二版),高等教育出版社,2004,01 p74第20题]2 求解非线性规划y x x x x x x x 3221122233min 2223=++++ 注:无约束非线性规划问题, 命令:fminunc子函数% unconop.mfunction y=unconop(x)y=x(1).^2+2*x(1).*x(2)+2*x(2).^2+2*x(2).*x(3)+3*x(3).^2;%book page 148 ex.7-1 主函数:xx=fminunc('unconop',[0.1 0.1 1]')思考:绘出两个变量的线性规划问题的可行域、标出可行的整数解和求出可行解;演示单纯形方法的迭代过程,如j z x x s t x x x x x j 121212min 2..360200,1,2=--⎧⎪+≤⎪⎪+≤⎨⎪⎪≥=⎪⎩实验五 数值计算5.1 实验目的1 掌握代数数值计算2 掌握常微分方程数值计算5.2 实验内容5.2.1 关于多项式设多项式1110()n n n n p x a x a x a x a --=++++表示为110[,,,,]n n p a a a a -=1)求多项式的根 roots(p) %求出p(x)=0的解。
Matlab上机实验答案
4. 完成下列操作: (1) 求[100,999]之间能被21整除的数的个数。 (2) 建立一个字符串向量,删除其中的大写字母。 >> n=100:999; >> l=find(rem(n,21)==0); >> length(l)
ans =
43
>> ch='aegbBOIEG0je23RGnc';
150 335 520 705 890
77 237 397 557 717
>> D=C(3:5,2:3)
D=
520 705 890 >> whos Name
397 557 717
Size
Bytes Class
Attributes
A B C D
5x5 5x3 5x3 3x2
200 double 120 double 120 double 48 double
(2) >> A*B ans =
68 309 154 >> A.*B ans =
44 -72 -5
62 596 241
(3) >> A^3 ans =
12 102 68 0 9 -130
4 261 49
37226 247370 78688
233824 149188 454142
48604 600766 118820
>> A.^3 ans = 1728 39304 27 39304 343 274625 -64 658503 343
(4) >> A/B ans =
16.4000 -13.6000 35.8000 -76.2000 67.0000 -134.0000 >> B\A ans =
matlab上机作业报告(计算初...
matlab上机作业报告(计算初...一、给定向量x ≠0,计算初等反射阵H k 。
1.程序功能:给定向量x ≠0,计算初等反射阵H k 。
2.基本原理:若()xx x R x ∈=,,, 的分量不全为零,则由12112212122()x (,,,)1()22n T T sign x e x x x x σσσρσσρ-?=? =+=+==+?=-=-u x u uu H I I uuu 确定的镜面反射阵H 使得y eHx =-=σ;当(1)k n ≤<时,由21/2k ()T 1()()()k 1()()()(())(0,,0,,,,)1()()=()2()nk i i kk nk k k n k T k k Tk k k kk k T k k sign x x x x x x σσρσσσρ=+-?=?=+∈?==+??=-?∑u R u u u H I u u 有T 121(,,,,,0,,0)n k k k x x x σ-=-∈H x R算法:(1)输入x ,若x 为零向量,则报错 (2)将x 规范化,{}x x x M ,,,m ax =如果M =0,则报错同时转出停机否则n i M x x i i ,,2,1, =←(3)计算2x =σ,如果0<1x ,则σσ-= (4))(1x +=σσρ (5)计算1,(1)x σ==+u x u (6)1Tρ-=-H I uu (7)(M ,0,,0)σ=-y(8)按要求输出,结束3.变量说明:x -输入的n维向量;n -n维向量x的维数;M -M是向量x的无穷范数,即x中绝对值最大的一项的绝对值;p -Householder初等变换阵的系数ρ;u -Householder初等变换阵的向量Us -向量x的二范数;x -输入的n维向量;n -n维向量x的维数;p -Householder初等变换阵的系数ρ;u -Householder初等变换阵的向量Uk -数k,H*x=y,使得y的第k+1项到最后项全为零;4.程序代码:(1)function [p,u]=holder2(x)%HOLDER2 给定向量x≠0,计算Householder初等变换阵的p,u %程序功能:函数holder2给定向量x≠0,计算Householder初等变换阵的p,u;%输入:n维向量x;%输出:[p,u]。
