MatLab Desktop and Programming: Windows, Variables, Matrices, Loops, Functions, and Graphs, Lab Reports of Mechanical Engineering

An overview of the matlab desktop interface, including the workspace, history, and command windows. It also covers variables and assignments, matrix and array creation and manipulation, loops, functions, and graph plotting. Topics include creating arrays, using the size and diag functions, creating identity matrices, transposing matrices, and performing matrix multiplication and element-wise operations.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-ieq-2
koofers-user-ieq-2 🇺🇸

10 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. MatLab Desktop
The default setting of MatLab desktop shows three windows; WorkSpace, History, and
Command windows. What you typed in the command window appears in History window
that can be double clicked to reexecute or can be copied by [ctrl + c] or Copy in the popup
menu (use right mouse button) once it is selected, and then pasted in the Command window
by [ctrl + v] or Paste in the popup menu.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download MatLab Desktop and Programming: Windows, Variables, Matrices, Loops, Functions, and Graphs and more Lab Reports Mechanical Engineering in PDF only on Docsity!

1. MatLab Desktop The default setting of MatLab desktop shows three windows; WorkSpace, History, and Command windows. What you typed in the command window appears in History window that can be double clicked to reexecute or can be copied by [ctrl + c] or Copy in the popup menu (use right mouse button) once it is selected, and then pasted in the Command window by [ctrl + v] or Paste in the popup menu.

2. Variables, Assignment, and Evaluation The variables can have any name from the combination of alphabets and numbers and be assigned any value by equal ( = ) sign. If the line does not end with semi-colon (;), the software echos the result as shown below. Otherwise, there’s no echo of the result. 3. Matrix and Array An array can be defined in two different ways; one by a range separated by colons and the other by a set of numbers in a bracket. Several arrays can be defined in a single bracket to form a matrix separating each array by semi-colon (;) as shown below. The elements may be separated by comma (,) or a space. An element can be identified by indecies within a parenthesis like m(2,3). In MatLab the first index starts at one. A whole range of index can be specified by colon (:) that can be used to select an array in a matrix.

An identity or unit matrix can be created by eye function. The transpose of a matrix is obtained by the single quote operator (‘).

An array can be created with an increment between two values. If the increment is not specified, then the default value of one is used. The following linspace and logspace create the number of elements between two values using linear and log scales.

Arrays can be added together (concatenation) to form a matrix. Special matrices can be created by such functions as zeros , ones , and repmat. Also, a range of elements can be specified if they are same.

Some of important matrix operations are flipud that flips elements up and down and fliplr that flips elements left and right.

Some other functions for an array and matrix are sum , cumsum (adds elements columnwise), det , inverse (^-1), eig (computes eigenvalues), transpose (‘), and linear equation solver () and shown below.

4. Loops (for, while) Loop commands can be used to generate data. The clear commands clears the current memory and erases all previous data.

Another simple plotting function ezplot can accept a function in a string or a handle of a function. The default range of the ezplot is [-2π, 2π].

ezplot('sin(x)') ezplot('sin(t)','cos(t)') %defines x and y in terms of the parameter ‘t’ The ezplot3 is another 3D plotting function that uses three variables, x,y, and z in terms of a parameter.

ezplot3('sin(t)','cos(t)','t') % also, try ezplot3('sin(t)','cos(t)','t',[0,6*pi]) To plot multiple curves, use ‘hold on’ and ‘hold off’ commands. ezplot('sin(x)'); hold on; ezplot('cos(x)'); hold off; Other useful plotting functions are area are fminbnd. f=@(x) sin(x.^2.5)+exp(-x); x=0:pi/100:pi; area(x,f(x)); title('test function'); %adds a title for the graph.

7. Programs A program in MatLab is a set of instructions in a script file that has the file extension ‘m’ like filename ‘prog.m’. A script file can be created newly [File > New > m-file or click the new file icon] or opened from an existing file [File > Open] in the main menu bar. First, open a new script file by clicking the new file icon in the main menu bar. This creates a new script file with a default file name. Now, the ‘Editor’ tab has been added at the bottom of the command window that can be used to switch between the command window and script file. Click the editor tab to show the script file. Then, type in as below to create a function that can be accessed even from other files and save it. The functions created in script files are stored in the harddisk and can be accessed during the MatLab session. Switch to command window and use the function as: >> tmp(1,5) ans = 3 The returned value could be an array or a matrix, too. Open a new script file and make a program as below and save it.

tmp(5) ans = 1 2 3 4 If you need to pass a function as an argument, you can use feval (function evalute) to evaluate a function. The following shows how to use this function. func('sin',pi/2) ans = 1 func(@sin,1) ans =

f = @(x) sin(x.^2.5)+exp(-x) f(1) ans =

func(f,1) ans =

The following is a wrong way to pass a function.

func('x^2-1',1) ??? Invalid function name 'x^2-1'.

disp(iter); disp('f(xr)='); disp(fr); Let’s define a function and find the root.

f=@(x) x^2-1; bisection(0,2,f,0.01,50) iter= 8 f(xr)=

ans =

8. Matrix Solution >> a=[1 2 3; 4 7 5; 11 2 9] a = 1 2 3 4 7 5 11 2 9 >> b=[1 2 3] b = 1 2 3 >> a\b' % solves the matrix equation, but in a little different way from ‘linsolve’ below ans = 0. 0. 0. >> linsolve (a,b') %note b’ is the transpose of the b. ans = 0. 0. 0. 9. Miscellaneous Miscellaneous things are introduced here. >> x=[1 0 2 0 3 0]; >> x x = 1 0 2 0 3 0

size (x) ans = 1 6 for i=1:2:size(x') % x’ : transpose of x array j= floor (i/2)+1; % see also ‘ceil’, ‘int32’, ‘uint32’, ‘mod’ y(j)=x(i); end y y = 1 2 3