Sei sulla pagina 1di 3

1250 Homework MATLAB 6

S 14

QOD: How do you set up a repetitive calculation in MATLAB without just repeating the
code over and over again?

1. Complete the MATLAB script file called sum_sines.m below that finds the output of
a subtracting amplifier for two input voltages, v1 and v2, where v1 changes with time
and v2 is constant, (see code below). That is, create a 'for' loop that steps through the
entries in v1 and passes them into the sub_amp function. Build an array, called v0, of
the return values. That is, start with an empty v0 and add each return value to the end
of the v0 array. After the for loop, create a plot of v0 versus t. Be sure to label the
plot!

% sum_sines.m Script file to plot output waveform for op-amp.

t = 0:30 % time goes from 0 to 30 seconds


v1 = 1.5*sin(0.1*pi*t); % v1 = 1.5 V sinusoid at 0.1 Hz
v2 = 0.5; % v2 = 0.5 V constant
R1 = 1e3; % 1 kohm for both parts (a) and (b) below
R2 = 2e3; % 2 kohm
R3 = 3e3; % 3 kohm
R4 = 4e3; % 4 kohm

% Start with empty v0 array for results.


v0 = [];

% Add loop to step through values of v1 and call sub_amp


% function each time. Append the v0 value to the end of the
% v0 array each time through the loop.

% Plot v0 versus t.

Example function for Subtracting Amplifier.


function [vout,G1,G2] = sub_amp(Rvals,v1,v2)
% Usage examples:
% vout = sub_amp ([R1,R2,R3,R4],v1,v2)
% [vout,G1,G2] = subtracting_amp ([R1,R2,R3,R4],v1,v2)
%
% Computes output voltage for subtracting op-amp circuit.
% Rvalues are resistors in circuit.
% Note: R values must be in order shown
% v1 is input on - side of amplifier
% v2 in input on + side of amplifier

R1 = Rvals(1); % Unpack array of resistor values for clarity.


R2 = Rvals(2);
R3 = Rvals(3);
R4 = Rvals(4);
G1 = -R2/R1;
G2 = (R1+R2)/R1 * R4/(R3+R4);

vout = G1*v1 + G2*v2;


return

Example of using sub_amp


v1 = 1; % v1 = 1 V
v2 = 0.5; % v2 = 1/2 V
% We use the three output version here. We can also use just
% one argument: v0 = subtracting_amp([1,2,3,4]*1e3,v1,v2)
[v0,G1,G2] = subtracting_amp([1,2,3,4]*1e3,v1,v2)
v0 =
-1.1429
G1 =
-2
G2 =
1.7143

2. a) Using MATLAB, create the following A and B arrays:


⎡ 1 0 −1 ⎤ ⎡ 1 1 0 ⎤
A=⎢ 2 0 0 ⎥ B = ⎢ −2 0 0 ⎥
⎢ ⎥ ⎢ ⎥
⎢⎣ 0 3 1 ⎥⎦ ⎢⎣ 1 0 1 ⎥⎦
b) Use MATLAB commands to compute the following:
i) A and B, ii) A or B, iii) A exclusive-or B, iv) not A

3. In this problem, you will simulate an 'electronic candle' circuit that you will build in
Lab 6. The following script and function files initially create an 8-bit binary pattern
of all ones and then shift the pattern left one bit each time through a loop. One bit
falls off the left end when shifted, and a new bit is randomly placed in the position
left open on the right end. The new pattern is the return value for the function. In the
script file, the lowest-order bit is appended to an array that is eventually printed out.
By calling the function inside a 'for' loop, a random sequence of 1's and 0's is
obtained.
a) Type in the script file and function below. Run the script file and record the
output.
b) Change the function file so that, instead of putting a random 0 or 1 into the empty
bit of the shift register, the value is the exclusive-or of the 1st and 7th bits in the
pattern before the shift. (The 1st bit will be the value put into the empty bit on the
previous iteration.) You should get a random pattern that repeats every 127 bits.
% candle.m Script file to print out a random pattern of 0's and
% 1's. When implemented as a logic circuit, the output
% will randomly light an LED, making it look like a candle.

% Start with a pattern of all 1's.


pattern = [1, 1, 1, 1, 1, 1, 1, 1];

% Initialize array that will hold output bit pattern.


bit_pattern = [];

% Use a for loop to call a function that shifts the pattern left and
% enters a random 0 or 1 into the last bit.
for index = 1:20
% Call function to shift pattern left.
pattern = shifter(pattern);

% Append the lowest-order bit to output array.


bit_pattern = [bit_pattern, pattern(8)];
end

disp(bit_pattern)

function new_pattern = shifter(old_pattern)


% Usage: new_pattern = shifter(old_pattern)
% old_pattern = 8-bit pattern of 0's and 1's (not all 0's)
% new_pattern = input pattern shifted left one bit and new
% random 0 or 1 placed in last bit.

% Eliminate the leftmost bit of pattern.


new_pattern = old_pattern(2:8);

% Create a random 0 or 1.
new_bit = rand(1) > 0.5;

% Insert new random bit at the end of pattern.


new_pattern = [new_pattern, new_bit];

end

Potrebbero piacerti anche