




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
四川师范大学MATLAB语言实验报告1系级班年月日实验名称:Intro, Expressions, Commands姓名学号指导教师成绩1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB program development environment and to develop your first programs in this environment.2 Using MATLAB2.1 Starting MATLABLogon to your computer and start MATLAB by double-clicking on the icon on the desktop or by using the Start Programs menu. MATLAB Desktop window will appear on the screen.The desktop consists of several sub-windows. The most important ones are:l Command Window (on the right side of the Desktop) is used to do calculations, enter variables and run built-in and your own functions.l Workspace (on the upper left side) consists of the set of variables (arrays) created during the current MATLAB session and stored in memory.l Command History (on the lower left side) logs commands entered in the Command Window. You can use this window to view previously run statements, and copy and execute selected statements.You can switch between the Launch Pad window and the Workspace window using the menu tabs under the sub-window on the upper left side. Similarly, you can switch between the Command History and Current Directory windows using the menu tabs under the sub-window on the lower left side.2.2 Executing CommandsYou can type MATLAB commands at the command prompt “” on the Command Window.For example, you can type the formula cos(/6)2 sin(3/8) as(cos(pi/6) 2) * (sin(3 * pi/8)Try this command. After you finish typing, press enter. The command will be interpreted and the result will be displayed on the Command Window.Try the following by observing how the Workspace window changes: a = 2; (Make note of the usage of “;”) b = 3; c = a 4 b 5 + pi 3You can see the variables a, b and c with their types and sizes on the Workspace window, and can see the commands on the Command History window.Spend a few minutes to practice defining array variables (i.e. vectors and matrices) usingthe square bracket (“ ”) and colon (“:”) operators, and zeros() and ones() functions. ar = 1 2 3 4 5 ; br = 1 2 3 ;4 5 6 ; cr = 1 : 3 : 15; Set dr to rst 3 elements of ar.dr=ar(1:3); Set er to second row of br.er=br(2,:); Set ar to dr er. Find the number of elements of ar.ar=dr er;length(ar)2.3 Getting HelpThere are several ways to get help on commands and functions in MATLAB. First of all you can use the Help menu. You can also use the “?” button. Try to find information on the plot function from the help index. Also try to get information on the same function using the help command (i.e. type help plot). Finally, experiment with the lookfor command. This command looks for other commands related to a given keyword. 2.4 Some Useful CommandsTry the following commands and observe their results:Which : Version and location infoClear : Clears the workspaceClc : Clears the command windowwho, whos : Lists content of the workspace3 ExercisesPlease solve the following problems in MATLAB. Do not forget to keep a diary of your commands and their outputs.(1) Dene the variables x y and z as 7.6, 5.5 and 8.1, respectively, and evaluate:(2) Compute the slope of the line that passes through the points (1,-2) and(5,8).(3) Quiz 1.1: 5(4) 1.6 Exercises: 1.1, 1.4(5) 2.15 Exercises: 2.6, 2.9, 2.114 Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your diary file and M-file to your TA.Do not forget to delete your les from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告2系级班年月日实验名称:Programming, Relational and Logical Expressions姓名学号指导教师成绩1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB script files (M-files), subarrays, relational and logical operators.2 Script FilesScript files are collections of MATLAB statements that are stored in a file. Instead of typing commands directly in the Command Window, a series of commands may be placed into a file and the entire file may be executed by typing its name in the Command Window. Such files are called script files that are also known as M-files because they have an extension of .m. When a script file is executed, the result is the same as it would be if all of the commands had been typed directly into the Command Window. All commands and script files executed in the Command Window share a common workspace, so they can all share variables in the workspace. Note that if two script files are executed successively, the second script file can use the variables created by the first script file. In this way, script files can communicate with other script files through the data left behind in the workspace. An Edit Window is used to create new M-files or to modify existing ones. The Edit Window is a programming text editor, with the features of MATLAB language highlighted in different colors. You can create a new M-file with the File/New/M-file selection and you can open an existing M-file with the File/Open selection from the desktop menu of MATLAB.(1) Create a new working directory under the current directory and change the current directory to TAs suggest.3 SubarraysIt is possible to select and use subsets of MATLAB arrays. To select a subset of an array, just include a list of the elements to be selected in the parentheses after the array name. MATLAB has a special function named end that is used to create array subscripts. The end function always returns the highest value taken on by a given subscript. It is also possible to use subarrays on the left-hand side of an assignment statement to change only some of the values in an array. If values are assigned to a subarray, only those values are changed but if values are assigned to an array, the entire contents of the array are replaced by the new values.(1) Define the following 5 x 5 array arr1 in MATLAB.(2) Write a MATLAB statement to select a subset of arr1 and return the subarray containing the values as shown.arr11=arr1(1,5,2 4 5);(3) Write two MATLAB statements to select the last row and last column of arr1, separately.arr12=arr1(5,:);或arr12=arr1(end,:); arr13=arr1(:,end);或 arr13=arr1(:,5); (4) Write MATLAB statements to obtain the following array from arr1.arr2=arr1(1 5,:);4 Relational and Logical OperatorsRelational and logical operators are the two types of operators that produce true/false results in MATLAB programs. MATLAB interprets a zero value as false and any nonzero value as true. Relational operators ( =, =,=,0;(3) Write an expression using arr4 and a relational operator to produce the following result.arr6=arr4=1;(4) Write a MATLAB program which will generate an (n-1)x(n-1) matrix from a given nxn matrix which will be equal to given matrix with first row and first column deleted.arr44=rand(5); arr444=arr35(2:end,2:end);(5) Generalize your program above so that the program should ask the row and column numbers to be deleted and then generate new (n-1)x(n-1) matrix.n=input(input n:);matrixn=rand(n)delrow=input(input row numbers to be deleted:);delcolumn=input(input column numbers to be deleted:);matrixn_1=matrixn(1:delrow-1 delrow+1:end, 1:delcolumn-1 delcolumn+1:end)(6) Quiz 3.1 (P88)5 Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your diary file and M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告3系级班年月日实验名称:Branches and Loops, Logical Arrays.姓名学号指导教师成绩1 ObjectiveThe objective of this lab is to familiarize you with the MATLAB Branches and Loops, Logical Arrays.2 ExercisesDo not forget to add sufficient documentation and proper indentation to all programs you write.(1) Write a program that calculates follow equation with for and while loop, and write a program without loop. % for loopk1=0;for ii=1:64 k1=k1+2(ii-1);end % while loopk2=0;n=0;while n=0&n) followed by a carriage return (press the return key). A SIMULINK window should appear shortly, with the following icons: Sources, Sinks, Discrete, Linear, Connections, Extras.(3) Next, go to the File menu in SIMULINK window and choose New in order to begin building the block diagram representation of the system of interest.(4) Open one or more of the block libraries and drag the chosen blocks into the active. (5) After the blocks are placed, draw lines to connect their input and output ports by moving the mouse over a port and drag using the left button. To make a line with a right angle in it, release the button where you want the corner, then click on the end of the line and drag to create next segment. To add a second line that runs off of an existing line click the right mouse on an existing line and drag it.(6) Save the system by selecting Save from the File menu.(7) Open the blocks by double-clicking and change some of their internal parameters.(8) Adjust some simulation parameters by selecting Parameters from the Simulation menu. The most common parameter to change is Stop Time that defines the length of time the simulation will run.(9) Run the simulation by selecting Start from the Simulation menu. You can stop a simulation before completing by selecting Stop from the Simulation menu.(10) View the behavior of the system by attaching Scope blocks to the variables of interest, or by using To Workspace blocks to send data to the MATLAB workspace where you can plot the results using standard MATLAB commands.3 Exercises(1) Your TA has shown you how to observe and print signals from the scope. Try this out by printing out the input signal, which should be a -1V to 1V square wave with frequency 0.1 Hz. Note the peak-to-peak voltage difference of this signal. Note to write key blocks parameters.(2) Write a Simulink model to calculate the following differential equation, Initialized ,。(3) An open-loop control system is as following:P(s)ryControllerPlantKuSet the controller gain K=10. Simulate the system and examine the step response of the closed loop system using an input step size of 2.0 (change the step size by double-clicking on the Step Fcn block). View the controller output u and output y by Scope blocks, and plot output vs. time in MATLAB workspace. Use a Transfer Fcn block for the plant. To design the controller, use a Gain block for the proportional control block, and a Step Fcn block for the input. Tie Scope blocks to the controller output (plant input) and plant output. In order to bring the variables into the MATLAB workspace, tie To Workspace blocks to time from the Clock block, and the output y. Make the step occur at t=1.0 second. Double click To Workspace block, and set “Save format” to “Array”.Library: Step Sources Gain Math Transfer Fcn Continuous Scope Sinks To Workspace Sinks Clock Sources4 Quitting MATLABTyping quit on the command window will close the program. Do not forget to send your M-file to your TA.Do not forget to delete your files from the hard disk of the PC you used in the lab at the end of the lab session.四川师范大学MATLAB语言实验报告5系级班年月日实验名称:plotting姓名学号指导教师成绩1 Objectivel Learn how to use MATLAB 2D plotsl learn how to use Matlab demosl learn how to use 3D surface plotsl have fun2 2D Plotting and rand/round functions.(1) Create a 10 x 10 random matrix with value 0, 1 using rand. Call this matrix z.(2) Compute and plot sum of all columns. This should result in one plot line. The x axis should be column number, and the y axis is the sum of each single column.(3) Apply round to the matrix, and then plot the sum of the resulting matrix.(4) Apply ceil to the matrix, and then plot the sum of the resulting matrix. (5) Apply floor to the matrix, and then plot the sum of the resulting matrix. Put all of these plots on the same graph (there will be a total of four lines), with each line a different color and/or pattern. Make sure to create a legend identifying each line. Also put a title and axes labels on the graph.Save this script to copy to the TA at the end of the lab.3 Matlab demos 2D Plotting, 3D Surface Plotting.Now lets learn how to plot.At the same time, were going to learn how to use Matlab demos.You can use Matlab demos whenever you want to learn more about a particular feature. Find the demo under MATLAB - Graphics -2-D Plots and 3-D Plots.Read the description and click on Run this demo. Play with the different options, and look at the code used to generate each plot (in the mini command window as part of the demo). You want to be able to use what youve learned from this demo. Try plotting one style of graph on your own, by putting it in a script file and executing the commands.4 3D Surface Plotting.In the demo you looked at above, a demo function called peaks was used to get the data for the 3D surface plot. This matrix stores the z values (height values) for the x and y dimensions. When you plot it, if you do not specify x and y values, it makes assumptions about the values of the x and y axes (namely, they start at 1 and go up by 1 for the entire length of the matrix). We are going to make our own 3D surface plot, but using our own data.Using the 10x10 random matrix you generated at the beginning of the lab, make a 3D surface plot. If your matrix with random values is named z, you can create this plot by using the command surf(z).Change the shading and the colormap of this plot, and store the commands in an m-file to copy the TA at the end.Now, lets say we dont like the default setting of the x and y axes as 1:10. We want our x values to be 16 to 25, and our y values to be 111 to 120,.We cant just use these x=16:25 and y=111:120 values in surf directly.Now use meshgrid to create the x and y matrices appropriate to use in surf(x,y,z) for the plot with the stated desired axes. Plot this data on a new figure, and save the code to copy TA at the end of the lab.5 More fun with 3D surface plots.Use this data: X,Y = meshgrid(-8:.5:8); R = sqrt(X.2 + Y.2) + eps;Z = sin(R)./R;mesh(X,Y,Z) %similar to surfWhat will happen if you plot the same thing, but change Z to -Z ? Convince yours
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中化学人教版必修一3.3用途广泛的金属材料(教学设计1)
- 2025电力工程设计院技术服务合同员工待遇模板
- 2025房屋买卖合同申请模板
- (正式版)DB1501∕T 0020-2021 《绿道规划设计导则》
- 高中信息技术必修教学设计-6.3.2 信息交流的方式2-教科版
- 辽宁省大连市高中化学 第一章 认识有机化合物 1.4.2 元素分析与相对分子质量的测定说课稿 新人教版选修5
- 平面设计合同范本3篇
- 跨境电商箱包配饰品牌营销活动策划与执行指南
- 2025年检察院试题答案及答案
- 2025年办公定实务考试题及答案
- 外国运动员体育俱乐部聘用合同
- DL-T 5876-2024 水工沥青混凝土应用酸性骨料技术规范
- 副总经理招聘笔试题与参考答案(某大型国企)2024年
- 挂靠合同协议书版模板
- 【骨肌】化脓性骨髓炎课件
- 部编版五年级上册道德与法治全册课时练(一课一练)(含答案)
- DL∕T 1679-2016 高压直流接地极用煅烧石油焦炭技术条件
- 档案专业人员职业能力竞赛考试题库(含答案)
- 同种异体骨软骨移植与软骨修复
- 故障分析实验报告
- 行为生活方式与健康智慧树知到期末考试答案章节答案2024年杭州师范大学
评论
0/150
提交评论