che692-lab2

48
PROCESS MODELING AND SIMULATION MATLAB – Essential For Engineers In the Process Industries

Upload: mxr-3

Post on 18-Jul-2016

214 views

Category:

Documents


2 download

DESCRIPTION

lol

TRANSCRIPT

PROCESS MODELING AND SIMULATION

MATLAB – Essential For Engineers In the Process

Industries

Plotting in Matlab

• Basic 2D plotting– Define the function– Specify the range of values to plot– Syntax to plot in Matlab: plot(x,y)

Example1:

• Plot cos(x) for 0 ≤ x ≤10, interval 0.1• >> x = [0:0.1:10];• >> y = cos(x);• >>plot(x,y),xlabel(‘x’),ylabel(‘y’)

Or by using drop down tools in Matlab

Example 2:

• Plot cos(x)sin(x) for 0 ≤ x ≤10, interval 0.1

• >> x = [0:0.1:10];• >> y = cos(x).*sin(x);• >>plot(x,y), grid on

Axis command

• To have equal spacing on both axes:• >>plot (x,y),grid on,axis equal

Showing multiple functions on one plot

• >> t = [0:0.01:5];• >> f = exp(-t);• >> g = exp(-2*t);• >>plot(t,f,t,g,’--’)• ‘--’ dashed line, ‘-’ solid line, ‘-.’ dash dot, ‘.’

dotted• >>plot(t,f,’r’,t,g,’b--’) • r = red solid line, b=dashed blue line

Line styles and marker

• Colour strings are ‘c’, ‘m’, ‘y’, ‘r’, ‘g’, ‘b’, ‘w’, and ‘k’ correspond to cyan, magenta, yellow, red, green, blue, white and black.

• Line strings are ‘-’ for solid, ‘--’ for dashed, ‘:’ for dotted, ‘-.’ for dash-dot, and ‘none’ for no line.

• The marker types are ‘+’, ‘o’, ‘*’, and ‘x’ and the filled marker types ‘s’ for square, ‘d’ for diamond, ‘^’ for triangle, ‘v’ for down triangle, ‘>’ for right triangle, ‘<‘ for left triangle, ‘p’ for pentagram, ‘h’ for hexagram, and none for no marker

• >> plot(x,y,'r^')

Exercise 1

• Plot the data given:

Trial Distance, m1 58.52 63.83 64.24 67.35 71.56 88.37 90.18 90.69 89.5

10 90.4

Exercise 2

An experiment to find the effect of flowrate to the output response of the system’s level was carried out by a student. The data obtained are as in the next slide.

Plot the data given and complete the figure by labeling the legend, x-axis label, y-axis label, title & grid.

Flow rate in (m3/s)

Output response (Level, m)

1 203 255 327 409 4511 5213 6015 7017 9019 10021 110

Exercise 1

Subplots

• Enable for the graph window to be splited into subwindows.

• >> x=[0:0.5:50]';• >> y=[5*x.^2];• >> plot(x,y)

• >> semilogx(x,y)

Generating random numbers

• Rand function – generates random numbers uniformly distributed over the interval [0,1].

– rand (n) – generates a n x n matrix – rand (m,n) – generates m x n matrix

• Seed value – used to initiate a random sequence of values– rand (‘seed’,n) – sets the value of the seed to

n– rand (‘seed’) – returns the current value of the

random number generator seed

Example

• For an array m x n of uniformly distributed random numbers between a and b– >>a+(b-a)*rand(m,n)

• randn(m,n) – returns an array m x n of normally distributed random numbers with mean 0 and standard deviation 1 (Standard Normal Distribution)– >>a+(b-a)*randn(m,n) – mean ‘a’, standard

deviation ‘b’.

• >>floor(10*rand(10,1)• floor(x) – function that gives the largest

smaller integer to x• ceil(x) – function that gives the smallest

larger integer to x

Exercise 2

Polynomial function

• >> x=[1:0.5/200:5];• >> f=3*x.^4-0.5*x.^3+x-5.2;• >> plot(x,f)

Polyval function

• Evaluates a polynomial with coefficients a for the values of x

• >> x=[1:0.5/200:5];• >> a=[3,-0.5,0,1,-5.2];• >> f=polyval(a,x);• >> plot(x,f)• Gives the same plot as previous plot

Polynomial operations

• Functions addition:– g(x) = x4-3x2-x+2.4– h(x) = 4x3-2x2+5x-16– s(x)= g(x) + h(x)

• >> x=[1:0.5/200:5];• >> g=[1,0,-3,-1,2.4];• >> h=[0,4,-2,5,-16];• >> s=g+h

• s =1.0000 4.0000 -5.0000 4.0000 -13.6000

• Function multiplication• >> g=[1,0,-3,-1,2.4];• >> s=3*g• s =3.0000 0 -9.0000 -3.0000 7.2000 s = 3x4 - 9x2 – 3x + 7.2