Download presentation
Presentation is loading. Please wait.
Published byAlbert Booth Modified over 6 years ago
1
Matlab review Matlab is a numerical analysis system Can write “programs”, but they aren’t compiled Should still use structured programming Should still use comments Comments are indicated by % at the beginning of the line
2
Matlab’s basic component is a vector
All operations are optimized for vector use Loops run slower in Matlab than in Fortran (not a vector operation) Really good help command
3
Matlab topics and how it differs from Fortran
Syntax Data types Input-output Mathematics Do loops If then structures
4
Syntax No complicated rules Perhaps the most important thing to remember is semicolons at the end of a line surpress output Type more on to keep text from leaving screen too fast
5
Data types All numbers are double precision Text is stored as arrays of characters. You don’t have to declare the type of data (defined when running)
6
Vectors Everything in Matlab is a vector (or matrix) Even single value variables size command gives size of a vector
7
Example Pi= a= » pi » a=[1 2; 3 4] ans = 3.1416 a = 1 2 » size(pi) 3 4
» a=[1 2; 3 4] a = » size(a) ans = Pi= a=
8
Input-output Values can be assigned by typing vector name = value Matlab automatically prints the results of any calculation (unless surpressed by ;) Use disp to print out text to screen
9
To read files in - if the file is an ascii table, use load - if the file is ascii but not a table, file i/o needs fopen and fclose Reading in data from file using fopen depends on type of data (binary or text).
10
Mathematics All the standard operators +,-,*,/,^ Built-in functions too sin(), cos(), exp(), etc. IMPORTANT These operators are vectorized.
11
Example: Vectorized operators
But a*b gives an error because dimensions are incorrect. Need to use .*
12
Likewise
13
Do loops They do exist in Matlab, and sometimes you need them for i=1:n for j=1:n a(i,j)=1/(i+j-1); end
14
Because of vectorization , can often do loops more efficiently
for i=1:3 b=sin(i) end i=1:3 b=sin(i) is much faster
15
If - then also exists The general form of the IF statement is IF expression statements ELSEIF expression ELSE END
16
Possible comparisons are
= = equals < less than > greater than <= less than or equal to >= greater than or equal to ~= not equal to
17
Programming In Matlab, one can write a set of commands in a text file (called an m-file, since the ending must be .m) These commands can be a program Type the name of the m-file in the Matlab environment to run the program
18
Difference betweem scripts and functions
Scripts share variables with the main workspace Functions do not
19
One of the best things about Matlab is graphics
plot is the one you will be using most often many other 3 dimensional plotting functions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.