User:Eml4500.f08.ateam.rivero/HW1

=Homework Assignment 1: MatLab Introduction Matlab Primer, University of Florida =

Section 2: Entering Matrices
Matrices in MATLAB can be entered in several different ways. Below are two examples that output the same matrix.

>> A=[1,2,3;4,5,6;7,8,9]

A = 1    2     3     4     5     6     7     8     9

>> A = [ 1 2 3 4 5 6 7 8 9 ]

A = 1    2     3     4     5     6     7     8     9

Entering Matrices with complex numbers. Below are two different ways to enter complex matrices.

>> B = [1 2;3 4] + i*[5 6;7 8]

B =

1.0000 + 5.0000i  2.0000 + 6.0000i 3.0000 + 7.0000i  4.0000 + 8.0000i

>> B = [1+5i 2+6i;3+7i 4+8i]

B =

1.0000 + 5.0000i  2.0000 + 6.0000i 3.0000 + 7.0000i  4.0000 + 8.0000i

Referencing an element from a matrix. For example in the matrix A above, if you want to reference the value of the element located in row 2, column 3 type A(2,3). View example below.

>> A(2,3)

ans =

6

Section 6: For, while, if - and relations
For.

Using the For. statement to produce a vector. Example for n = 3

>> x = []; for i = 1:n, x=[x,i^2], end

or

>> x = []; >> for i = 1:n x = [x,i^2] end

Will result in:

x =

1

x =

1    4

x =

1    4     9

Producing the same vector as shown above, but in reverse order. Example is shown below.

>> x = []; for i = n:-1:1, x=[x,i^2],end

x =

9

x =

9    4

x =

9    4     1

While.

A while loop allows for the statement to be executed continuosly aslong as the relation defined remains true.

General Form:

while relation statement end Example:

>> a =30; >> n = 0; >> while 2^n < a    n = n + 1; end

Will result in:

>> n

n = 5

If.

An if statement is only executed if the relation is true.

General Form:

if relation statement end

Example:

>> if n < 0 parity = 0; elseif rem(n,2) == 0 parity = 2; else parity = 1; end

Relations

MATLAB Relational Operators:

MATLAB Logical Operators:

If the statement is true it is a value of 1. If the statement is false it is a value of 0. See the examples below.

>> 3 < 5

ans =

1

>> 3 > 5

ans =

0

>> 3 == 5

ans =

0

>> 3 == 3

ans =

1

>> a = rand(5)

a =

0.9501   0.7621    0.6154    0.4057    0.0579    0.2311    0.4565    0.7919    0.9355    0.3529    0.6068    0.0185    0.9218    0.9169    0.8132    0.4860    0.8214    0.7382    0.4103    0.0099    0.8913    0.4447    0.1763    0.8936    0.1389

>> b = triu(a)

b =

0.9501   0.7621    0.6154    0.4057    0.0579         0    0.4565    0.7919    0.9355    0.3529         0         0    0.9218    0.9169    0.8132         0         0         0    0.4103    0.0099         0         0         0         0    0.1389

>> a == b

ans =

1    1     1     1     1     0     1     1     1     1     0     0     1     1     1     0     0     0     1     1     0     0     0     0     1

Section 10: Command Line Editing and Recall
Keys and their functions to edit in MATLAB

Section 14: Managing M-Files
It is recommended that if your system allows you to run multiple processes that you keep both the MATLAB Command Window and your m-file editor open simultaneously. At any point if you would like to edit your m-file use the function !ed. For example you would type into the command window as follows: >>!ed filename.m

Note: Make sure to replace filename with the file you are trying to edit.

Commands useful for managing m-files.

Planar Plots
The plot command creates linear x-y plots; if x and y are vectors of the same length, the command plot(x,y) opens a graphics window and draws an x-y plot of the elements of the x versus the elements of y.

For example command: >> x = -4:.01:4; y=sin(x); plot(x,y)

Results in:

One can also refrence a function from an m-file in the given format: >>fplot('expnormal',[-1.5,1.5])

Results in:

Plots of parametrically defined curves can also be made. For example:

Commands for plots:

Manualing Scaling Axes:

Multiple Plots on a Single Graph: Two possible ways

1. Example: Listing them within the same command line >> x=0:.01:2*pi; Y=[sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y)

Result in:

2. Example: Using the hold function >> x=0:1:10; y=x; plot(x,y) >> hold on >> x=0:1:10; y=2*x; plot(x,y) >> x=0:1:10; y=3*x; plot(x,y) >> hold off

Result in:

Editing Linetypes, Marktypes, and Colors of plots:

Commands:


 * Linetypes: solid(-), dashed(--), dotted, dashdot(-.)
 * Marktypes: point(.), plus(+), star(*), circle(o), x-marks(x)
 * Colors: yellow(y), magenta(m), cyan(c), red(r), green(g), blue(b), white(w), black(k)

Example Code: >> x=0:.01:2*pi; y1=sin(x); y2=sin(2*x); y3=sin(4*x); plot(x,y1,'--',x,y2,':',x,y3,'+')

Graphics Hardcopy
To obtain a hardcopy of your plots you would use the commmand: >>print

3-D Line Plots
To plot a 3-D line defined by vectors x, y, z of the same size, you would use the command plot3(x,y,z). Below is an example:

>> t=.01:.01:20*pi; x=cos(t); y=sin(t); z=t.^3; plot3(x,y,z)

Result in:

3-D Mesh and Surface Plots
3-D Surface Mesh plots are drawn with the command mesh

Example Code:

>> mesh(eye(10))

Result in:

Faceted surface plots are drawn with the command surf

Example Code:

>> surf(eye(10))

Result in:

Plotting 3-D Functions: Function:



Code:

>> xx = -2:.2:2; >> yy = xx; >> [x,y] = meshgrid(xx,yy); >> z = exp(-x.^2 - y.^2); >> mesh(z)

Resulting in:

One could also use the surf command for the same function resulting in: Code:

>> surf(z)

Shading and Color Profiles

Commands:

*shading faceted *shading interp *shading flat

*colormap(hsv) Note: Default *colormap(hot) *colormap(cool) *colormap(jet) *colormap(pink) *colormap(copper) *colormap(flag)

Other Useful Commands: Can further explore under the help command

*meshz *surfc *surfl *contour *pcolor

*colormap(gray) *colormap(bone)