User:Eml4500c.f08.gravy.mmm/Matlab

 Summery of Matlab tutorial by Kermit Sigmon 

Statements expressions and variables
Matlab uses operators, functions and variable names (which are case-sensitive) to complete tasks. When an operation has been completed the results are displayed in matrix form, unless the line ends in a semicolon. The semicolon will suppress the output but the operation will still be carried out. To stop matlab mid operation hit CTRL and C, or CTRL and break.

The workspace saves all variables and their values. The command “who” or “whos” will displace the variables in the workspace. To clear a variable the command is “clear” followed by the variable name, if the variable name is omitted then all variables will be cleared.

Saving a session
To save the variables from the workspace the command is “save”. Save will write the variable in a file called matlab.mat. To load the saved session the command is “load”.

Hardcopy
Saving input from the command window to an alterable and printable file can be done using the command diary. To start recording the command is “diary” then the file name. If you don’t give a file name the default file name is diary. To end recording the command is “diary off”.

Planer plots
The syndics for plot is “plot (x,y)” with x and y being the variables to be plotted. An interval and mesh size can be set by “variable name= lower limit: mesh size: upper limit”.

“fplot (‘function file’,[lower limit, upper limit])” is the command to graph functions. Function file is a m-file that contains the function to be graphed.

Only one figure is active at a time but you can switch between the figures using “figure(n)” where n is the figure number wanted. “gcf” command gives the active figure’s number.

Multiple plots on one graph can be made using “plot(x, y1,x,y2,…)”Other ways are, declaring a matrix of functions like U=[function1, function2 ] and using plot(x,U). Or using the “hold” command to freeze the current plot and all plots after will be placed on the same plot. To unfreeze the plots, the command is  “hold off”.

3-D line plots
To create 3-d plots the command is “plot3(x,y,z)”.

3-D mesh and surface plots
To create 3-D wire mesh surface plots the command is “mesh(z)” and three dimensional facetted surface plots “surf(z)”where z is a matrix of points in the z direction.

To plot a function over a rectangle use first the command “[x,y]=meshgrid(xx,yy)” where xx and yy are matrixes of the sides of the rectangle. x and y result in matrixes whose columns equal xx and yy respectably. Next the command mesh is used with z= exp(-x.^2-y.^2)

The color of the shadings can be changed using the command “shading” followed by the setting. The settings are faceted, interp ( for interpolated), and flat. The color profile is changed using the command “colormap” followed by the color. The color codes are hsv, hot, cool, jet, pink, copper, flag, gray, and   bone.

Labeling the figure
The commands Title, xlabel, ylabel and zlabel, can be used to label the figure. An example is Title(‘Title name’).

gtext(‘The Spot’), command places text where on the figure the mouse is clicked, while the text command places the text at coordinates.

Axis scaling
The following commands are used to change the scaling on the axis.

axis([x lower limit, x upper limit, y lower limit, y upper limit ]), to set scaling.

axis(axis), freezes scaling for subsequent graphs.

axis auto, resets the scale to the default.

Variable= axis, the variable will be set to the current scaling.

axis square, makes the same scale for both axes.

axis equal, make the same scale and tic marks for both axes.

axis off, turns off scaling and tic marks axis on turns them on.

Line types
To change the line type, mark type and color use the tables below. The syntax is Plot(x,y,’color line type mark type’) so Plot(x,y,’b:*’) is a blue dotted line with star markers.

'''

Graphics hardcopy
The command “print” will send the current graphic to the default printer. The command “print ” then the file name will save and overwrite the graphic to a file, but ”print -append” then the file name will add the graphic to the end  of a file.

To save a graphic that is not the current one, the command is “print –deps –f#” then the filename and # the figure number.