东南大学2015短学期MATLAB作业1.doc_第1页
东南大学2015短学期MATLAB作业1.doc_第2页
东南大学2015短学期MATLAB作业1.doc_第3页
东南大学2015短学期MATLAB作业1.doc_第4页
东南大学2015短学期MATLAB作业1.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Matlab Worksheet 1Part A1. Get into Matlab: Use the diary command to record your activity in to a file: diary mydiary01.doc before you start your work. (And diary off to switch off your diary when you finish your work.)At the Command Window assign a value x=10, then use the Up Key to repeat the expression, editing it to show the effect of using a semicolon after the 10, namely x=10;Answers: x=10x = 10 x=10;2. Confirm whether the following names of variables are acceptable:a) VelocityYes b) Velocity1Yes c) Velocity.1 Nod) Velocity_1Yes e) Velocity-01 Nof) velocityONEYes g) 1velocity No3. Assign two scalar variables x and y values x=1.5, y=2.3, then construct Matlab expressions for the following:a) b) c) d)Answers:10.2138,8.7566,0.0232,-3.73604. Assign two variables with complex values u=2+3j and v=4+10j and then construct expression for:a) b) c) d) Answers: a)0.3276 - 0.0690ib) -22.0000 +34.0000ic) 1.5000 - 1.0000id) 6.1421e+008 +1.5355e+009i5. Use the colon operator : to assign numerical values between 0 and 1 to vector array variable a in steps of 0.1.Answer: a=colon(0,0.1,1)6. Use linspace function to assign 20 values to vector variable y between 20 and 30.Answer: linspace(20,30,20)7. Assign 20 values to a variable h increasing logarithmically between 10 and 1000.Next, use the colon operator to assign the first 10 elements of h to a variable p.Answers: h=linspace(10,1000,20); p=h(1:10)8. Create 6 element row vector z with values 1.0 1.2 1.6 -1.7 1.8 1.9, then construct an expression for the sum of the 2nd 4th and 6th elements of z.Answers: z=1.0 1.2 1.6 -1.7 1.8 1.9sum=z(2)+z(4)+z(6)9. Use the colon operator to create a vector array x between 10 and -10in steps of -1, and second, an array vector y between 20 and -20 in steps -2 .a) Add x and y, b) subtract 10x from 5y.Answers: x=10:-1:-10y=20:-2:-20add=x+ysub=5*y-10*x10. Use the size, length, who and whos commands to establish the size and length of x and y from Question 9, and use transpose operator to convert vector x from Question 9.Answers: size(x)ans = 1 21 size(y)ans =21 whoYour variables are:add ans h p sub sum u v x y z whos Name Size Bytes Class add 1x21 168 double array ans 1x2 16 double array h 1x20 160 double array p 1x10 80 double array sub 1x21 168 double array sum 1x1 8 double array u 1x1 16 double array (complex) v 1x1 16 double array (complex) x 1x21 168 double array y 1x21 168 double array z 1x6 48 double arrayGrand total is 125 elements using 1016 bytes11. Show that if w= 2i 3i 3+i the . operator creates the transpose. What effect does the operator applied to w have on its own?Answer: w.ans = 0 + 2.0000i 0 + 3.0000i 3.0000 + 1.0000iwans = 0 - 2.0000i 0 - 3.0000i 3.0000 - 1.0000i 12. Use the ones function to create a 4 by 6 array of 1s. Considering just the shape of the resulting array, what do the expression ones(3), ones(5) and ones(7) all have in common?Answer:ones(4,6)They are all N-by-N matrix of ones.13. Create using the rand function a 5 by 4 random matrix and assign it to matrix array variable A and observe carefully what A(:,3) A(1:2) and A(3,2 4) mean.Answer: A=rand(5,4)A = 0.0579 0.2028 0.0153 0.4186 0.3529 0.1987 0.7468 0.8462 0.8132 0.6038 0.4451 0.5252 0.0099 0.2722 0.9318 0.20260.1389 0.1988 0.4660 0.6721A(:,3)ans = 0.0153 0.7468 0.4451 0.9318 0.4660A(1:2)ans = 0.0579 0.3529A(3,2 4) ans = 0.6038 0.525214. Using array subscripts, create an expression for the sum of the element in the top right-hand-corner of A and the bottom left-hand-corner of A. Also assign the 2nd column of A to a column vector b, and assign the 3rd row of A to a row vector d.Answer: sum=A(4,1)+A(1,4)sum =0.4285b=A(:,2)b = 0.2028 0.1987 0.6038 0.2722 0.1988d=A(3,:)d = 0.8132 0.6038 0.4451 0.5252 diary off to switch off your diary now.15. Using the colon operator, assign a row vector array t, values between 0 and 10 in steps of 0.01. Use the ; operator to prevent displaying the information. Obtain the term-by-term values of functions:a) b) c) Answers:a) z=exp(-0.05*t);b) z=exp(-0.05*t).*sin(pi*t);c) z=exp(-0.05*t).*sin(pi*t).*cos(2*pi*t);16. For Question 15 c), use the plot function to plot the value z against t. And use xlabel and ylabel to label the t axis time and z axis Response and use title to give your figure a title. Copy the figure into your mydiary01.doc (from the figure edit menu, select copy, then open mydiary01.doc and paste in the figure as an object).Answers: Part B1. Create a new Script m-file, called myfirst.m, to assign values to three vector arrays a, b, and c; add each component together and assign to array d, where A= 1, 2, 3, b=4 5 6, c=7 8 9. Run myfirst.m. Examine the variable values in the WorkSpace . Edit myfirst.m to change variable b to b=10 20 30. Re-run myfirst.mAnswers:d =12 15 18d = 18 30 422. Create a function m-file, called myfun.m, that accepts two arrays x and y as arguments, and produces the sum of x and y, and gives as the function output, the maximum of x+y.Answer:x=1 2 3 ;y=2 3 4 ; myfun(x,y)ans = 3 5 7min(myfun(x,y)ans = 33. Modify your script m-file myfirst.m by calling in it myfun.m twice, namely with a and b as parameters in the argument list, and second with parameters b and c. Run myfirst.mand afterwards. Examine the WorkSpace variable values.Answers:myfirst.fun:clear all;a= 1, 2, 3;b=10 20 30; c=7 8 9;aplusb=myfun(a,b);bplusc=myfun(b,c)Command Window:aplusb = 11 22 33 bpluscbplusc = 17 28 394. Modify your script m-file myfirst.m by introducing variables A and B. Assign scalar values A=1, B=2. Declare A and B as global variables both in myfirst.m and myfun.m. Modify myfun.m by adding A and B to the maximum value of x+y. Answers:myfirst.m:global Aglobal BA=1;B=2;myfun.m:function sumA, sumB = myfun(x, y)global A;global B;sumA=A+max(x+y);sumB=B+max(x+y);Command Window:x=1 2 3;y=2 3 4 ;a,b=myfun(x,y)a = 8b = 95. Create a script m-file and, using a pair of nested for loops, assign to a 5 by 5 matrix array A, values equal to the sum of the subscripts. Ensure that the array is first cleared before the loops and afterwards display the array values. Run the m-file.Answer: m-file:clear allfor m=1:5 for n=1:5 A(m,n)=m+n; endendACommand Window:A = 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 6 7 8 9 106. Relational Operators:- Assign variables x=1 and y=2, then examine the values of the expressions:a) xy, b)xy)*(x=y)?Answer: a) ans = 0b) ans = 1c)ans = 2z = 07. While loop:- Create a script m-file, and using a while loop, generate the partial sum for the series: which stops adding-on terms when they are less than 1e-6 times the total sum.Answer: m-file:clear allterm =1/pi;n=1;sum=term;while (term 1e-6*sum) n=n+1; term=term/n; sum = sum + term;endsumCommand Window:sum = 0.54698. While loop:- Use a while loop to keep adding array A to itself until just one of the elements in A is greater than, but not equal to 40 where, initially .Answer: m-file:A=1 2; 3 4;while (max(max(A) y) a=x;else a=y;endCommand Window: new(1,1)ans = 110. If statements:- Create a function m-file that receives variable x, y and z as arguments, then uses an if elseif and else statements, generate the output value to be =x if first of all x1, otherwise =y if y10, otherwise =z if z20, otherwise =1 if none of those conditions hold. Test it on values (100,100,100) and (0, 0,0).Answer:m-file:function a = new (x, y,z)if (x 1) a=x;elseif(y 10) a=y;elseif(z 20) a=z;else a=1;endCommand Window: new(100,100,100)ans = 100 new(0,0,0)ans = 111. Switch Statement:- Create a function with one input argument x to test whether the expression: real(x2) is equal to 0,1, or -1, which then displays a string stating appropriately whether: x is equal to zero, x is equal to +1 or -1 or x

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论