hw1 solution
DESCRIPTION
circuits homework1 solutionTRANSCRIPT
1
EECS 215: Introduction to Electronic Circuits FA15
Homework Set 1 Solutions
© Fred Terry 9/17/15
Problem 1 Solution
I will use one m-file for all parts of this problem. There are some comments in the code on specific
issues. Keys to the problem:
I set up time t in seconds, frequency in cycle/second (Hz) and scale the plots for the desired
units of time.
We have to use ‘.*’ not ‘*’ alone to multiply arrays on a point but point basis.
%problem 1 clear all; close all; f0=1e4; t=-2:1e-3:1; % in ms spaced by 1e-3 ms t=t*1e-3; % convert t into seconds f1=1e3; f2=500; % part a y0=5*cos(2*pi*f0*t); plot(t*1e6,y0,'linewidth',2); % multiplying t by 1e6 for plot converts to
microseconds ylabel('Voltage'); xlabel('t (\mus)'); title('Problem 1(a)'); grid; % part b m=0.25; y1=y0.*(1+m*cos(2*pi*f1*t)); % note the .* figure; plot(t*1e6,y1,'linewidth',2); ylabel('Voltage'); xlabel('t (\mus)'); title('Problem 1(b)'); grid; % part c y2=y0.*(1+m*cos(2*pi*f1*t)).*(1+m*cos(2*pi*f2*t)); figure; plot(t*1e6,y2,'linewidth',2); ylabel('Voltage'); xlabel('t (\mus)'); title('Problem 1(c)'); grid;
2
-2000 -1500 -1000 -500 0 500 1000-5
-4
-3
-2
-1
0
1
2
3
4
5
Voltage
t (s)
Problem 1(a)
3
-2000 -1500 -1000 -500 0 500 1000-8
-6
-4
-2
0
2
4
6
8
Voltage
t (s)
Problem 1(b)
4
-2000 -1500 -1000 -500 0 500 1000-8
-6
-4
-2
0
2
4
6
8
Voltage
t (s)
Problem 1(c)
5
Problem 2 Solution
First I’ll read and plot the data using m-file:
%problem 2 clear all; close all; data=csvread('prob2_data.csv'); plot(data(:,1),data(:,2)); % column 1 is x, column 2 is y grid;
Resulting in the plot
Note that this looks parabolic with a minimum that appears to be at about (0.2, 1.2) integrating by eye
through the noise. Thus we would guess a fitted curve of the form 2
0.2 1.2y C x .
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 11
1.5
2
2.5
3
3.5
4
4.5
5Raw Data Only
6
Also note that we seem to have y=4.5 at x=-1 (again fitting by eye for the noise). Thus we estimate:
2
2
2
2 2
0.2 1.2
4.5 1 0.2 1.2
4.5 1.2 1.2 2.292
2.292 0.2 1.2 2.292 2.692 1.292
y C x
C
C
y x x x
Using the following m-file
%problem 2 clear all; close all; data=csvread('prob2_data.csv'); x=data(:,1); y=data(:,2); plot(x,y); % column 1 is x, column 2 is y grid; C=(4.5-1.2)/(-1.2)^2 yfit=C*(x-0.2).^2+1.2; figure; plot(x,y); hold on; plot(x,yfit,'r','linewidth',2); grid; legend('data','fit');
We get
7
Note that the fit appears to be good. This is all l expected for this problem; however, you could use
numerical procedures to get a least-squares fit. One of several solutions is shown below and yields a
least-squares result of (to an excessive number of digits):
22.301193248761393 0.919310931047964 + 1.291204901139047y x x
Note that these coefficients are pretty close to those we found from eye-ball procedures (Chi-by-eye to
numerical folks).
%problem 2 clear all; close all; data=csvread('prob2_data.csv'); x=data(:,1); y=data(:,2); plot(x,y); % column 1 is x, column 2 is y grid; title('Raw Data Only'); C=(4.5-1.2)/(-1.2)^2 yfit=C*(x-0.2).^2+1.2; figure;
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 11
1.5
2
2.5
3
3.5
4
4.5
5 by Eye Approach
data
fit
8
plot(x,y); hold on; plot(x,yfit,'r','linewidth',2); grid; legend('data','fit'); title('\chi by Eye Approach'); % least squares pfit=polyfit(x,y,2) % fit quadratic to data yfit2=pfit(1)*x.^2+pfit(2)*x+pfit(3); figure; plot(x,y); hold on; plot(x,yfit2,'r','linewidth',2); grid; legend('data','fit2'); title('Non-linear Least Squares Using Polyfit');
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 11
1.5
2
2.5
3
3.5
4
4.5
5Non-linear Least Squares Using Polyfit
data
fit2
9
Problem 3 Solution
I’m going to do the general set-up to solve both (a) and (b), then I will look at these individual solutions.
First I will reorder the equations for my convenience:
1 3 1
4 5 4 3 2 3
0 3 3 1 3 2 3 4
2 4
5
0 1 2
10 5 5 0
10 2 8 2 0
4 1 5 2 8 0
1
L
V K i i K i
V K i i K i i K i i
K I K i K i i K i i K i i
i i mA
i I
I i i
I will then group terms and put knowns on the right hand side of the equations.
1 3
2 3 4 5
1 2 3 4 0
1 2 0
2 4
5
10 5 10
2 10 10 2 10
5 2 16 8 4 0
0
1
L
K i K i V
K i K i K i K i V
K i K i K i K i K I
i i I
i i mA
i I
Now include all the “0” terms in order:
10
1 2 3 4 5 0
1 2 3 4 5 0
1 2 3 4 5 0
1 2 3 4 5 0
1 2 3 4 5 0
1 2 3
10 (0) 5 (0) (0) (0) 10
0 2 10 10 2 (0) 10
5 2 16 8 (0) 4 0
1 1 0 (0) (0) 1 0
0 1 0 ( 1) (0) 0 1
0 0 0 (
K i i K i i i I V
i K i K i K i K i I V
K i K i K i K i i K I
i i i i i I
i i i i i I mA
i i i
4 5 00) (1) 0 Li i I I Now map this row by row to a 6x6 matrix equation
1
2
3
4
3
5
0
1010000 0 5000 0 0 0
100 2000 10000 10000 2000 0
05000 2000 16000 8000 0 4000
01 1 0 0 0 1
100 1 0 1 0 0
0 0 0 0 1 0 L
i V
i V
i
i
i A
I I
a) For this part, 0LI , so we can solve the above problem purely numerically in Matlab. This
file:
%problem3 clear all; a=[1e4 0 -5000 0 0 0; 0 2000 -1e4 1e4 -2000 0; -5000 -2000 16e3 -8000 0 4000; 1 -1 0 0 0 -1; 0 1 0 -1 0 0; 0 0 0 0 1 0]; vvec=transpose([10 -10 0 0 1e-3 0]); % transpose make vvec a column vector format long; % to see high precision result ivec=a\vvec % doing ivec=inv(a)*vvec would also work
Yields:
ivec =
0.000086956521739
-0.001521739130435
-0.001826086956522
11
-0.002521739130435
0
0.001608695652174
Which is in the form:
1
2
3
4
5
0
i
i
i
i
i
I
b) I can do this part in one of two ways. First, I can use Matlab to get the matrix inverse and
manually do row/column evaluation. This is total fine. The inverse matrix values are messy here,
but the result will be the same as in my more elegant solution below.
If I use the symbolic tool box of Matlab, I can have it do the work for me and produce and exact
answer. The new m-file below:
%problem3 clear all; a=[1e4 0 -5000 0 0 0; 0 2000 -1e4 1e4 -2000 0; -5000 -2000 16e3 -8000 0 4000; 1 -1 0 0 0 -1; 0 1 0 -1 0 0; 0 0 0 0 1 0]; vvec=transpose([10 -10 0 0 1e-3 0]); % transpose make vvec a column vector format long; % to see high precision result ivec=a\vvec % doing ivec=inv(a)*vvec would also work %% get inv(a) to do part b numerically inv(a) %% part b done using symbolics IL=sym('IL','real'); % make IL a symbolic term vvec2=transpose([10 -10 0 0 1e-3 -IL]); % transpose make vvec a column vector ivec2=a\vvec2; eval(ivec2) % eval just cleans up the fractions
Produces:
1/11500 - (7*IL)/23
- (31*IL)/46 - 7/4600
- (14*IL)/23 - 21/11500
- (31*IL)/46 - 29/11500
12
-IL
(17*IL)/46 + 37/23000
Again in the format:
1
2
3
4
5
0
1/11500 7* / 23
31* / 46 7 / 4600
14* / 23 21/11500
31* / 46 29 /11500
17* / 46 37 / 23000
L
L
L
L
L
L
i
i
I
I
I
I
Ii
I
i
i
I