PRACTICAL FILE OF Matlab
PRACTICAL FILE OF Matlab
MATLAB
INDEX
SR.NO PROGRAMS SIGNATURE
.
1. Introductionto MATLAB.
2. MATLAB Functions.
3. Writing your firstMATLAB program.
4. Write a program to perform simple calculation in
command window.
5. Write a program to check greater number in command
window.
6. Write a program to calculate simple interest in
command window.
7. Write a program to perform matrix addition in
command window.
8. Write a program to add two numbers in editor
window.
9. Write a program to find factorial of number in editor
window.
10. Write a program to display first 10 natural numbers in
editor window.
11. Write a program tofind addition of two numbers in
editor window.
12. Write a program to plot a line.
13. Write a program to create a multidimensional graph.
1. Introduction to MATLAB
MATLAB or Matrix Laboratory is a high-level programming language consisting of an
visualization. It has been developed by MathWorks. The basic functions of MATLAB are
plotting of functions and data, the creation of user interfaces, matrix manipulations. It also
provides support for interfacing with other programming languages in C, C++, Fortran, and
Java. Besides, it is also used to analyse data, create models and applications, and also
develop algorithms. Along with all this, introduction to MATLAB also provides numerous
numerical methods, generating plots, and a lot of other functions. MATLAB also has a very
good scope in the automotive domain using Rapid control Prototyping or RCP used
the components of MATLAB. MATLAB provides a lot of functionalities that can help in
computational mathematics. Below are the most common functions and mathematical
3. Linear Algebra
4. Algebraic Equations
5. Non-linear Functions
6. Statistics
7. Data Analysis
9. Numerical Calculations
10. Integration
11. Transforms
Characteristics
MATLAB is a versatile tool designed for computational mathematics and supports lots of
other operations. Below given are a few of the characteristics that make Matlab an
intelligent tool –
1. MATLAB contents are a huge library of built-in functions providing support for
numerical computation.
3. MATLAB provides support for creating custom plots and data visualization with its
5. MATLAB provides integration support with other languages like C, Java, Microsoft
Excel, and .NET for integrating MATLAB-based algorithms with external applications.
6. With its uniquely designed programming interface, MATLAB helps its users with
Applications of MATLAB
As we discussed in the introduction to MATLAB, we will now learn about MATLAB’s
applications which are as follows: MATLAB is widely used in the industry as a tool for
engineering, mathematics, Etc. The various applications involving MATLAB are below –
1. Computational Finance
2. Control systems
6. Computational biology
Advantages of MATLAB
Below are the advantages of MATLAB:
like as required in C, C ++. Codes are written in sentences and executed one by one.
4. MATLAB coder is used to converting the code that is written in MATLAB to Java,
Python, C++, .NET, etc., making the MATLAB language more versatile.
5. Different languages can be used to implement scientific theories, and after building
the library files or .dll files, those can be directly implemented in MATLAB using other
languages.
6. The inbuilt rich library of MATLAB contains a library of the neural network, power
etc. Thus, using this reach library, it is always easy to develop and implement any
scientific simulation. These tools can be controlled by using built-in codes so that any
8. MATLAB also supports loops by using the length of code same or even shorter as
9. MATLAB also supports the writing of user-defined functions, which are extremely
easy to implement and also can be stored in separate files for future use. This
potentially saves a lot of time writing the same code which is needed for different
10. MATLAB allows the import and export of data to or from a text or excels file using a
single command which is very useful for analysis of previously recorded data.
11. Data can also be saved in variables with very simple commands, which are easy to
12. MATLAB also gives a feature to use GUI or graphical user interface using add and
drop boxes. On programming the proper functions of the code by using a graphical
user interface, the code is automatically generated by the program. The compilation
of the code is also very easy to implement, where the program is compiled in
13. MATLAB offers a huge set of toolboxes and functions, allowing easy solving of CFD or
14. MATLAB also provides REPL – read, evaluate, print, loop or interactive environment
Disadvantages of MATLAB
Below are the disadvantages of MATLAB:
2. MATLAB is used mainly for scientific research and is not suitable for development
5. MATLAB is more expensive. The license is very costly, and users need to buy each and
6. MATLAB is not known to create application deployment for installation like the task
done by others which includes the setting of files another executable that copies
during installation.
2. MATLAB Functions:
A function is a group of statements that together perform a task. In MATLAB, functions are
defined in separate files. The name of the file and of the function should be the same.
Functions operate on variables within their own workspace, which is also called the local
workspace, separate from the workspace you access at the MATLAB command prompt
which is called the base workspace.
Functions can accept more than one input arguments and may return more than one output
arguments.
Syntax of a function statement is −
function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)
Example
The following function named mymax should be written in a file named mymax.m. It takes
five numbers as argument and returns the maximum of the numbers.
Create a function file, named mymax.m and type the following code in it −
Anonymous Functions:
Matlab functions:
Any function other than an anonymous function must be defined within a file. Each function
file contains a required primary function that appears first and any number of optional sub-
functions that comes after the primary function and used by it.
Primary functions can be called from outside of the file that defines them, either from
command line or from other functions, but sub-functions cannot be called from command
line or other functions, outside the function file.
Sub-functions are visible only to the primary function and other sub-functions within the
function file that defines them.
Example
Let us write a function named quadratic that would calculate the roots of a quadratic
equation. The function would take three inputs, the quadratic co-efficient, the linear co-
efficient and the constant term. It would return the roots.
The function file quadratic.m will contain the primary function quadratic and the sub-
function disc, which calculates the discriminant.
Create a function file quadratic.m and type the following code in it −
function[x1,x2]= quadratic(a,b,c)
quadratic(2,4,-4)
MATLAB will execute the above statement and return the following result −
ans = 0.7321
Nested Functions:
You can define functions within the body of another function. These are called nested
functions. A nested function contains any or all of the components of any other function.
Nested functions are defined within the scope of another function and they share access to
the containing function's workspace.
A nested function follows the following syntax −
function x = A(p1, p2)
...
B(p2)
function y = B(p3)
...
end
...
end
Example
Let us rewrite the function quadratic, from previous example, however, this time the disc
function will be a nested function.
Create a function file quadratic2.m and type the following code in it −
function[x1,x2]= quadratic2(a,b,c)
functiondisc % nested function
d =sqrt(b^2-4*a*c);
end%endoffunction disc
disc;
x1 =(-b + d)/(2*a);
x2 =(-b - d)/(2*a);
end%endoffunction quadratic2
You can call the above function from command prompt as −
quadratic2(2,4,-4)
MATLAB will execute the above statement and return the following result −
ans= 0.73205
Private Functions:
A private function is a primary function that is visible only to a limited group of other
functions. If you do not want to expose the implementation of a function(s), you can create
them as private functions.
Private functions reside in subfolders with the special name private.
They are visible only to functions in the parent folder.
Example
Let us rewrite the quadratic function. This time, however, the disc function calculating the
discriminant, will be a private function.
Create a subfolder named private in working directory. Store the following function
file disc.m in it −
function[x1,x2]= quadratic3(a,b,c)
x1 =(-b + d)/(2*a);
x2 =(-b - d)/(2*a);
end%endof quadratic3
You can call the above function from command prompt as −
quadratic3(2,4,-4)
MATLAB will execute the above statement and return the following result −
ans= 0.73205
Global Variables:
Global variables can be shared by more than one function. For this, you need to declare the
variable as global in all the functions.
If you want to access that variable from the base workspace, then declare the variable at the
command line.
The global declaration must occur before the variable is actually used in a function. It is a
good practice to use capital letters for the names of global variables to distinguish them
from other variables.
Example
Let us create a function file named average.m and type the following code in it −
functionavg= average(nums)
global TOTAL
avg= sum(nums)/TOTAL;
end
Create a script file and type the following code in it −
global TOTAL;
TOTAL =10;
n =[34,45,25,45,33,19,40,34,38,42];
av= average(n)
When you run the file, it will display the following result −
av = 35.500
3. Writing Your First Program.
In this section we will show you how to write a simple program in MATLAB. To speed up
understanding, we will write a program that replicates the plot generated in the “Visual
Display” section.
If you didn’t complete the previous sections, you can download the data
series HERE. Once you have downloaded the MATLAB file, place it in your
active MATLAB “Current Folder”.
Writing a program for everything you do in MATLAB is the most efficient way
of operating within MATLAB. A program allows you to replicate difficult
processes quickly, save your work when it is not complete, fix mistakes before
they become problems, help you remember how you solved a particular
problem and much more.
Start writing a new program by clicking new script at the top left of the
MATLAB window
It is always wise to place some notes at the top of your program describing
the program’s purpose. It is also a good idea to place comments around
confusing parts of your code. There are often times when you will be working
as a team and everyone writes code differently. Do yourself and your team
members a favour and explain what you are doing.
As a house keeping practice, I always write three lines of code at the top of
my program to avoid loitering variables and figures:
“Clear all” clears all variables from the Workspace
“Close all” closes all open figures
“clc” clears the history of commands entered in the Command
Window
Another important habit is to place a semi-colon”;” at the end of every
command in your program. This will suppress printing of the command in the
Command Window. However, occasionally it will be useful to allow the
computer to print a particular command.
Type the commands in the script editor in the order that you would like
MATLAB to execute them:
It is important to notice that if your variables are not saved in a file like
“data.mat”, they will be cleared by the first line of code
Once you have written your code you will need to save it. Click on “Save” near
the upper left of your MATLAB window. Let’s name the program “plotSP”
Once the file is saved you are free to run the program by clicking “Run” or by
typing “plotSP” and enter in the command window
Suppose that after you look at the graph, you decide that the real interesting
part of the series is the middle 2 quarters of the year. You can easily change
your program to only print the middle two quarters of the year by changing
the observation range to 64-189 (252/4=63).
To make this change in your program, change the range
of dateNums and price as shown below:
Suppose further that you now love the data selection on the graph, but you
don’t like the unused space. You can fix this by adding the command “axis
tight”. For more information, see: Axis Scaling and Appearance
Saving Output
You now love your data selection and your graph so much that you want to
save it as a Portable Network Graphic file or”.png”
First you need to save the figure as a variable. Place the command “fig1 =
figure” before any plotting occurs (you could alternatively write fig1 = plot (x,
y)). This action saves all the following figure commands to the variable fig1.
To save the final figure, you will need to add the command “save
as(fig1,’plotSP.png’)” at the end of all the figure commands. This saves the
variable fig1 as a file named “plotSP” of type”.png”.
Once you have done all this, run the program again. You can view the .png file
in your MATLAB folder, which is located in your “Documents” folder on
Windows 7.
Don’t forget to update your description of the program as your program no
longer plots the entire year; only the middle two quarters.
Your final program should look like this:
OUTPUT-
9.Write a program to find factorial of number using for loop in
editor window.
INPUT-
OUTPUT-
10.Write a program to display first 10 natural numbers in editor
window.
INPUT-
OUTPUT-
11.Write a program to find addition of two numbers in editor
window.
INPUT-
OUTPUT-
12.Write a program to plot a line.
INPUT-
OUTPUT-
13.Write a program to create a multidimensional graph.
INPUT-
OUTPUT-