che692-lab2
DESCRIPTION
lolTRANSCRIPT
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’)
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
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.
Subplots
• Enable for the graph window to be splited into subwindows.
• >> x=[0:0.5:50]';• >> y=[5*x.^2];• >> plot(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
• 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
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