matlab
1250 S 14
Homework MATLAB 4
QOD: What is the special MATLAB format for the first line of a user defined function, and what does each term on the first line mean?
1. a) Write a MATLAB command to create the following matrix:
A =
1 1 1 1 1 0 0 0 0 1 0 0 2 3 4 5
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
b) Write a MATLAB command that sets matrix B equal to rows 3 and 4 of matrix A, (by extracting rows 3 and 4 of A).
2. Write a function called Rpar.m to compute the parallel resistance of two resistors. The function would be used as follows (and you should show this command gives the proper output with your Rpar function) to compute the resistance of the network shown below: >> Rtot = 1e3 + 3.3e3 + Rpar(10e3,1.5e3 + Rpar(2.2e3,4.7e3)) Rtot = 6.6068
3. Write MATLAB functions for the op-amp cards listed below. A code sample is given for each case to show how your function should work. Also, a function for a subtracting amp is given below.
>> R1 = 1e3; % 1 kohm for both parts (a) and (b) below >> R2 = 2e3; % 2 kohm >> Rf = 3e3; % 3 kohm >> Rs1 = 1e3; % 1 kohm for part (b) >> Rs2 = 2e3; % 2 kohm >> v1 = 1.5; % v1 = 1.5 V (Arbitrary value I chose) >> v2 = 0.5; % v2 = 0.5 V (Another arbitrary value)
a) Inverting Summer: >> v0 = inv_sum([R1,R2,Rf],v1,v2) % R's are in an array v0 = -5.2500
b) Non-Inverting Summer >> v0 = non_inv_sum([R1,R2,Rs1,Rs2],v1,v2) % R's are in an array v0 = 1.7500
Example function for Subtracting Amplifier. function [vout,G1,G2] = subtracting_amp(Rvals,v1,v2) % Usage examples: % vout = subtracting_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 subtracting_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
4. Show that superposition works for your op-amp function in Problem 3.a. That is, use the following commands with one voltage source turned on at a time, and show that you get the same final value of v0:
>> v01 = inv_sum([R1,R2,Rf],v1,0); % v1 on, v2 off >> v02 = inv_sum([R1,R2,Rf],0,v2); % v1 off, v2 on >> v0 = v01 + v02 v0 =
-4.0833