Introduction to Matlab What is Matlab Matlab is
Introduction to Matlab
What is Matlab • Matlab is a high-level scientific language and data visualization. • Advantage over traditional programming languages is the ability for a much cleaner and shorter code and easy debugging. • Disadvantage is that the code is generally slower than compiled programs. • Very fast when code is written in terms of matrix operations.
Fortran and Matlab Fortran Matlab Can be solved using simple command
Data-types and variables • Data-types: int, double, char, logical, function-handle (@function) • Matlab has several in-built characters and variables
Arrays Defines an Array Matlab differentiates between row and column vectors
Strings
Operators
Comparison Operators Numbers are of the logical type Logical Operators
if, elseif Flow Control 1: Conditionals switch
To while Flow Control 2: Loops break, to terminate a loop for To calculate cos(x) from 0 to 5 for loop should be avoided if calculation can be done by vectorized expression.
continue, the control is passed to the next iteration Loops: Contd. . return, function can be forced to exit with this command error, execution of program is forced with a printed message
Functions It has to be saved under the function_name. m Sub functions: Function in the same. m file as the main function Nested function: Function called in the main function. Function to solve the following equation using Newton. Raphson
Functions-Contd. . The second function has to be store in file myfunc. m Will work only when myfunc. m is the file where the function is stored Generally not a good idea to tinker with a debugged code. Any change in the name of myfunc. m will make the code to fail. Good idea is to send the function through arguments so that it can be used for any function.
Inline functions Anonymous Functions. Contd. .
Reading Input/Outpu t Printing Output
1. Creating Arrays 2. Colon Operator 3. Linspace 4. Zeros Array Manipulation 6. Rand 7. Identity 5. Ones
Length Size Reshape dot prod sum cross Array Functions
- Slides: 17