MATLAB Programming/Graphics

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

MATLAB Programming

Chapter 1: A Tutorial Introduction

Chapter 2: Basic MATLAB Concepts

Saving and loading a MAT-file
MATLAB's Command Prompt
Basic Reading and Writing Data from a File

Chapter 3: Data Storage and Manipulation

Data Types and Operations on Point Values

Boolean and Rational
Strings
Portable Functions
Complex Numbers

Arrays and Matrices

What is an array?
Introduction to array operations
Vectors and basic vector operations
Struct Arrays
Cell Arrays
Sparse Matrices

Chapter 4: Graphics

Basic Graphics Commands
Annotating Plots

Chapter 5: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
MATLAB Caveats
Debugging M Files

Chapter 6: Mathematical Manipulations

Linear Algebra

Simple matrix manipulation
More complicated matrix operations

Differential Equations

Ordinary Differential Equations
Partial Differential Equations

Chapter 7: More advanced I/O

Writing and Reading to a Serial Port
Writing to a USB Port

Chapter 8: Examples

Filtering
Controls
Phase Vocoder and Encoder

Chapter 9: Object-Oriented Programming

Struct arrays
MATLAB Classes

Chapter 10: An alternative to MATLAB: Octave

What is Octave ?
Octave/MATLAB differences

Chapter 11: Toolboxes

Symbolic Math Toolbox
GUIDE
Simulink
Psych Toolbox

edit this box


Contents

[edit] 2D Graphics

[edit] Plot

Plots a function in Cartesian Coordinates, x and y.
Example:

x=0:0.1:2;  % creates a line vector from 0 to 2
fx=(x+2)./x.^2; % creates fx
plot(x,fx,'-ok') % plots 2d graphics of the function fx 

To plot 2 or more graphs in one Figure, then simply append the second (x,y) pair to the first:

>>>x1 = [1,2,3,4]
>>>y1 = [1,2,3,4]
>>>y2 = [4,3,2,1]
>>>plot(x1,y1,x1,y2)

This will plot y1 and y2 on the same x-axis in the output.

[edit] Polar Plot

Plots a function using θ and r(θ)

t = 0:.01:2*pi;
polar(t,sin(2*t).^2) 

[edit] 3D Graphics

[edit] plot3

The "plot3" command is very helpful and easy to see three dimensional images. It follows the same syntax as the "plot" command. If you search the MATlab help (not at the command prompt. Go to the HELP tab at the top of the main bar then type plot3 in the search) you will find all the instruction you need.

Example:

l=[-98.0556  ; 1187.074];       
f=[ -33.5448 ; -240.402];       
d=[ 1298     ; 1305.5]           
plot3(l,f,d); grid on;

This example plots a line in 3d. I created this code in an M-file. If you do the same you change the values and hit the run button in the menu bar to see the effect.

[edit] Mesh

Creates a 3D plot using vectors x and y, and a matrix z. If x is n elements long, and y is m elements long, z must be an m by n matrix.

Example:

x=[0:pi/90:2*pi]';
y=x';
z=sin(x*y);
mesh(x,y,z);

[edit] Contour

Creates a 2D plot of a 3D projection, using vectors x and y, and a matrix z. If x is n elements long, and y is m elements long, z must be an m by n matrix.

Example:

x=[0:pi/90:2*pi]';
y=x';
z=sin(x*y);
contour(x,y,z);

[edit] Contourf

Same as contour, but fills color between contour lines

[edit] Surface

Basically the same as mesh