A
Module 2
D
AD
U
VT
Creating and Plotting simple plots
• The MATLAB commands used are
• plot creates a 2-D line plot,
A
D
• axis changes the aspect ratio of the x-axis and the y-axis,
AD
• xlabel annotates the x-axis,
U
• ylabel annotates the y-axis,
VT
• title puts a title on the plot, and
• print prints a hard copy of the plot
Plot y = sin x, 0<x<2pi, taking 100 linearly spaced points in the given
interval. Label the axes and put "Plot created by your name" in the title.
A
D
AD
• x=linspace ( 0 , 2*pi , 100) ;
• plot (x , sin(x) )
U
VT
• xlabel(‘ time period ‘)
• ylabel ( ‘ amplitude‘)
• title ( ' Plot created by your name’ )
• Line styles: Make the same plot as in Exercise 1, but rather than
displaying the graph as a curve show the unconnected data points. To
A
display the data points with small circles, use plot (x , y, 'o').
D
AD
• plot (x , sin (x) ,x, sin(x) , ' o’)
• xlabel('x ' )
U
• ylabel ( ' s in (x) ’)
VT
• Space curve: Use the command plot3 (x , y, z) to plot the circular helix
x(t) = sin t, y(t) = cos t, z (t) = t, 0 < t >20
A
D
AD
• t=linspace ( 0 , 20 , 100) ;
• plot3 (sin (t ) , co s (t) ,t)
U
VT
Steps to create function file
[Link] 1: Create a file and write commands using the function
keyword.
A
[Link] 2: Save the function file with the same name as the
D
function. Here I have saved the file with the name solve.m.
AD
[Link] 3: Use the function in Command Video by providing
U
suitable argument/s.
VT
% function to calculate sin(x)+cos(x) where x is in radians
% Here solve is the name of the function which
% will return x and takes an argument a
A
function x = solve(a)
D
AD
x = sin(a) + cos(a);
U
VT
Working with arrays and matrices
• A 2-D array is a list of numbers arranged in rows and columns.
• If you form an array by writing numbers in rows, all rows must have
A
the same number of entries.
D
• Same is true for columns. An array with m rows and n columns is
AD
called an m x n array and it has a total of m · n entries.
U
• An element of the array is recognized by its location-its row number
VT
and column number.
• These row and column identifiers are called indices of the matrix.
Thus A( i, j) refers to a specific element of matrix A located in the ith
row and jth column.
• >>A= [l 2 3; 4 5 6; 7 8 8]
• A=
A
1 2 3
D
4 5 6
7 8 8
AD
• >> A( 2,3)
U
Ans =
6
VT
>> B = A ( 2:3,1 :3)
B=
4 5 6
7 8 8
• >> B= A(2:3, : )
A
• B=
D
4 5 6
AD
7 8 9
U
VT
• >> B(:,2)= []
• B=
4 6
7 9