Matlab Vit
Matlab Vit
Rohit Pathak
Satyadhar Joshi
www.nanotechbiz.org
Contents
What is MATLAB?
Overview
Elementary Mathematics
Programming
I/O
M-Files
Basics of M-File
Function and Script Files
M-File Programming
Graphics
2D Plots
3D Plots
What is MATLAB ?
• It stands for MATrix LABoratory
▫ >> whos
▫ Name Size Bytes Class Attributes
▫ ans 1x1 8 double
▫ fid 1x1 8 double
▫ i 1x1 8 double
Variables Basics
▫ >> 16 + 24
▫ ans =
▫ 40
▫ >> sum(diag(durer))
▫ ans =
▫ 34
Other Diagonal using Flip Left-Right
▫ >> durer
▫ durer =
▫ 16 3 2 13
▫ 5 10 11 8
▫ 9 6 7 12
▫ 4 15 14 1
• Unformatted I/O
▫ fread: Read binary data from file
▫ fwrite: Write binary data to a file
• Formatted I/O
▫ fgetl: Return the next line of a file as a string without line
terminator(s)
▫ fgets: Return the next line of a file as a string with line
▫ terminator(s)
▫ fprintf: Write formatted data to file
▫ fscanf: Read formatted data from file
Low-Level File I/O
• File Positioning
▫ feof: Test for end-of-file
▫ ferror: Query MATLAB about errors in file input
or output
▫ frewind: Rewind an open file
▫ fseek: Set file position indicator
▫ ftell: Get file position indicator
•
• String Conversion
▫ sprintf: Write formatted data to a string
▫ sscanf: Read string under format control
Example
• fid = fopen(‘filename’, ‘permission’);
• status = fclose(fid);
• frewind: frewind(fid);
▫ rewinds the file
▫ >> {exam.name}
▫ ans =
▫ 'Jim Kirk' 'Janice Lester'
The MAX and MIN functions
• [Y,I] = MAX(X) returns the indices of the maximum values in vector I. If the
values along the first non-singleton dimension contain more than one
maximal element, the index of the first one is returned.
▫ >> max(rpm_raw)
▫ ans =
▫ 1115 1120 1043
▫ >> max(max(rpm_raw))
▫ ans =
▫ 1120
▫ >> min(min(rpm_raw))
▫ ans =
▫ 961
▫ >> roots(p)
▫ ans =
▫ 12.1229
▫ -5.7345
▫ -0.3884
Using Symbolic Computation Toolbox
• syms or sym is used to declare symbols
▫ >> syms x y
▫ >> y = x^4 + x^2 + (x*2) + 87
▫ y=
▫ x^4 + x^2 + 2*x + 87
▫ >> syms x y
▫ >> y = x^2 + x + 1
▫ y=
▫ x^2 + x + 1
▫
▫ >> differentiation = diff(y, x)
▫ differentiation =
▫ 2*x + 1
▫
▫ >> integration = int(y, x)
▫ integration =
▫ (x*(2*x^2 + 3*x + 6))/6
>> x = [0:pi/100:pi];
>> y = sin(x);
>> plot(x,y), title('Simple Plot')
>> z = cos(x);
>> plot(x,y,'g.',x,z,'b-.'),
title('More Complicated')
>> t = 0:pi/25:pi;
>> [x,y,z] =
cylinder(4*cos(t));
>> subplot(2,1,1)
>> contour(y)
>> subplot(2,1,2)
>> mesh(x,y,z)
>> xlabel('x')
>> ylabel('this is the y axis')
>> text(1,-2,0.5,...
'\it{Note the gap!}')
Used to display multiple
plots in the same figure
window
subplot(m,n,i)
subdivides the window into
m-by-n subregions
(subplots) and makes the
ith subplot active for the
current plot
>> subplot(2,3,1)
>> plot(t, sin(t), 'r:square')
>> axis([-Inf,Inf,-Inf,Inf])
>> subplot(2,3,3)
>> plot(t, cos(t), 'g')
>> axis([-Inf,Inf,-1,1])
>> subplot(2,3,5)
>> plot(t, sin(t).*cos(t), 'b-.')
>> axis([-Inf,Inf,-Inf,Inf])
mesh(Z) generates a wireframe
view of matrix Z, where Z(i,j)
define the height of a surface over
the rectangular x-y grid
>> figure(2);
>> [X,Y] = meshgrid(-
16:1.0:16);
>> Z = sqrt(X.^2 + Y.^2 +
5000);
>> mesh(Z)
surf(Z) generates a colored
faceted 3-D view of the surface.
By default, the faces are
quadrilaterals, each of constant
color, with black mesh lines
The shading command allows you
to control the view
>> figure(2);
>> [X,Y] = meshgrid(-
16:1.0:16);
>> Z = sqrt(X.^2 + Y.^2 +
5000);
>> surf(Z)
>> shading flat
>> shading interp
>> colormap hot
• My startup.m file:
▫ addpath e:\download\MatlabMPI\src
▫ addpath e:\download\MatlabMPI\examples
▫ addpath .\MatMPI
▫ format short g
▫ format compact
MATLAB’s path
• The addpath command adds directories to the MATLAB search
path. The specified directories are added to the beginning of the
search path.
• rmpath is used to remove paths from the search path
▫ >> addpath('c:\');
▫ >> matlabpath
▫ MATLABPATH
▫ c:\
▫ E:\MATLAB\R2006b\work
▫ E:\MATLAB\R2006b\work\f_funcs
▫ E:\MATLAB\R2006b\work\na_funcs
▫ E:\MATLAB\R2006b\work\na_scripts
▫ E:\MATLAB\R2006b\toolbox\matlab\general
▫ E:\MATLAB\R2006b\toolbox\matlab\ops
Some common commands
• ls / dir provide a directory listing of the current directory
▫ >> ls
▫ . .. sample.m
▫ >>
▫ >> pwd
▫ ans =
▫ e:\Program Files\MATLAB\R2006b\work
▫ >>
Some common commands
• The system command can be used to run OS commands
• On Unix systems, the unix command can be used as well
• On DOS systems, the corresponding command is dos
▫ >> dos('date')
▫ The current date is: Thu 01/04/2007
▫ Enter the new date: (mm-dd-yy)
▫
▫ ans =
▫ 0
Links of Online MATLAB Resources
• www.mathworks.com/
• www.mathtools.net/MATLAB
• www.math.utah.edu/lab/ms/matlab/matlab.html
• web.mit.edu/afs/athena.mit.edu/software/matlab/
• www/home.html
• www.utexas.edu/its/rc/tutorials/matlab/
• www.math.ufl.edu/help/matlab-tutorial/
• www.indiana.edu/~statmath/math/matlab/links.ht
ml
• www-
h.eng.cam.ac.uk/help/tpl/programs/matlab.html
Thanking You
Rohit Pathak
Satyadhar Joshi
Salman Ahmed
Ripudaman Singh
www.nanotechbiz.org