Pre-laboratory 7 - Electrical and Computer Engineering

advertisement
University of Waterloo
Faculty of Engineering
Department of Electrical and Computer Engineering
ECE 204A
Pre-Laboratory 7
Prepared by
Surname/Last Name, Legal Given/First Name(s)
UW Student ID Number: 2NNNNNNN
UW User ID: uwuserid @uwaterloo.ca
2A Electrical/Computer Engineering
7 February 2016
7.1a We will use the function f62 from the previous laboratory.
From calculus, you are aware of the chain rule
d
d
d
 u ( x)v( x)    u ( x)  v( x)  u ( x)  v( x)  e
dx
 dx

 dx

and thus we may rewrite this as
d
 d
d

u ( x )  v ( x )    u ( x )v ( x )    u ( x )  v ( x ) .
 dx
 dx
 dx

Integrating both sides yields
d

d

d

a u ( x)  dx v( x)  dx  a  dx  u ( x)v( x)    dx u ( x)  v( x)  dx
b
b
b

a
d
d
 u ( x)v( x)  dx    u ( x)  v( x)dx
dx
dx

a
b
b
d

The fundamental theorem of calculus says that   F ( x)  dx  F ( x) a  F (b)  F (a )
dx

a
and thus we may rewrite this as:
b
b
d

d

a u ( x)  dx v( x)  dx   u( x)v( x) a  a  dx u( x)  v( x)dx
b
b
b
b
By cancelling the dx terms, this is often written as  u dv   uv a   v du .
b
a
a
Thus, you are able to calculate the exact solution to
4
4
 f  x  dx   xe
62
0
x
dx .
0
Using the equation editor in Word (Insert→Text→Object and when the dialog appears,
select the Object Type: Microsoft Equation 3.0 from the list and select OK) and replace
Assign this value to the variable exact in Matlab (your answer should be around 0.9):
>> exact = % your Matlab command and the output
7.1b Let us approximate the integral using the trapezoid rule
b
 f  x  dx  2  f  a   f  b    b  a 
1
a
in Matlab:
a0 = 0.5*(f62(0) + f62(4))*(4 - 0)
Copy and paste your Matlab commands and the output here.
7.1b Let us approximate the integral approximating the integral on the intervals [0, 2] and
[2, 4]:
a1 = 0.5*(f62(0) + f62(2))*(2 - 0) + 0.5*(f62(2) + f6(4))*(4 - 2)
Copy and paste your Matlab commands and the output here.
2
We can do this because
b
b
c
a
c
a
 f  x  dx   f  x  dx   f  x  dx
and thus, an approximation of
the entire integral may be approximated by the sum of two approximations on smaller
intervals.
7.1c Next, we could try this on four and eight intervals:
a2 = 0.5*(f62(0)
0.5*(f62(1)
0.5*(f62(2)
0.5*(f62(3)
+
+
+
+
a3 = 0.5*(f62( 0 )
0.5*(f62(0.5)
0.5*(f62( 1 )
0.5*(f62(1.5)
0.5*(f62( 2 )
0.5*(f62(2.5)
0.5*(f62( 3 )
0.5*(f62(3.5)
f62(1))*(1
f62(2))*(2
f62(3))*(3
f62(4))*(4
+
+
+
+
+
+
+
+
-
0) + ...
1) + ...
2) + ...
3)
f62(0.5))*(0.5
f62( 1 ))*( 1
f62(1.5))*(1.5
f62( 2 ))*( 2
f62(2.5))*(2.5
f62( 3 ))*( 3
f62(3.5))*(3.5
f62( 4 ))*( 4
-
0 )
0.5)
1 )
1.5)
2 )
2.5)
3 )
3.5)
+
+
+
+
+
+
+
...
...
...
...
...
...
...
but this would be tedious. Instead, we note that we are evaluating the function at five and
nine points on the interval [0, 4]:
xs2 = linspace(
fxs2 = f62( xs2
xs3 = linspace(
fxs3 = f62( xs3
0, 4, 5 );
);
0, 4, 9 );
);
Now, we can factor out 0.5 and all the products are the same, so essentially, these
simplify to:
a2 = 0.5*(fxs2(1) + 2*sum(fxs2(2:end-1)) + fxs2(end))*(4 - 0)/4
a3 = 0.5*(fxs3(1) + 2*sum(fxs3(2:end-1)) + fxs3(end))*(4 - 0)/8
where h = 1 and h = 0.5, respectively.
3
We must now implement this as a function:
function intf = comp_trap( f, ab, h )
where ab is a row vector of two values: [a, b]. The first step of the function is to assign
these two entries to the variables a and b, respectively.
ba
ba
1 .
and, therefore, n 
h
n 1
However, we have a problem with numerical calculations: it could be that the result is
not exactly an integer. Thus, it would be wise to use the round function:
The next step is to determine n from h: recall that h 
n = round( (b - a)/h + 1 );
Next, we must determine n linearly spaced points between a and b.
Then, evaluate the function at each of those n points.
Finally, calculate and return
 b  a 
1
 n1

f
(
x
)

2
f ( xk )   f ( xn )  


1
.

2
 k 2

  n 1 
function intf = comp_trap( f, ab, h )
% enter your implemention here
end
These pairs of Matlab commands should produce identical results:
xs2 = linspace( 0, 4, 5 );
f622 = f62( xs2 );
a2 = 0.5*(f622(1) + 2*sum(f622(2:end-1)) + f622(end))*(4 - 0)/4
a2c = comp_trap( @f62, [0, 4], (4 - 0)/4 )
xs3 = linspace( 0, 4, 9 );
f623 = f62( xs3 );
a3 = 0.5*(f623(1) + 2*sum(f623(2:end-1)) + f623(end))*(4 - 0)/8
a3c = comp_trap( @f62, [0, 4], (4 - 0)/8 )
Copy and paste your Matlab commands and the output here.
4
7.1d We need to approximate the average value:
b
f
(2)
62 [ a ,b ]
1

f 62(2)  x  dx .

ba a
Fortunately, this is easy: by the fundamental theorem of calculus, we have that
b
f 62(2) [ a ,b ] 
1
f 62(2)  x  dx 

ba a
 f  x
(1)
62
ba
Thus, we may simplify the error formula to be 

b
a

f 62(1)  b   f 62(1)  a 
.
ba
h 2 1
f (b)  f 1 (a)
12

Thus, using Dc and richardson22 to approximate f62(2) [0,4] by using both f 62(1)  4  and
f 62(1)  0  (you implemented f62) and to then take the difference and assign this result to
the variable delta. Use an initial h = 0.01 and let step = 10-10 and Nmax = 100. Your
answer should be approximately –1.0549.
Copy and paste your Matlab commands and the output here.
7.1e Finally, you calculated the value exact. When we calculated a3c, we used a value of
h = 0.5. Therefore, demonstrate that
h = 0.5;
actual_error = exact - a3c
approx_error = -h^2/12*delta
Is the approximation of the error a good approximation of the actual error? Discuss this
referring to the relative error of the approximation of the error.
Copy and paste your Matlab commands and the output here.
Your answer here.
5
7.2a Find the maximum eigenvalue of the matrix
 4

0.2
M1  
 0.3

 0.1
0.2 0.3 0.1 

8
0.4 1.1 
.
0.4 10
0.5 

1.1 0.5 12 
with N = 100 and step = 10-6.
Enter your code and the output here.
7.2b Find all the eigenvalues of the matrix M1 in Question 7.2a using the eig function:
>> eig( M1 )
% Enter your output here
7.2c Our algorithm only finds the maximum eigenvalue, but this can still be useful.
Consider the arbitrary matrix
 1 2 3


M2   4 5 6  .
7 8 9


If we calculate the square root of the maximum eigenvalue of M 2 M 2T (that is, M2*M2'),
we call this the norm of the matrix. This number is the maximum that the matrix will
stretch a vector. Demonstrate this by doing the following:
>>
>>
>>
>>
sqrt( max_eig( M2*M2', 1e-5, 1000 ) )
norm( M2 )
maximum = 0;
for i = 1:100000
v = rand( 3, 1 );
maximum = max( maximum, norm( M2*v )/norm( v ) );
end
>> maximum
You will note that both v and M2v are vectors, and thus, norm( M2*v )/norm( v )
M2v 2
simply calculates
where v 2 is the Euclidean norm of the vector, that is,
v2
6
v 2  vT v  v12  v22 
 vn 2 .
Enter your output here
7.2d Repeat 7.2c but with a random 10 10 matrix called M3. You do not have to copy
the matrix here, just copy the output of the appropriately modified commands shown in
7.2c.
Enter your output here
Also include the output of:
>> eig( M3 )
>> eig( M3*M3' )
Enter your code and the output here
Are any of the eigenvalues of M3*M3' complex or negative?
Enter your answer here.
7
Download