Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1/15
2/15
3/15
4/15
Figure 2.5:
2.2
t = (0:.1:2*pi)';
subplot(2,2,1)
plot(t,sin(t))
subplot(2,2,2)
plot(t,cos(t))
subplot(2,2,3)
plot(t,exp(t))
subplot(2,2,4)
plot(t,1./(1+t.^2))
If you have hard time seeing some of the plots that you do in
matlab on the color workstations, you should probably change
the colors.
To find out how to do that, you can type at the matlab prompt:
Control Engineering Lab
5/15
help plot
In short, color can be specified inside the plot command.
can type:
You
plot(x,y,'w')
to plot a white line. You can also use other colors -- they
are all listed in the plot help information.
2.3
Here are other ways to graph multiple curves, using matrices (plotted by
columns) and using "hold."
Figure 2.6:
Functions of two variables may be plotted, as well, but some "setup" is required!
>> [x y] = meshgrid(-3:.1:3, -3:.1:3);
>> z = x.^2 - y.^2;
Here are two options for plotting the surface. Look at the help page for details.
Control Engineering Lab
6/15
Figure 2.7:
2.4
3D Plots:
41x21
41x21
21x1
41x1
6888
6888
168
328
Class
double
double
double
double
array
array
array
array
7/15
>> mesh(x,y,z)
>> surf(x,y,z)
8/15
2.5
plots x vs log(y)
loglog(x,y)
grid
title('text')
9/15
Polar plots:
angle = 0:.1*pi:3*pi;
radius = exp(angle/20);
polar(angle,radius),...
title('An Example Polar Plot'),...
grid
Bar graph:
bar(x,y) and bar(y)
stair (x,y) and stair(y)
Multiple plots:
x1=0:.05*pi:pi;
y1=sin(x1);
plot(x1,y1)
hold
y2=cos(x1);
plot(x1,y2)
You can create multiple graphs by using multiple arguments.
In addition to the vectors x,y created earlier, create the
vectors a,b and plot both vector sets simultaneously as
follows.
a = 1 : .1 : 3;
b = 10*exp(-a);
plot(x,y,a,b)
Multiple plots can be accomplished also by using matrices
rather than simple vectors in the argument. If the arguments
of the 'plot' command are matrices, the COLUMNS of y are
plotted on the ordinate against the COLUMNS of x on the
abscissa.
10/15
2.6
EQUATION FITTING
INTERPOLATING POLYNOMIAL
= a0 + a1x1
= a0 + a1x2
= a0 + a1x3
.
.
.
.
.
.
.
.
.
yn+1 = a0 + a1xn+1
+
+
+
a2x12
a2x22
a2x32
.
.
.
+
+
+
a2xn+12
11/15
x1
x12.....x1n+1
x2
x22.....x2n+1
[X] =
y3
.
.
.
yn+1
a1
a2
[a] =
1
.
.
.
1
x3
.
.
.
xn+1
x32.....x3n+1
.
.
.
.
.
.
xn+12... xn+1n+1
a3
.
.
.
an+1
Note that the right side of the matrix equation must be the
product of a 'n+1' x 'n+1' square matrix times a 'n+1' x 1
column matrix! The solution using Gauss elimination calls for
left division in 'matlab' or
[a] = [X]\[y]
Using this method, we can find the coefficients for a ndegree polynomial that passes exactly through 'n+1' data
points.
If we have a large data set, each data pair including
some experimental error, the n-degree polynomial is NOT A GOOD
CHOICE. Polynomials of degree larger than five or six often
have terribly unrealistic behavior BETWEEN the data points
even though the polynomial curve passes through every data
point ! As an example, consider these data.
x
2
3
4
4
3
5
12/15
5
6
7
8
9
10
4
7
5
7
10
9
ones()
y
so
[X][a] = [y]
Solve for the coefficients, matrix
entering the command
a in the aboveequation, by
a = X\y
which results in
13/15
a = [ 1.0e+003*
3.8140
-6.6204
4.7831
-1.8859
.4457
- .0649
.0057
- .0003
.0000 ]
Note that the ninth coefficient a(8) appears to be zero.
Actually, it is finite, it only appears to be zero because
'matlab' is printing only 4 significant figures to the left of
the decimal point. To observe the coefficients with more
significant figures, enter the commands
format long
a
and the format is changed to one with 15 significant digits.
Clearly a(8) is not zero, it is a small number because it is
multiplying a number, x, raised to the eight power.
Now that we have the coefficients, let's generate a sufficient
number of points to create a smooth curve. For x, form a
vector over the range 2 <= x <= 10 in increments of 0.1.
x = [ 2:.1:10 ];
For y, calculate the value of the eighth degree polynomial for
each x.
y =a(1)+ a(2).*x + a(3).*x.^2 + a(4).*x.^3 +
a(5).*x.^4...
+ a(6).*x.^5 + a(7).*x.^6 + a(8).*x.^7 + a(9).*x.^8;
Now plot (x,y) and the data points (x9,y9).
plot(x,y,x9,y9,'o')
The polynomial results appear to pass exactly through every
data point, but clearly the polynomial is useless for
representing our impression of the data at any other point
within the range of x!
This is a common behavior for highorder interpolating polynomials. For this reason, one should
never attempt to use high order interpolating polynomials to
represent experimental data.
Control Engineering Lab
14/15
Lab Task
Plot all trigonometry, exponential and any polynomial function
using subplot, mesh, surf, plot
15/15