Experiment 1 - Fundamental Concepts
Experiment 1 - Fundamental Concepts
Objective
Introduction of MATLAB
Understand how programs are written in MATLAB
Understand the MATLAB windows, working with basic commands, Elementary built in functions, Variable
declaration and Rules for variables
Time Required : 3 hrs
Programming Language : MATLAB
Software Required : MATLAB
Hardware Required : NIL
Introduction
MATLAB is stand for MATrix LABoratory. It is a mathematical package based on matrices and consists of
extensive library of numerical routines.
All Data stored in the form of a matrix
Case-sensitive
The basic data type is matrix (normally with 16 decimals precision)
Possible to handle complex numbers
High-Level programming language and Matrix based computing environment
Visualization of data as advanced graphics two and three dimensional
How to run MATLAB
From Start Menu -> Select Programs ->Select MATLAB 7.1
MATLAB Prompt
Tells that MATLAB
is ready for your
command
>> 39*4.4+5
ans =
176.6000 The MATLAB command
Command window
The result.
>> LHS=cos(x/2)^2
LHS = 0.9045
>> RHS=(tan(x)+sin(x))/(2*tan(x))
RHS = 0.9045
Creating Matrix/Arrays
The Matrix/Array is a fundamental form that MATLAB uses to store and manipulate data. It is a list of numbers
arranged in rows and/or columns. Simplest way to create matrix is to use the constructor operator [].
One Dimensional
It is a list of numbers that is placed in a row or a column. Variable_name=[ type vector elements]
Row Vector: To create a row vector type the element with a space or a comma between the elements inside
square brackets.
row = [E1, E2, … Em] or row = [E1 E2 … Em]
>>A=[12, 62, 93, -8, 22]
Column Vector: To create a column vector type the element with a semicolon between the element or press the
enter key after each element inside square brackets.
col = [E1; E2; … Em] or row = [E1
E2
… Em]
>>B=[12; 62; 93; -8; 22]
Example Date
Year 2007 2008 2009 2010
Population 136 158 178 211
(Millions
Practice 1
>> yr=[2007 2008 2009 2010] %Year is assigned to a row vector named yr.
Matrix
yr creation
= 2007 using
2008: Operator
2009 (colon)
2010
Colon operator is used to create a vector with constant spacing the difference between the elements is the same.
>> pop=[136; 158; 178; 211] %Population data is assigned to column vector named
Variable_name=[ start : step : stop] or start : step : stop
pop.
Wherepop
start
= is first term, stop is last term and step is spacing between elements. The default value of step is 1.
136 Practice 2
158 >> yr=[2007 : 2010] %Year is assigned to a row vector named
178 yr.
yr = 2007 2008 2009 2010
3 Numerical
>> x=[1: 2 :Methods
13] – Fundamental Concepts%First element is 1, spacing 2 and last 13
x = 1 3 5 7 9 11 13
>> y=[1.5: 0.1 : 2.1]
y = 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000
>> x = -pi/2:2*pi/60:pi/2;
Note: If stop value can not be obtained by add step’s to stop then the last element is the vector with be last
number that does not exceed stop.
Two Dimensional
A two dimensional array/matrix has numbers in rows and columns. Matrix can be used to store information like
in a table.
Variable_name=[ 1st row elements; 2nd row elements; ……. ;last row elements ]
To create a matrix type the element with a semicolon between the rows or press the enter key after each row
inside square brackets.
row = [RE1; RE2; … REm] or row = [RE1
RE2
… REm]
Practice 4
>> a=[5 35 43; 4 76 81; 21 32 40] %A semicolon is typed before a new line
Practice 9
>> ve=[4 15 8 12 34 2 50 23 11]; %A vector ve is created
Practice 18
>> A=[2 6 3; 5 8 4];
Graphics
MATLAB produce two and three dimensional plots of curves and surface
Two Dimensional Plots
Plots are very useful tool for presenting information.
Plot title, Legend, X axis label, Y axis label, Text label and Markers.
Graph is shown in figure window.
Plot Command
The plot command is used to create two dim plots.
The simplest from of the command is plot(x,y), Where x (horizental axis) and y (vertical axis) are vector.
Both vectors must have the same number of elements.
The curve is constructed of straight line segments that connect the points.
The figure that is created has axes with linear scale and default range.
Line Specifiers:
Line specifiers are optional and can be used to define the style and coloar of the line and the type of
markers.
Within the string the specifiers can be typed in any order.
Specifier is optional i.e. None, one, two, or all the three can be included in command.
Plot of function
Plot of given functgion can be done in MATLAB by using the plot or the fplot commands.
Using plot: Plot a function y=f(x) with plot command, user first needs to create a vector of values of x for the
domain that function will be plotted.
Then a vector y is created with the corresponding values of f(x) by using element by element calculation
Plot the x and y vectors by using plot command.
Using fplot: Plot a function y=f(x) with fplot command between specified limits.
function can be typed directly as a string and The function can not include previously defined variables.
limits is a vector with two element that specify the domain of x [xmin,xmax] or [xmin ,xmax,ymin,ymax].
n is the number of points between interval.
Line specifier are the same as in plot command.
>>fplot(‘cos’,[-2,4])
6 Numerical Methods – Fundamental Concepts
>>hold on
>>fplot(‘sin’,[-2,4])
M-Files
Files that contain a MATLAB code are called the m-files. There are two kinds of m-files:
1. script files
Script files is a sequence of MATLAB statements and do not take the input arguments or return the
output arguments
2. function files
The function files may take input arguments or return output arguments. Function name should be same
as the name of the file without .m extension
To make the m-file click on File next select New and click on M-File
To open the m-file from within the Command Window type >>edit filename
Script Files
A Script file is a sequence of MATLAB command also call a program.
When a script file runs, MATLAB executes the commands in the order they are written.
When a script file displayed the output in command window.
It is convenient way to edit and execute command many times.
Script file can be typed and edited in any text editor and saved as .m file.
Script files is also called M-files because the extension .m is used.
The input command
The input command is used to prmpt user to assign a value to variable in command window.
variable_name=input(‘string with a message that displayed in command windows’)
The string is a message prompting the user to enter a value that is assigned to variable.
Practice 2
%Create a script file with name TempConv.m
disp('Converting table Cel 2 Fah');
start=input('Start temp in Celsius: ');
incr=input('In-crement in Celsius: ');
total=input('No of Entries in table: ');
stop=start+(total-1)*incr;
C=start:incr:stop;
F=9/5*C+32;
disp('Celsius Fahrenheit');
fprintf('%10.2f %10.2f \n', [C;F]);
>>TempConv
Function files
A user defined function is a MATLAB program that is created by the user saved as function file, and then
can be used like a built in function.
It must be put in a .m-file with same name as function
The input and output can be one or several variables, and each can be a scalar, vector or an array of any size
A large program can be made up of smaller “building blocks” that can be tested independently.
Structure of a function file
Practice 3
function [mpay, tpay]=loan(amount,rate,years) %function definition line
%loan calculates monthly and total payment of loan. %The H1 line
%Input arguments: % Help Text.
%amount=loan amount in $ “
%rate=annual interest rate in percent. “
%years=number of years.
%Output arguments:
%mpay=monthly payment, tpay=total payment.
format bank %function body
ratem=rate*0.01/12;
a=1+ratem;
b=(a^(years*12)-1)/ratem;
mpay=amount*a^(years*12)/(a*b); %Assignment of values to output
arg
tpay=mpay*years*12;
Practice 4 FtoC.m
Function C = FtoC(F)
%FtoC converts degrees F to degrees C
C=5*(F-32)./9;
>>FC=inline(‘5*(F-32)./9’);
>>z=FC(70);
%f(x,y)=2x2-4xy+y2
>>HA=inline(‘2*x^2-4*x*y+y^2’);
>>z=HA(2,3)
z=-7
Practice 5
>>feval(‘sqrt’,64)
ans=8
>>feval(‘FtoC’,70);
>>[M,T]=feval(‘loan’,50000,3.9,10)
M=502.22
T=60266.47
Summary:
In this lab practical students understand the programming concepts of MATLAB. These concepts are essential
for every student for implementation and analysis of Numerical Method.
Web Resources
http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
Videos Resources
http://www.mathworks.com/videos/writing-a-MATLAB-program-69023.html