Вы находитесь на странице: 1из 54

# Plotting In two Dimension Contd

## Recall that sources of data can either be

from generating functions (known as
function data) or from measurement of
experimental data (known as measured
data)
(1) Function data: fd is data created by
mathematical operations.
Characteristics of fd: (i) data uniformity.
Data is not sparse or riddled with
discontinuities.
(ii) can be controlled, vary parameters,
and recreate data in any form.

## Disadvantage: Such ideal data is not a

true representative of real situations.
Example of fd is shown below:
Y(t) = sin(20t) + sin(60t)
(2) Measured Data: m.d results from real
world experiments. Xtics: (i) Measured
data is only as accurate as the device
making the measurement. (ii) There is
always some degree of uncertainty
associated with the data. (iii) Data may
take extreme excursions (iv) m.d may
be incomplete or have gaps.

## Importing Data: Data, whether measured

or function generated data created using
another application can have all their
contents imported into MATLAB
environment.
The most straightforward method of
importing data is to use the load
either binary files containing matrices
generated by earlier MATLAB sessions
using the save command. The save
command will save the specified data in
MATLABs binary data format.

## Simply issuing the load command

with the filename is all that is
needed. e.g
>>save mydata x y
In using save and load in this way,
MATLAB will by default attach
the .mat extension to the data file
and the file will be created or read
from the current working directly.

## Elementary 2-D plots

The most useful graph that can be
created is a simple line plot of
numeric data.
The basic MATLAB graphing
procedure, in 2D plots is to take a
vector of x- coordinates, x = (x1; : : : ;
xN), and a vector of y-coordinates, y =
(y1; : : : ; yN), where x and y are
prepared in an identical array form; i.e
x and y must either be row arrays or
column arrays of the same length.

## The MATLAB command to plot a

graph is plot(x,y). e.g given the
vectors 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) The graph is as
shown below.

-1

1.5

2.5

3.5

4.5

5.5

## The plot functions has different

forms depending on the input
arguments. If y is a vector then
plot(y)produces a piecewise linear
graph of the elements of y versus
the index of the elements of y.
Example:
>>y=(-5:0.1:4).^3;
>>plot(y);
Here, y is plotted against the index
of all the elements of y.

100

50

-50

-100

-150

10

20

30

40

50

60

70

80

90

100

## Notice that the x-axis labels are not

the numbers that were cubed. They
are the subscripts or index numbers of
the vector y. By default MATLAB
the index number when only one
argument is passed to the plot
function.
You can type
length(y)
ans =
91 meaning that there are 91
elements

## At the last point (91) on the x-axis it

is 64 on the y-axis or y(91) = 64.
We can have the indices of the
display the value of the input that
was cubed or responsible for the
output.
>>x = -5:0.1:4;
>>y = (x).^3;
>>Plot(x,y);

100

50

-50

-100

-150
-5

-4

-3

-2

-1

## Note: When you do not insert a

semi colon after the function,
the MATLAB will both list all
vectors y and draw the graph.
annotations.
axis labels and titles, e.g let us
add an x- and y-axis labels.

>> xlabel('x');
>> ylabel('y');
>> title('Graph of y=x^3');
>> grid; The graph is as
shown.

Graph of y=x 3

100

50

-50

-100

-150
-5

-4

-3

-2

-1

## The plot command arguments are

not restricted to being vectors.
The input may also be matrices.
Example: Plot the function sin (x)
on the interval [0; 2].
1st create a vector of x values
ranging from 0 to 2, then
compute the sine of these values,
and finally plot the
result:

>> x=(0:2*pi);
>> y=sin(x);
>> plot(x,y);
Also
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
See the graph as displayed.
If you omit the increment, MATLAB
automatically increments by 1.

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

## Let us again add labels and

title.
>>xlabel('x');
>> ylabel('y');
>> xlabel('2*pi');
>> title('Graph of Sine x');
>> xlabel('2\pi');
>>

Graph of Sine x

1
0.8
0.6
0.4

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

## Multiple data sets in one plot

Multiple (x; y) pairs arguments
create multiple graphs with a
single call to plot.
For example, plot the related
functions of x: y1 = 2cos(x), y2
= cos(x), and y3 =
0.5cos(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,'.');
The graph is as shown below:

2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

## Lets add title and label the

axes:
>> xlabel('0\leq x\leq 2\pi');
>> ylabel('cosine functions');
>> grid
>>legend('2*cos(x)','cos(x)','0.
5*cos(x)');
The graph is shown

2
2*cos(x)
cos(x)
0.5*cos(x)

1.5

cosine functions

1
0.5
0
-0.5
-1
-1.5
-2

3
4
0 x 2

## By default, MATLAB uses line style

and color to distinguish the data sets
plotted in the graph. However, you
can change the appearance of these
annotations to the graph to help
Specifying line styles and colors
It is possible to specify line styles,
colors, and markers (e.g., circles, plus
signs, . . . ) using the plot command:

plot(x,y,'style_color_marker')
where style_color_marker is a triplet of values from
Table below: obtained by typing >>help plot
colour markers line style
b
blue
.
point

g
r
c
m
y
k

green
red
cyan
magenta
yellow
black

o
circle
x
x-mark
+
plus
*
star
s
square
d
diamond

solid

:
dotted
-. dashdot
-- dashed
(none) no line

## calculate the function x(t) = etsin(2t) if t varies from 0 to 8

sec, and then plot the resulting
function, with incremental step
of 0.1

>>t=(0:0.1:8);
>> x=(exp(-t).*sin(2*t));
>> plot(t,x,'o')
>> plot(t,x,'-')
>> xlabel('t')
>> ylabel('x')
title('graph of x=exp(t)sin(2t)');

graph of x=exp(-t)sin(2t)

0.6
0.5
0.4

0.3
0.2
0.1
0
-0.1
-0.2

4
t

## To further illustrate the plot

function and options using the
MATLAB statements, consider
>> t=0:0.25:10; y=sin(t);
plot('t,x,'r+-')
You can indeed type all your
arguments on one line.
Note the combination of r+- in the
plot command where r=red colour,
+=marker. And is the line style.

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

## You can show the array of

either t or y:
The code
>> t=0:0.25:10;
>> y=sin(t), plot(t,y,'ys-.');
Will display the array of the
calculated y before plotting.

Columns 1 through 9

0.2474

0.4794

0.6816

0.8415

0.9490

0.9975

0.9840

0.9093

Columns 10 through 18

0.7781
-0.8950

0.5985

0.3817

## 0.1411 -0.1082 -0.3508 -0.5716 -0.7568

Columns 19 through 27

## -0.9775 -0.9993 -0.9589 -0.8589 -0.7055 -0.5083 -0.2794 -0.0332

0.2151

Columns 28 through 36

0.4500
0.6247

0.6570

0.8231

0.9380

0.9946

Columns 37 through 41

0.4121

0.9894

0.9226

0.7985

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

## You can change the colour, marker

and line style.
>> plot(t,y,'rd-');
>> plot(t,y,'rd-')
Remember you can type all of your
argument in a line.
>> t=0:0.25:10, y=sin(t);
plot(t,y,'g*-.')
Notice the absence of the semicolon
(;) from the 1st argument. Values of t
will be displayed before plotting.

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

## Practice: Calculate and plot two

functions x = sint and x =
sin(0.5t) for 0 t 12. Use
blue and green colours,
diamond and star for markers,
smooth and dashed lines for
line style. (ii) label the axes,
title of graph and legend

SOLUTION:
>> t=0:0.25:12; x1=sin(t);
x2=sin(0.5*t);
plot(t,x1,t,x1,'bd-',t,x2,t,x2,'g*-'
) all in one line
The plot command may also be
written as: >> t=0:0.25:12;
x1=sin(t); x2=sin(0.5*t);
plot(t,x1,'yd-',t,x2,'r*-')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

>> xlabel('time,s')
>> ylabel('y')
>> title('two sine functions')
>> legend('x1', 'x2') or
>> legend('sin(t)', 'sin(0.5*t)')

## The graph is as shown.

two sine functions

sin(t)
sin(0.5*t)

0.8
0.6
0.4

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

5
time,s

10

2 sin t 0.05t
x(t )
e
,0 t 30
1
2 cos t
4

## Take 5% as the increment for t

between 0 and 30.
PP: Cal. and plot the functions:
x
x
f1 ( x)
, f 2 ( x)
, if 5 x 5
4
4
1 x
1 sin x x

>> x=exp(-0.05*t).*(2.+sin(t))./
(2.-cos(0.25*t));
>> plot(t,x,'d-')
>> xlabel('time,sec')
>> ylabel('function x')
>> title('graph of x(t)')

graph of x(t)

2.5

function x

1.5

0.5

10

15
time,sec

20

25

30

## 1. A liquid flows through a pipe

with a velocity equal to 0.9 m/s.
If the diameter of the pipe is 0.3
m and the density of the liquid
is 400 kg/m3. Calculate the
full:
Area
Volumetric flow rate
Mass flow

>> vel=0.9;
>> dia=0.3;
>> den=400;
>> Area=pi*(dia^2)/(4)
Area =
0.0707
>> volflorate=Area*vel
volflorate =
0.0636
>> massflorate=den*volflorate
massflorate =
25.4469

Plot k as a function of
temperature from 373 to 600 k
(use 50 points of Temperature).
If you know: K=A exp(B-(C/T)ln(D*T)-E*T+F*T2)
Where, A=3.33, B=13.148,
C=5639.5
D=1.077, E=5.44*10-4,
F=1.125*10-7

## 3. The heat capacity of ethylene is

given by the equation:
Cp = 3.95 + 0.156T 8.3 10 5
T 2 + 17.6 10 9 T 3
Write a program to evaluate the
heat capacity T = 40 C. Then
calculate the ethylene enthalpy (h)
when T1 = 80.
Given that,
H = Cp (T1-Tref)
Tref = 0 C

## 4. Write a code for the

distillation column shown below
to find the value for stream B
and the compositions of stream
D.

## The Secant Method

While Newton's method is fast, it
has a big downside: you need to
know the derivative of f in order
to use it. In many "real-life"
applications, this can be a showstopper as the functional form of
the derivative is not known. A
natural way to resolve this would
be to estimate the derivative using
f(x)f(x+)f(x)for 1

## The secant method uses the

previous iteration to do something
similar. It approximates the
derivative using the previous
approximation. As a result it
converges a little slower (than
Newton's method) to the solution.
xn+1=xnf(xn)xnxn1f(xn)
f(xn1)