Follow by Email

Monday, May 27, 2013

Learn MATLAB with examples

I know it that most of you must be thinking of MATLAB as a programming language and that it is going to be very boring to learn the syntax, data types, functions, and various other aspects of MATLAB.

Well MATLAB has all these boring aspects, but we shall not go into those details directly. I would rather get you acquainted with these programming aspects while you use them. This implies that we will do the practicals to learn, than just going through the boring and complicated plain text about these technical aspects.

Having said that let's get familiar with the MATLAB interface.

Monday, May 20, 2013

Why and Where to choose MATLAB as programming language?

          This is the most important question to be answered. One should be very clear about the application, so as to know which programming language suits the task, best. MATLAB is most known for its numerical computation ability. So most probably it is used in applications where a lot of numerical computations are to be performed. Following is the questionnaire that you must ask yourself while opting MATLAB.

  • Numerical Computations: 

    MATLAB is a good choice if your application has a lot of numerical calculations involved. Of course your calculator can also do that but it definitely can't handle variables right?? All basic mathematical operations like addition subtraction multiplication, etc are available in MATLAB. Not only that, MATLAB can handle the input as a combination of variables, that is it can handle expressions. It uses the BODMAS rule for solving such expressions.

  • Complex Calculations: 

    If your programs use calculations such as sine, cosine, imaginary numbers, integration, derivative, polynomials, transfer functions, finding roots, curve fitting, interpolation, etc.

  • Repeated/Recursive algorithms:

    There are a lot of times when some operation is to performed repeatedly. Consider the difference between the Interest credited by your bank (compound interest) and simple interest. The concept of compound interest repeatedly applies the simple interest concept on the new amount over each interval (monthly, quarterly, etc.).

  • Fast processing (Block-wise operation rather than just 8 bit):

    The best part of MATLAB is that it can operate on array, matrices and vectors as a whole block and not element by element functioning. This function thus improves the speed of operation of the program developed by. This block processing capability of MATLAB is what differentiates it from the rest of programming languages.

  • Simulation purposes:

    The output of any particular operation can be simulated without the actual hardware. Thus simulation helps you to correct any errors or problems in your design before finalizing it. Each block of your operations can be tested/simulated individually using the "cell mode". The results of each simulation can be stored in the MATLAB directory in the form of ".mat" files; this enables reference of functionality of the various versions of your program. It is common to include MATLAB simulation results in research paper publications, journals, thesis.

    MATLAB provides a number of inbuilt easy-to-use data representation tools such as:

    1. Display result as text in the command widow.

    2. Graphs: Using tools such as plot, ezplot, contour, surf, etc the required data can be represented in the form of  two or three dimensional graphs with custom legend.

    3. File output: The data can be directly written into file of your choice such as text, table, Spreadsheet, ".mat" file, image file(eps, bmp, jpg, png, tiff), audio file, movie file.

    4: Graphical User Interface (GUI): A custom GUI can be made manually or by using MATLAB's inherent GUI- Development Environment (called as GUIDE). The data can be input as well as output with the help of GUI, giving a pleasing appearance. Even the graphs or images can be included in the GUI itself.

  • Mathematical modelling:

    Your concept should have a mathematical model so that it can be tested in various conditions for various types and values of inputs. With the help of Simulink blocks, MATLAB toolboxes or by using basic MATLAB commands the mathematical model of your concept can be generated. The model can then be simulated for various conditions as inputs to analyse the functionality and make appropriate changes if necessary before finalizing.

  • Logical computations

    In case your application involves logical operations such as comparison of numbers, variables for conditions such as greater than, smaller than, is equal. There are conditions when one needs to analyse the variable type and then proceed with the further code; such conditions are: is number, is alphabet, is hexadecimal number. This can be very easily implemented in MATLAB by using the inbuilt functions.

  • Various datatypes involved in same program

    It is a bit irritating to handle multiple variables with distinct data types in the same program, especially if you are not a hard-core programmer. The situation becomes even worse if these variables are inter-dependent. MATLAB is of great help in this condition because a program in MATLAB does not necessarily need "Declaration" of data-type of variables. Also it dynamically allocates then appropriate data type. Hence if you try to store a "float" variable into a variable that was "integer", yes it is possible and does not give any error. But to ensure reliability of the program, it is recommended that you should stick to the required/designed data type rather than wandering data types.

    Not only can MATLAB handle various data-types, but it can also interpret various data formats and file formats such as images(bmp, eps, png, jpg, tiff), audio and video files, etc with extreme ease for you by using the inbuilt data read and write functions specific to the applications. You don't have to worry about the code to read or write a specific file format if it is supported by MATLAB.

Above are few of the many situations your application may have and you may be confused about choosing the right programming language. This should help you in considering MATLAB if your application is intensive.

Please note: MATLAB has variety of functionality, but choosing it is best recommended if your task is indeed very computation intensive for other programming and simulation tools. If it is not so intensive you may give MATLAB a miss and choose tools such as "C" language.