MATLAB (MATrix LABoratory) Chapter 2
First: Check guidelines for accessing MATLAB.
Second: Examine all the windows in the MATLAB open screen.
Discuss three ways to save work in MATLAB
1. Diary: Type diary day1scott (Use your name here). At the end of the session type diary again. This saves in a text file everything you you did in the command window during a particular work session. This text file can be opened up in any other text editor on Unix, MAC, or PC. You use diary for your purposes only so you can review your work. Never submit to me a diary or anything that looks like a diary. It has the all the errors and all the elements of each matrix. You will submit to me your sctipt M or .m files described in #3 below.
2. Saving Variables that are in the Workspace Window: It is helpful sometimes to save a set of variables that could be useful to you later rather than having to type them all over again. Click in the workspace window and go to the File>Save Workspace As... then give the file an appropriate name. To load the file back into the workspace, go to File>Open, or go to Current Directory window and click on the saved file. YOu can also drag the file to the Workspace. This is also a text file that can be read by any text editor.
These files are for your purposes only. Do not submit to me a diary or saved variables. Only submit script M-files
3. Script M-Files also known as .m files: An example and description of this is found in the second column of the table below. You will submit to me your .m files to the appropriate folder in my account. Keep one copy for yourself.
ans: Default variable in command window
scalar: A single value that is a 1 x 1 array, one row by one column.
scalar operations : The same as the operations in arithmatic and C++. (a to the b power is a^b)
row vector: a = [4 12 6 9] produces a 1x4 array
4 12 6 9
column vector: b = [5; 11; 7; 8] a 4x1 array
matrix: c = [4 2 6 9; 8 3 1 5; 4 1 7 0]
a 3x4 array
2 6 9
8 3 1 5
4 1 7 0
Try: disp(c) This is helpful for clean output.
What is the difference between c and disp(c)
Array or matrix operations with scalars Ex: a+5
Add 5 to every element of a produces:
9 17 11 18
array or matrix with element by element operations.
Given d=[1 2 2 1; 3 2 1 3; 1 1 3 2] we can calculate: c+d, c-d, c.*d, c./d
We can also calculate d.^2 or c.^d
Note when the . notation is necessary and why.
(The dot (.) notation forces operations by a scalar rather than matrix multiplication which requires special configuration of 2 matrices and has special purposes. There ar no dot (.) notation if c and d are scalars.
What happens with each of the following:
g = [5:12]
h = [2:5:25]
table =[a', b]
Using menu, label x-axis and y-axis.
Examine other menus.
p = linspace(2,16,4)
q = logspace(1,4,4)
Pay special attention to the Tools->edit plot. Select line and right click to change line properties.
Creating, Saving and Running a script M-file
Problem: Create a conversion table of pounds force to newtons. The table will start at 0 aand go to 1000 lbf at 100 lbf intervals. the conversion factor is: 1 lbf = 4.4482216 N
- Go to File>New>Blank M-File or File>New>Script. There is also an icon for a new script file under the File menu.
- Use % to comment your code, ID, and specs.
%April 2, 2012
%Desired output: a table converting pounds force(lbf) to newtons(N)
%Given input: Start value, end value, increment values between in lbf and conversion formula
Equations: lbf = [0:100:1000] and N= lbf * 4.44822
- For block of several comments do the following:
- Highlight the block to be commented out.
- Select Text from the menu bar.
- Choose Comment from the Text drop-down menu.
- Notice that there is a choice for Uncomment as well.
- Here is the code for the .m file. Notice the similarities to c++.
clear,clc % Every file must begin with this.
lbf = [0:100:1000];
N = lbf * 4.44822;
Table_lbfToN = [lbf',N'] %This is the output. No semicolon.
- Use semicolon (;) to surpress output after each input line.
- Do not use semicolon (;) when you want a display of output.
Better yet, use disp(something)to show output.
- Save file as a .m file. Example: SeritaSsample.m or...
- Click the green run button. If file is not saved you will be asked for a file name and it will be saved before the run.
- Check the command window of Matlab for the answer.
- Copy output and put at the end of your .m file as a group of comments. Label the output clearly with comments.
- If you do not like your output in scientific notation you can choose other formats listed on pp. 41 &42. For example: type format bank before typing formula for output.
top ,Chapter 2 Homework, Chapter 2 Assignments