Project

Basics of MATLAB

PROJECT NAME – MATLAB BASICS

AIM: -

To find out the results by executing different MATLAB commands and experiencing the syntax of the MATLAB.

OBJECTIVES: -

  1. To build strong foundation of MATLAB syntax.
  2. Understanding of MATLAB GUI.
  3. To understand basic functions and commands in MATLAB.
  4. To understand basic coding and its rules for MATLAB.

The GUI of MATLAB has –

  1. Command window.
  2. Editor window.
  3. Workspace.
  4. Current folder.
  1. Command window –

Command window in MATLAB is like a basic calculator for a user which is used to calculate the basic commands for e.g. 5+3 = 8, but it’s a temporary calculator and temporarily shows out the information. Command window basically displays a command prompt of double greater sign (>>) and after typing the information if you press the enter key answer will be displayed. For e.g. the simple command 6+5 is executed and below output is shown in command window (fig. 1)

As shown the result is stored in variable name ‘ans’ this happens when we don’t designate the variable. To show it lets assign the variable and we will execute the command and the result is shown in fig. 2

When you see this you will realise this time the value is stored to a variable assigned to it. And also its not recommended to use operations in a MATLAB without assigning variables to them because every time you perform the operation value of answer will be overwritten and also its very irritating for all the results to be displayed instantaneously in command window so to neglect this effect you can terminate the statements with the semicolon (;).

We can bring previous command for editing by pressing the up arrow on keyboard in command window.

Fig. 1 – command processed by MATLAB IN command window (without variable).

Fig. 2 – command processed by MATLAB IN command window (with variable)

  1. Editor window –

In order to create the proper programme which can edited, saved and can be used anytime whenever needed then we have to use editor window and the file saved in the editor window automatically assigned to a .m file editor window have a script file in order to open a script file user have to go to script option provided at the right corner of the MATLAB, and select the new script which will be opened in the editor window and the result of the editor window will be shown in the command window after you execute the programme in editor window by pressing the RUN button. (fig.3)

Fig. 3 – commands in editor window and their results

3.Workspace –

Variables in MATLAB that are generated while performing commands like value, class, data type and many more are contained in the workspace and while executing the commands the variables are overwritten in the workspace and when the values keep updating the editor window or command window with new variables and their values.

Workspace is shown in fig (4)

Fig (4) – overview of the workspace

  1. Current folder –

Current folder is the quickest way to access all the files that are being saved in the MATLAB and it is also very important that in order to run any file that file must be in a current folder and or in a search path the current folder is shown in the fig. (5).

Fig. (5) - Current folder in MATLAB

Functions with respect to MATLAB and OCTAVE-

Y = f(x)

So f is a function of x and y is the output and x is the input we can also write it as y1 = f(x,y) = x2 + y2 this is called as function definition and if I have to call the function then I have to call the function for which I have to pass the right values

Function having 3 input y = (x,y,z) and equation is x2 + y2 –z now if I have to call the function then f(1,2,5) that is x = 1 , y = 2, z = 5 so we should match the order of inputs with order in which the function has been defined and this is what the computer programme will be going to do the programme will going to compare your function called with function definition and it will detect that there is something wrong and it will throw you some error

For e.g. y1, y2 = f1(x1, x2)

y1 = x + y

y2 = x-y

therefore, f1(x, y) and y1 and y2 = f1(5,5) you will get values y1 = 10 and y2 = 0

one thing should be always remembered that when you save the programme then the name of the file and name of the function is exactly the same and the fig (6) is shown in which the function is run and you can see that in command window output variable is called as results and in editor window output variable written as y so it doesn’t matter just the number of input and output should match.

Fig. (6) - Basics of function command

Now let’s perform another functions in editor window if we type or edit

Function [y, y1] = f1(x, x1)

y = x;

y1 = x+x1;

end

and in command window we will assign the values of x and x1 in terms of an and b like a = 5 and b = 6 and in command window we will type [result1, result2] = f1(a, b) and results will be seen in the command window. One thing should be taken care is that your main function and main programme are in the same folder and other thing should be make sure that the name of the function and the name from which the function is to be saved should be match otherwise MATLAB will show error while running the file.

Fig. (7)- Use of function command with more values

Creating Arrays-

The most important and fundamental part of MATLAB programming is creating arrays and arrays are used to store the data. In array list of numbers are arranged in a rows or columns. There are 3 most common types of arrays which are used in MATLAB

1 – Single element array (only 1 column and one row)

2- Row vector

3 – column vector

You will see the examples of each in the following fig.

To operate on single element, you just have to type that single element with assigned variable for e.g. a = 5.

To operate on row vector, you just have to type the elements with equal spacing between two elements

To operate on column vector you just have to type elements with a semicolon (;) between them.

Fig (8) – use of singular vector, row vector and column vector

Now if we want to create an array having n number of columns and m number of rows then we have to type [300 459 495; 698 972 693; 523 653 658] then in command window you will see the results such as a matrix of 3 rows and 3 columns

Now if you want to access the 3rd row only then we have use the command as (3, :) and command window will show us result similarly for column (:,3) and it will be shown as given in the figure

Fig (9) – Matrix generation and pointing specific elements in matrix

Now suppose if you want to extract a matrix which has 2 rows and 2 columns so type in editor window b(1:2,1:2) so you will get 2 rows and 2 columns in command window as per the following fig.

Fig (10) Extracting specific components in a matrix

2D Arrays –

So basically 2D array is a matrix is done easily by meshgrid command go to the MATLAB editor window create arrays for x and y

x = linspace (0,1,4);

y = linspace (0,2,4);

now to calculate 2D representations

[xx yy] = meshgrid(x,y)

When you will run this command you will see -

Fig (11) Generation of 2D Arrays

Loops –

We are going to use for loops when we are repeating set of actions again and again. A loop can be defined as when execution of command or group of command executes several times consecutively in for end loops the number of passes is specified when the loop starts

e.g. for t:p:s

…………… a group of MATLAB commands

……………

……………….

End

T is the value in the first pass, p is the increment of t in each pass and s is the value of last pass example of for loop is attached in fig.

Fig(12)- Execution of for end command

Basics of line plots –

Plotting helps us to visualize the data. It is very useful tool to present information and with the help of MATLAB we can create many different types of plots also we cn customize the line thickness, grid lines can be added as can title and text comments also plot command can be used to create simple 2D plots and the simplest form of the command is plot(x,y)

To change the color, thickness, properties we have to use

Plot(x,y, ‘line specifier’, ‘property name’, ‘property value’)

Line specifiers define the color and type of the line and markers

Property values that are used to specify line width and fill colors

A simple graph can be plotted as –

Fig (13) Basics of plot command

2D Plots-

While using 2D plots we will use meshgrid command

For plotting 2D graph

x = linspace(-1,1,10);

y = linspace(-1,10,20)

[xx,yy] = meshgrid (x,y);

zz = xx2 + yy2;

contour(xx,yy,zz)

and with the help of surf command you can plot 3D graph also

Fig (14)- 2D plotting

And now just by changing the command name from contour to surf we can plot 3D plots

Fig (15) – Plotting of 3D command

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.