0% found this document useful (0 votes)
141 views

Basic Plotting:: X (1 2 3 4 5 6) y (3 - 1 2 4 5 1) Plot (X, Y)

This document provides an overview of basic plotting in MATLAB. It demonstrates how to plot single and multiple data sets, add titles and labels, and shows an example of plotting multiple functions on the same graph. It also discusses MATLAB programming basics like creating M-files, the difference between script and function files, getting input from the user, and includes an exercise with examples of MATLAB code.

Uploaded by

abdul wakeel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Basic Plotting:: X (1 2 3 4 5 6) y (3 - 1 2 4 5 1) Plot (X, Y)

This document provides an overview of basic plotting in MATLAB. It demonstrates how to plot single and multiple data sets, add titles and labels, and shows an example of plotting multiple functions on the same graph. It also discusses MATLAB programming basics like creating M-files, the difference between script and function files, getting input from the user, and includes an exercise with examples of MATLAB code.

Uploaded by

abdul wakeel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Plotting:

MATLAB has an excellent set of graphics tools. Plotting a given data set or results
of computation is possible with a very few commands. Trying to understand
mathematical functions with graphics is an enjoyable and very ancient way of
learning mathematics. Being able to plot mathematical functions and data freely is
the most important step, and this section is written to assist you to do just that.
The MATLAB command used to plot the graph is “plot(x,y)”. For example, if the
vector x= (1; 2; 3; 4; 5; 6) and y=
(3; -1; 2; 4; 5; 1)
>> x= [1 2 3 4 5 6];
>> y= [3 -1 2 4 5 1];
>>plot(x,y)
Now try the following:

>>x = 0: pi/100: 2*pi;


>>y= sin (x);
>>plot (x,y)

Adding titles and axis labels:

MATLAB enables you to add axis labels and titles. For example, using the graph
from the previous
example, add x- and y-axis labels.
>> xlabel (‘x = 0:2\pi’);
>>ylabel (‘sine of x’);
>>title (‘Plot of the Sine Function’)
Multiple data sets in one plot:

Multiple (x;y) pairs arguments create multiple graphs with a single call to plot. For
example, these statements plot
three plots related functions of x: y1=2 cos(x), y2=cos(x), and
y3=0.5*cos (x) in the interval 0 ≤ x ≤ 2Π
>> x= 0:pi/100:2*pi;
>>y1= 2*cos(x);
>>y2= cos (x);
>>y3= 0.5*cos (x);
>>plot (x, y1, ‘- -‘,x, y2, ‘- - ‘, x, y3, ‘:’)
>>xlabel (‘0 \leq x \leq 2\pi’)
>>ylabel (‘Cosine functions’)
>>legend (‘2*cos(x)’, ‘cos(x)’, ‘0.5*cos(x)’)
>>axis ([0 2*pi -3 3])

The result of the multiple data in one graph is shown below:


MATLAB Programming:
To make a file in MATLAB, click on “New” in the file menu in menu bar. It will
open a new file. After writing the code, if we want to save this file (called m-file),
it will be saved in “bin” folder of MATLAB by default. Such files are called “M-files”
because they have an extension of “.m” in its filename.
There are two types of M-files: Script Files and Function Files
% This is a sample m-file
a = [1,2,3;0,1,1;1,2,3]
b =a’;
c = a+b
d = inv(c)
Save this file as “rkg.m”.
Then in order to execute this file, type
>> rkg <ENTER>
The % symbol indicates that the rest of the line is a comment. MATLAB will ignore
the rest of the line. However, the first comment lines which document the m-file
are available to the online help facility and will be displayed. An M-file can also
reference other M-files, including referencing itself recursively.
Function Files:
Function files provide extensibility to MATLAB. You can create new functions
specific to your problem which will then have the same status as other MATLAB
functions. Variables in a function file are by default local. However, you can
declare a variable to be global if you wish.
Example:
Function y = prod (a,b)
y = a*b;
Save this file with filename prod.m then type on the MATLAB prompt
>> prod (3,4)
ans =
12
In MATLAB to get input from user the syntax rule is:
>>variable name = input (‘variable’);
Example:
>>a=input (‘ Enter a number ‘);
And on command prompt if we run this program it will be displayed like:
>> Enter a number
If we type 2 at the cursor then we get the output as
a=2
EXERCISE:

1. Given that x=-5+9i and y=6-2i, use MATLAB to show that x-y=1+7i, xy= -12+64i
and x/y= -1.2 + 1.1i
2. Use MATLAB to compute: 6(351.4 )+140.35
3. Use MATLAB to plot the functions y=4√(6x+1) and z=5e0.3x – 2x over the
interval 0<x<1.5
4. Use MATLAB to plot function s= 2 sin (3t+2) + √(5t+1) over the interval 0<t<5.
5. Plot exp(-x) sin(8x) for 0≤ x ≤2Π
6. Plot the function y= (4/Π) [(sin x) + (sin 3x)/3 + (sin 5x)/5 + (sin7x)/7] over the
interval –Π<x<Π. These are the first four terms of the Fourier Series
representation of a square wave.
7. Define a 4x3 matrix with zeros everywhere except the first line that is filled
with 1. Hint= use ones and zeros command
8. Use MATLAB to solve following set of equations.
6x - 4y + 8z = 112
-5x – 3y +7z = 75
14x + 9y – 5z = -67
9. Use MATLAB to find roots of 13x3 + 182x2 – 184x +2503 = 0

You might also like