




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、四川師范大學 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 an
2、d 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: Command Window (on the right side of the Desktop) is used to do calculations, en
3、ter variables and run built-in and your own functions.Workspace (on the upper left side) consists of the set of variables (arrays) created during the current MATLAB session and stored in memory.Command History (on the lower left side) logs commands entered in the Command Window. You can use this win
4、dow 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 u
5、sing 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.2For example, you can type the formula cos( sin(3 n /6) as>>(cos(pi/6) A 2) * (si n(3 * pi/8)Try this command. After you fini
6、sh 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 A 4 ? b ? 5 + pi A3You can see the variables a,
7、 b and c with their types and sizes on the Workspace win dow, and can see the comma nds on the Comma nd History wi ndow.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 >
8、;> ar = 1 2 3 4 5 ;>> br = 1 2 3 ;4 5 6 ;>> cr = 1 : 3 : 15;? Set dr to ?rst 3 elements ofar.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;len gth(ar)2.3 Getting HelpThere are several ways to get help on comma nds and
9、 fun cti ons 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 comma nd (i.e. type help plot). Fin ally, experime nt with the lookfor com
10、ma nd. This comma nd looks for other comma nds related to a give n keyword.2.4 Some Useful Comma ndsTry the followi ng comma nds and observe their results: Which : Version and location infoClear : Clears the workspaceClc : Clears the comma nd wi ndowwho, whos : Lists content of the workspace3 Exerci
11、sesPlease solve the followi ng problems in MATLAB. Do not forget to keep a diary of your comma nds and their outputs.(1) De?ne the variables x y and z as 7.6, 5.5 and 8.1, respective and evaluate:4.78xzCompute the slope of the line that passes through the points (1,-2) and(5,8). Quiz 1.1: 52y 55.2x(
12、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
13、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
14、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
15、 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 worksp
16、ace. 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 o
17、r 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 MATL
18、AB.(1) Create a new working directory under the current directory and change the current directory to TA 'ssugges't .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 parenthesesafte
19、r the array name. MATLAB has a special function named end that is used to create arraysubscripts. 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 i
20、n 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.1 33 6 54 4 8 12 1016arr111 15 32 41 5116 17 18 95 2021 54 23 74
21、 22(2) Write a MATLAB statement to select a subset of arr1 and return the subarray containing the values as shown.365 arr1154 74 22arr11=arr1(1,5,2 4 5);(3) Write two MATLAB statementsto select the last row and last column of arr1, separately.arr12=arr1(5,:);或 arr12=arr1(end,:);arr13=arr1(:,end)或 ar
22、r13=arr1(:,5);(4) Write MATLAB statements to obtain the following array from arr1.1 21354arr2 3 236 745 22arr2=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 zer
23、o value as false and any nonzero value as true. Relational operators ( =, =,>,>=,<,<=) are operators with two operands that produce either a true (1) or a false (0) result, depending on the values of the operands. Relational operators can be used to compare a scalar value with an array.
24、They can also be used to compare two arrays or two strings only if they have the same size. Be careful not to confuse the equivalence relational operator ( = ) with the assignment operator ( = ). Logic operators ( &, |, xor, )are operators with one or two operands that yield a logical result suc
25、h as 0 or 1. There are three binary logic operators: AND (& ), OR ( |), and exclusive OR ( xor ); and one unary operator: NOT ( ). In the hierarchy of operations, logic operators are evaluated after all arithmetic and relational operators have been evaluated. The operator is evaluatedbefore othe
26、r logic operators.(1) Define the following 4 x 5 array arr4 in MATLAB.1234521234arr 43212343212(2) Write an expression using arr4 and a relational operator to produce the following result.1 111101111arr50011100011arr5=arr4>0;(3) Write an expression using arr4 and a relational operator to produce
27、the following result.1000001000arr 60010000010arr6=arr4=1;(4) Write a MATLAB program whichwillgenerate an (n-1)x(n-1) matrix from agiven 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 s
28、o 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:d
29、elrow-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
30、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 a
31、ll programs you write.(1) Write a program that calculates follow equation with for and while loop, and write a program without loop.63i 2 63K 2i 1 2 22263i0% for loopk1=0;for ii=1:64k仁 k1+2Fi-1);end% while loopk2=0;n=0;while n>=0&n<64k2=k2+2A n;n=n+1;end% without loopa=0:63;b=2.Aa;K3=sum(b
32、);(2) Write a program that accepts a vector of integers as input and counts the number of integers that are multiples of 3 in that vector. You can assume that the input contains only integer values. An example run of your program can be as follows:En ter a vector of in tegers: 1 3 2 8 0 5 6 The nu m
33、ber of multiples of 3 is 2(3) The root mean square is a way for calculating a mean for a set of numbers. Thermsaverage of a series of nu mbers is give n as:rmsaverageWrite a program that will accept an arbitrary nu mber of in put values and calculate the rmsaverage of the nu mbers. The program shoul
34、d ask the user for the nu mber of values to be en tered. Test your program with 4 and 10 set of nu mbers.% The root mean square is a way for calculati ng a mean for a set of nu mbers% In itializesum_x2=0;% Get the nu mber of points to in put.n=in put('E nter nu mber of poin ts:');% Loop to r
35、ead in put valuesfor ii=1: n% Read in n ext valuex=in put('E nter value:');% Calculate square sumssum_x2=sum_x2+xA2;end% Now calculate root mean squareroot_ms=sqrt(sum_x2/n);% Tell userfprintf('The number of data points is: %dn',n);fprin tf('The root mean square of this data set
36、is: %fn :root_ms); 3.8 exercises:3.5 4.7Exercises: 4.1 4.23 Quitti ng MATLABTyping quit on the comma nd win dow 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.四川師范大
37、學 MATLAB 語言實驗報告 4系 級 班年月日實驗名稱: MATLAB/SIMULINK package姓名 學號 指導教師 成績1 ObjectiveTo learn how to use MATLAB/SIMULINK packageTo learn how to estimate performance parameters from time-domain data2 SIMULINK BasicBasic steps(1) Click on the MATLAB button to start MATLAB.(2) Once MATLAB has started up, type
38、 simulink (SMALL LETTERS!) at the MATLAB prompt (>>) 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
39、 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 usin
40、g 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 syste
41、m 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 simu
42、lation 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 s
43、end 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
44、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.d2x dt2(1x2)生dtdx(0)Initialized x(°)1 , dt(3) An open-loop control system is as following:Con trollerPla nt1P(s) s 1Set the con troller
45、gain K=10. Simulate the system and exam ine the step resp onse of the closed loop system using an in put step size of 2.0 (cha nge the step size by double-clicking on the Step Fen block). View the controller output u and output y by Scope blocks, and plot output vs. time in MATLAB workspace.Use a Tr
46、an sfer Fen block for the pla nt. To desig n the con troller, use a Gain block for the proporti onal con trol block, and a Step Fcn block for the in put. Tie Scope blocks to the con troller output (pla nt in put) and pla nt output. In order to bring the variables into the MATLAB workspace, tie To Wo
47、rkspace blocks to time from the Clock block, and the output y. Make the step occur at t=1.0 sec ond. Double click To Workspace block, and set “ Save format ” to “ Array ”.Library:Step -SourcesGai n MathTran sfer Fcn -Con ti nu ousScope -SinksTo Workspace -SinksClock -Sources4 Quitti ng MATLABTyping
48、quit on the comma nd win dow 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 sessi on.四川師范大學MATLAB語言實驗報告5實驗名稱:plotti ng姓名學號指導教師成績1 ObjectiveLearn how to use MATLAB 2D plotsl
49、earn how to use Matlab demoslearn how to use 3D surface plotshave 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 numb
50、er, 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 p
51、lots 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 Su
52、rface Plotting.Now let's learn how to plot. At the same time, we're 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 cli
53、ck 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 you've learned from this demo. Try plotting one style of graph on your own, by putting it in a scrip
54、t 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 an
55、d 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
56、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, let's say we don't like the default s
57、etting 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 can't 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 desire
58、d 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.A2 + Y.A2) + 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 yourself of what will happen when you do this, and then try it out.Save this in a script and then manipulate the plot to make it have a smooth (non-grid
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版 (新課標)必修4 生活與哲學2 用對立統一的觀點看問題教學設計及反思
- 班主任工作心得范文(20篇)
- 2025醫院消防安全個人工作總結(4篇)
- 2025年讀書的心得體會范文(18篇)
- 小學美術贛美版二年級下冊第2課 造型多樣的生活用品教學設計及反思
- 2025-2026年自有房地產經營活動的創新與發展
- 《職場應聘技巧》課件
- 2025酒吧光棍節活動策劃方案(19篇)
- 《氣象萬千:氣候變化與影響》課件
- 富蘊長廊施工方案
- 第六單元實驗活動3創新實驗:二氧化碳的制取與性質一體化實驗說課-2024-2025學年九年級化學人教版上冊
- 工地會議室使用管理制度
- 3000道兩位數進位退位加減法題1
- 2024年東南亞智能聯網電視(Connected TV)市場深度研究及預測報告
- 中西醫結合內科學-主治復習
- 2022年版 義務教育《數學》課程標準
- 2025深圳市中考英語 語法填空 專項復習課件
- 《鐵路職業道德》課件-2.1鐵路職業道德的內涵及規范
- 機器學習課件周志華Chap08集成學習
- 新生兒亞低溫治療護理
- 中醫病歷書寫基本規范
評論
0/150
提交評論