150 likes | 160 Views
Learn about the default layout of the MATLAB Desktop, including windows like Workspace and Command Window. Explore basic commands, variable assignments, clearing commands, and executing scripts. Create a simple program to calculate the volume of a cylindrical tank.
E N D
this is the default layout for the Matlab desktop • the default layout contains: • menu bar • tool bar • current directory • workspace (this is a clickable tab) • command history • command window
the various windows may be detached and rearranged using the controls at the upper right corners • to return to the original desktop configuration, select the Desktop/Desktop Layout/Default menu option
Commands or instructions may be typed directly into the Command Window • Variable a has been set to be equal to 3. It is said that the value 3 has been assigned to the variable a. • By default, the result of each command is “echoed” back to the Command Window. The 3rd and 4th lines indicate that the variable a exists and that its current value is 3. That value will remain until another command changes it. • The variables and their current values may also be observed in the Workspace window (click on the tab)
The blank lines in the output on the Command Window can be eliminated by entering the “Format Compact“ command • Assign the value of 6 to a new variable b and note that the output is shown without blank lines • Set the variable c to the sum of a and b • The “echoed” output resulting from executing a command can be eliminated by adding a semicolon to the end of the command • To force the value of a variable to be displayed, simply type the variable name on a command line • If a calculated result is not assigned to a variable then, by default, it is assigned to a variable ans which is short for answer • The command to clear the command window is “clc”
Note the listing of the variables in the Workspace Window • These variables will exist until the workspace is emptied with the “clear” command
We are going to calculate the volume of a cylindrical tank using • Set the values of the variables d (diameter) and h (height) to 3 and 5, respectively. • The value of pi is built into Matlab as fixed constant • Set the variable Area to the tank cross-sectional area • Set the variable Volume equal to the calculated tank volume • The calculated volume can be displayed with much greater resolution by entering the “format long” command
The Command Window is useful, but its not really programming. We want tor record our commands in a file as a series of program instruction and then execute them by accessing the file. • The first step is to set the storage location for our program files, i.e., the “Current Directory.” By default, Matlab, starts with the current directory set to a work area on drive: C of the computer at which you are sitting. You will have to come back to the same computer to continue working on your programs which is not acceptable. • Click on the “Browse” button. • Select a folder on your TechDrive or on a USB Drive to be your current directory • Its good practice to put all of your work in a single Matlab folder with separate folders for each project. • Create a folder Matlab/Project 1.
Note the new current directory • Start the text editor by selecting the File/New/M-File menu option • Enter the program lines in the Editor window for the volume calculation. Be careful to replicate the use of uppercase and lowercase for the variable names as shown. • Select File/Save As and save the file as cylindrical_tank.m • All Matlab files use a .m filename extension and are referred to as M-files. • Note the use of an underline rather than a space in the filename. The simplifies references to the file on the command line.
Click on the Current Directory tab and note the presence of your file. • Right-click on the file name in the Current Directory window and select Run from the popup menu • Note the output in the Command Window • Display of the value assigned to H is a result of not ending that line with a semicolon. The output was suppressed on the other lines by ending them with the semicolon. • The disp( ) function is used to force the display of program output on the Command Window. The first disp() causes the display of the characters enclosed in quotes. • The second disp() causes the display of the value of the variable volume.
Click on the Workspace tab to display the Workspace Window. • Note that in addition to the variables that were used in the program (D, H, area, and volume), the previously defined variables (d, h, Area, and Volume) are still there. • This is a very easy to commit potential error. If we had accidently used the variable d instead of D in the program, the diameter would have been taken as 3 instead of 4. • It is good practice to begin each program with a “clear” command in the M-file so that each program execution starts with a clean workspace.
The program would be more useful if the user could change the values used for the diameter and height. • Modify the program to include the two input() commands as shown. • The input() function displays the prompt that in the parameter list, i.e., between the open and close parentheses. • Type cylindrical_tank on the command line. • Matlab will look in your current directory for an M-file with that name and if it finds one, it will try to execute it. • Respond to the program prompts.
The height of a rock thrown upwards can be calculated from • Create a program rock.m that takes the initial velocity and time as inputs and generates the height as output as shown above.
The power input requirement for a pump can be calculated from • Create a program pump.m that takes the flow rate, pressure jump, and efficiency as inputs and generates the power requirement as output as shown above.
The stretch or change in length of an axially loaded rod can be calculated from • Create a program stretch.m that takes the load, diameter, length, and modulus of elasticity as inputs and generates the length change as output as shown above.