230 likes | 389 Views
AM2032 JAYANTA MUKHERJEE. Vectorization. All the built in MATLAB functions are optimized for vector operations. Consequently vectorized commands or codes run much faster in MATLABVectorization refers to a manner of computation in which an operation is performed simultaneously on a list of number
E N D
1. AM2032 JAYANTA MUKHERJEE
2. AM2032 JAYANTA MUKHERJEE Vectorization All the built in MATLAB functions are optimized for vector operations. Consequently vectorized commands or codes run much faster in MATLAB
Vectorization refers to a manner of computation in which an operation is performed simultaneously on a list of numbers (a vector /array) rather than sequentially on each number of the list.
For example, let ‘ ’ be a list of 100 numbers.Then the statement y=sin( ), is a vectorized statement as opposed to y1=sin( 1),y2=sin( 2), …e.t.c.
3. AM2032 JAYANTA MUKHERJEE Loops and Iterations N=10;
for I = 1:N
for J = 1:N
A(I,J) = 1/(I+J-1);
end
end
4. AM2032 JAYANTA MUKHERJEE
5. AM2032 JAYANTA MUKHERJEE
6. AM2032 JAYANTA MUKHERJEE
7. AM2032 JAYANTA MUKHERJEE
8. AM2032 JAYANTA MUKHERJEE A script file is an ‘M-file’ with a set of MATLAB commands in it.
A script file may contain any number of commands, including those that call the built in functions or functions written by you.
The MATLAB editor is used to write the scripts. Press F5 to run your script.
Never name your script the same as the name of a variable it computes otherwise MATLAB will have problems accessing the file.
Avoid writing anything in your script that clashes with built-in functions.
Save your script as a m-file. Save it always in the ‘WORK’ directory/folder .Otherwise MATLAB will have problems in accessing that file.
Avoid any space between the letters or numbers when you name your m-file. MATLAB doesn't like it and will create unnecessary problems.
9. AM2032 JAYANTA MUKHERJEE
10. AM2032 JAYANTA MUKHERJEE
11. AM2032 JAYANTA MUKHERJEE A function file is also an m-file, like a script file.
Function files are like programs or subroutines in FORTRAN, procedures in PASCAL and functions in C or C++.
Once you get to know MATLAB well this is where you are likely to spend most of your time
—writing and refining your own function files.
A function file begins with a function definition line, which has a well defined list of inputs and outputs. Without this line the file becomes a script file.
12. AM2032 JAYANTA MUKHERJEE
13. AM2032 JAYANTA MUKHERJEE
14. AM2032 JAYANTA MUKHERJEE
15. AM2032 JAYANTA MUKHERJEE Executing a Function There are two ways in which a function can be executed, weather it is in built or user written.
(1)With explicit output, (2) Without any output.
With explicit output: This is the full syntax of calling a function. Both the output and the input list are specified in the call. Example: if the function reads:
function [rho, H, F] = motion (x, y, t);
then all the following commands represent legal call (execution) statements:
16. AM2032 JAYANTA MUKHERJEE Executing a Function-cont.
17. AM2032 JAYANTA MUKHERJEE Sub-functions Allows more than one function to be within the same M-file (modularize code)
M-file must have the name of the first (primary) function
Sub-functions can only be called from within the same M-file
Each sub-function has its own workspace
18. AM2032 JAYANTA MUKHERJEE
19. AM2032 JAYANTA MUKHERJEE Debugging (Visual)
20. AM2032 JAYANTA MUKHERJEE Miscellaneous Stuff MATLAB supports reading an entire file and creating a matrix of the data with one statement.
>> load my_data.dat; % loads file into matrix.
The matrix may be a scalar, a vector, or a matrix with multiple rows and columns. The matrix will be named my_data.
>> size (my_data)
% size will return the number of rows and number of columns in the matrix
>> length (my_data)
% length will return the total no. of elements in my_data
21. AM2032 JAYANTA MUKHERJEE TIPS Use Pseudo Codes, Flow charts etc to chalk out a plan for your code.
Keep your functions modular, that is break big computations into chunks and write separate functions for them. Also try to keep the length of functions small.
Make it a habit of writing comments. Its always useful.
22. AM2032 JAYANTA MUKHERJEE Lab Sessions Monday 2PM
Tuesday 3PM
Wednesday 12PM
This is how it was done last year. If anybody has any suggestion regarding the time and days please let me know.
There will be three groups with 30 students (approx) each divided alphabetically according to their names.
23. AM2032 JAYANTA MUKHERJEE