Lab 7
Lab 7
Lab 7
Introduction:
This experiment is designed to introduce to the student to some of the basic procedures in
MATLAB in preparation for other courses where it will be used regularly.
Procedure:
1. Help Files: MATLAB has a good internal help command. In the MATLAB
window, type help then press the enter key.
Linear Algebra:
All MATLAB variables are treated as arrays (matrices). This is slightly intimidating at
first, but the advantages soon become clear.
Procedure:
A = [1 2; 3 4]
2. Notice that the semicolon, ; , indicates a new row in the matrix. Create a second
matrix, B.
B = [5 6; 7 8]
3. Enter the variable name, A, as a command and notice that MATLAB remembers
everything you’ve entered previously.
4. The colon symbol, : , can be used to select columns or rows within a matrix.
Try the following commands:
y = A(1,:)
z = B(:,2)
A+B
A*B
A/B
A = [2 3; 1 -3]
B = [10;8]
X = inv(A)*B
7. MATLAB matrices are not limited to real numbers. MATLAB defines i as the
square root of (-1). However, in electrical engineering, we reserve i for current
and therefore use j as the imaginary unit vector. Type in the following to make a
complex matrix and calculate its determinant.
j = sqrt(-1)
A = [ 1+j*2 3+j*4 ; 5+j*6 7+j*8 ]
B = det(A)
8. FOR YOUR REPORT: Write Nodal or Mesh equations for the following circuit
and solve the equations in MATLAB to find Vx and Ix.
Procedure:
t = [0 : 0.1 : 10]
a = 10*sin(2*t)
4. Make the 3rd and 5th harmonic, add them to the fundamental sine wave and plot
the result (note that the semi-colon allows multiple commands on one line):
b = 3*sin(6*t); c = 1.5*sin(10*t); d = a + b + c
plot(t,d) (SCILAB command is plot2d(t,a))
5. FOR YOUR REPORT: Solve the following first order differential equation by
any method of your choosing, describe the solution as an equation in MATLAB
and plot it versus an appropriate time range.
MATLAB can be considered a fully functional programming language. As such it has the
ability to load external code (known as a script in MATLAB) and execute it. It also
allows users to create subroutines (known as functions in MATLAB). One can even call
a user-defined function from within another user-defined function (nesting). MATLAB
has an editor/debugger window that lets you create and debug a script called an M-File.
Procedure:
1. In MATLAB (SciPad instructions below), click on File in the top menu bar. In
the drop down window, click New>M-File. An editor window will appear. In
that editor window, type in the following script (note that the % symbol denotes a
comment line and the semi-colon, ; , will tell MATLAB to suppress the output of
that line when the script is executed):
Otherwise, in SCILAB, click on the new file symbol on the top menu bar, creating
a new SciPad window (note that the // denotes a comment line and the semi-
colon, ; , will tell SCILAB to suppress the output of that line when the script is
executed in the main program). Type in the following text:
2. In MATLAB, if you were to change any of the above code, creating an obvious
error, note that the erroneous code will automatically be underlined in red,
indicating a problem. Hovering the mouse-pointer over the red underlining will
show a description of the error. Now, in the editor window, click on Debug in the
top menu bar and select Save and Run in the drop-down menu. Your code will be
executed in MATLAB.
In SCILAB, there is no direct error detection in the SciPad editor window. In the
SciPad Window, click on Execute>Load into Scilab. Your code will be executed.
3. Making M-file scripts such as the one above is an excellent way to develop
MATLAB code and also a great way to save a copy of what you’ve done.
4. The other way to use the M-File concept is to develop a custom function. Open a
new script window as described in step 1 above. Enter the following code in a
new script window:
endfunction
6. MATLAB should now be able to call stat as a function. In the main MATLAB
command window, enter the following code. In SCILAB, in the SciPad window,
click on Execute>Load into SciLab, then execute the following code in the main
Scilab command window:
z = [.1 : .1 : .5]
[zmean, zstdev] = stat(z)
7. FOR YOUR REPORT: Write an M-File (or SciPad file) script to create two
custom functions called RtoP and PtoR. As a function, RtoP(x) will perform a
rectangular to polar conversion of the complex number, x. Similarly, PtoR(x,y)
will convert polar form to rectangular, where x = magnitude and y = angle in
degrees.
Conclusion
Your report will have a short introductory paragraph and a conclusion. In between create
three sections describing the work you performed to complete the tasks at the end of each
portion above. Include all MATLAB code, plots, etc.