170 likes | 184 Views
Explore the fundamentals of Matlab – a high-level scientific language for efficient coding and data visualization. Learn about data types, arrays, strings, operators, flow control, loops, functions, and more. Enhance your Matlab skills with examples and tips.
E N D
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 de-bugging. • 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 Matlab Fortran 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 Matlab differentiates between row and column vectors Defines an Array
Comparison Operators Numbers are of the logical type Logical Operators
if, else, elseif Flow Control 1: Conditionals switch
To calculate cos(x) from 0 to 5 To calculate cos(x) from 0 to 5 Flow Control 2: Loops while for To calculate cos(x) from 0 to 5 break, to terminate a loop 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 de-bugged 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.
Functions-Contd.. Inline functions Anonymous Functions
Reading Printing Output Input/Output
1. Creating Arrays 5. Ones Array Manipulation 6. Rand 2. Colon Operator 7. Identity 3. Linspace 4. Zeros
Length Array Functions Size Reshape dot prod sum cross