You are on page 1of 45

Georgia Institute of Technology

School of Electrical and


Computer Engineering

Mathcad and Matlab


User’s Manual

Lisa Desai
ECE 3041 L03
Table of Contents

CHAPTER 1
1.1 Introduction 1
1.2 System Requirements 1
1.2.1 Matlab System Requirements 1
1.2.2 Mathcad System Requirements 2

CHAPTER 2
Inverting Systems of Equations Using Mathcad
2.1 Defining Variables 3
2.1.1 How to Define Variables in Mathcad 3
2.1.2 How to add units to variables 4
2.1.3 Defining Resistor (R) Variables 4
2.1.4 Defining Conductance (G) Variables 5
2.1.5 Defining Current (I) variables 5
2.2 Creating Matrices 6
2.3 Displaying Answer 9

CHAPTER 3
Inverting Systems of Equations Using Matlab
3.1 Defining Variables 10
3.1.1 How to Define Variables in Matlab 10
3.1.2 Defining Resistor (R) Variables 11
3.1.3 Defining Conductance (G) Variables 11
3.1.4 Defining Current (I) Variables 12
3.2 Creating Matrices 13

CHAPTER 4
Two-Dimensional Plots using Mathcad
4.1 Defining Variables 14
4.2 Creating Graphs 15
4.3 Adding Labels to Graphs 17

CHAPTER 5
Two-Dimensional Plots using Matlab
5.1 Defining Variables 19
5.2 Creating Graphs 20
5.3 Adding Labels to Graphs 21

CHAPTER 6
Bode Plots using Mathcad
6.1 Defining Variables 23
6.2 Creating Vectors 25
6.3 Creating Bode Plot 26
CHAPTER 7
Bode Plots using Matlab
7.1 Defining Variables 31
7.2 Defining Transfer Functions 32
7.3 Creating Bode Plots 33
7.4 Changing Units 34

CHAPTER 8
Troubleshooting
8.1 Problems with Matrix Multiplication 36
8.2 Problems with Defined Variables 36
8.3 Problems with Units in Mathcad 36

APPENDIX A: Glossary 38
CHAPTER 1

1.1 Introduction
Matlab and Mathcad are software programs that have the capability to perform intensive
mathematical computations faster than traditional methods. As an engineering student it
is important to be familiar with these two programs, as they will be used extensively in
your studies.

Three types of problems are introduced in this instruction guide. They are systems of
equations, two-dimensional plots, and bode plots. These three problems are very
important to an engineering student. By the end of this guide, you will be familiar with
Matlab and Mathcad. You will also be familiar with solving equations and plotting. You
will also notice the differences in solving problems and interfaces of Matlab and
Mathcad.

Matlab and Mathcad both have their advantages but Mathcad has proven to be easier to
use. In chapter 2 and 3 the problem that is solved is systems of equations. In Mathcad
entering a matrix is much easier than Matlab. When entering a matrix in Mathcad, an
actual matrix appears whereas in Matlab you enter it in like a vector with the rows
separated by semicolons. Also Mathcad allows you to enter functions of an undefined
variable that can then be inputted later like in Chapter 4 and 6 when defining voltage and
transfer functions. In Matlab every variable must be defined before being used. Even
variables that vary like time.

1.2 System Requirements


1.2.1 Matlab System Requirements

• Operating System
o Microsoft Windows XP (SP1 or SP2)
o Microsoft Windows 2000 (SP3 or SP4)
o Microsoft Windows Server 2003
o Microsoft Windows Vista
• Processors
o Intel Pentium (IV or above)
o Intel Celeron

1
o Intel Xeon
o Intel Core
o AMD Athlon
o AMD Opteron
• 500 MB of disk space
• 512 MB of RAM (1024 MB Recommended)

1.2.2 Mathcad System Requirements

• Operating System
o Microsoft Windows XP Service Pack (SP) 2
o Microsoft Windows Home Edition SP2
• Processor: Pentium/Celeron, 400 MHz or higher
• 550 MB of disk space
• 256 MB of RAM (512 MB recommended)

2
CHAPTER 2

Inverting Systems of Equations Using Mathcad

Consider the following system of equations arranged in matrices:

G1 + G2 G2 0 0 0  VA   I1 


     
 G2 G2 + G4 + G5 G5 0 G4  VB   0 
 0 G5 G5 + G7 G7 0  * VC  =  I 2 
     
 0 0 G7 G7 + G6 G6  VD   I 2 
 0 G4 0 G6 G3 + G4 + G6  VE   0 

Given the values for G and I, you will be solving for the V values.
G is conductance and is defined as 1/R where R is resistance.
I is current and the unit is amps.
V is voltage and the unit is volts.

2.1 Defining Variables


This section will first show you how to define variables in Mathcad and then take
you through defining the variables in the problem above.

2.1.1 How to Define Variables

To define variables in Mathcad (also in Appendix A):


• Choose the variable (e.g. R)
• Enter the variable (e.g. R) + colon or select := on the
evaluation toolbar

3
Figure 1. Evaluation Toolbar.

• Enter the value for the variable


o Ex. Define R = 100
o Keyboard sequence for this would be R, colon, 100

2.1.2 How to Add Units to Variables

To insert units, multiply the variable (asterisk) and then click on the icon
for the measuring cup on the top of the command bar as shown in the
figure below.

Figure 2. Measuring cup icon in Mathcad outlined in red.

Select the appropriate unit and click on it

2.1.3 Defining Resistor Variables

1. Start Mathcad
2. Left click in whitespace/workbook
3. Define R1 = 20*k. Keyboard sequence for this would be R, period, 1,
colon, 20, asterisk, click on measuring cup on command bar, click on
kilo-ohms
4. Left click below the definition of R1 or hit enter after the previous step.
5. Define the following variables in the same manner as R1
a. R2 = 68* k
b. R3 = 18* k
c. R4 = 33* k
d. R5 = 15* k
e. R6 = 24* k
f. R7 = 43* k

All R variables defined will look like Figure 3, shown below

4
Figure 3. All resistor variables defined in Mathcad.

2.1.4 Defining Conductance (G) Variables

Now we’re going to define the conductance variables (G)


In the same manner as above, define the following
• G1 = 1/R1
• G2 = 1/R2
• G3 = 1/R3 Note: Mathcad is
• G4 = 1/R4 case-sensitive.
• G5 = 1/R5
• G6 = 1/R6
• G7 = 1/R7

All the G variables defined will look like the following figure

Figure 4. All G variables defined in Mathcad.

2.1.5 Defining Current (I) Variables

In the same manner as above, define the following:


• I1 = 5*mA
• I2 = 3*mA

All the I variables defined will look like Figure 5, shown below

Figure 5. All I variables defined in Mathcad.

Note: Mathcad works from top to bottom. A


variable must be defined before it is used.

5
2.2 Creating Matrices
1. To insert a matrix, go to InsertMatrix

Note: Keyboard
shortcut for
inserting a
matrix is Ctrl+m

Figure 6. Inserting a matrix.

2. The following will then appear

Figure 7. Creating a matrix. Tip: Once you enter


the first value in the
3. Enter 5 for rows and 1 for columns
matrix, hitting the
4. Click Ok
Tab key will get
5. After you click ok, this will appear
you to the next
placeholder in the
matrix.

Figure 8. A 5x1 matrix.

6. Enter the following variables so that your matrix looks like the
following

6
Figure 9. Creating the voltage matrix.

7. To define the matrix, type colon


8. Insert a new matrix (Ctrl+m)
9. Enter 5 for rows and 5 for columns. Then click ok. The following will
appear

Figure 10. Creating a 5x5 matrix.

10. Add the following variables so that your matrix looks like the
following figure

Figure 11. Entering the G matrix.

11. Push the right arrow to move the cursor outside the matrix
12. Raise the matrix to the -1 power by typing ^, -, 1

Figure 12. Taking the inverse of the G matrix.

13. Push the right arrow to move the cursor out of the power

7
14. Type * (asterisk on the keyboard) to multiply
15. Insert a 5 by 1 matrix (Ctrl + m)

Figure 13. Adding a 5x1 matrix.

16. Type in the following variables so that your matrix looks like the
following

Figure 14. Creating the I matrix.

After carrying out the above steps, you should have the following

1
 VA   G1 + G2 G2 0 0 0   I1 
 VB   G2 G2 + G4 + G5 G5   
0 G4
     0 
 VC  :=  0 G5 G5 + G7 G7 0    I2 
 
V   0 0 G7 G7 + G6 G6
  I2 
 D    
 VE   0 G4 0 G6 G3 + G4 + G6   0 
   
Figure 15. The complete definition of the voltage matrix.

8
2.3 Displaying the Answer
1. Insert a 5x1 matrix (Ctrl+m)
2. Enter the following

 VA 
 VB 
 
 VC 
V 
 D
 VE 
 
Figure 16. Entering the voltage matrix.

3. Enter = on the keyboard and the result will automaticaly be evaluated

The answer will look like this

Figure 17. The final answer.

Note: If an error is made, the text will turn red.

You have just successfully solved a system of equations in Mathcad!

9
CHAPTER 3

Inverting Systems of Equations Using Matlab

In this section you will learn how to do the example from Chapter 2 in Matlab.
Consider the following system of equations arranged in matrices

G1 + G2 G2 0 0 0  VA   I1 


     
 G2 G2 + G4 + G5 G5 0 G4  VB   0 
 0 G5 G5 + G7 G7 0  * VC  =  I 2 
     
 0 0 G7 G7 + G6 G6  VD   I 2 
 0 G4 0 G6 G3 + G4 + G6  VE   0 

Given the values for G and I, you will be solving for the V values.
G is conductance and is defined as 1/R where R is resistance.
I is current and the unit is amps.
V is voltage and the unit is volts.

3.1 Defining Variables


This section will walk you through defining variables in Matlab. It is a bit
different than in Mathcad, so be sure to play close attention. Note that variables
must always be defined before they are used or you will get an error message.

3.1.1 How to Define Variables in Matlab

• Choose the variable (e.g. R)


• Enter the variable, then =, then enter the value for the variable
• Example
o Ex. Define R = 100
o Keyboard sequence would be R equal 100

10
To prevent Matlab
from repeating the
entry back after you
hit enter, place a
semicolon at the end
of the definition.

Figure 18. Defining R.

3.1.2 Defining Resistor Variables

1. Start Matlab
2. Define R1 = 20. Keyboard sequence would be R,1, equal, 20,
semicolon, enter
3. Define the following variables in the same manner as R1
a. R2 = 68
b. R3 = 18
c. R4 = 33
d. R5 = 15
e. R6 = 24
f. R7 = 43

All R variables defined will look like the following figure

Figure 19. Defining the R variables.

3.1.3 Defining Conductance (G) Variables

Now you’re going to define the G variables in the same manner as you did
above for the resistor values.

In the same manner as above, define the following


• G1=1/R1;
• G2=1/R2;
• G3=1/R3;
• G4=1/R4;
• G5=1/R5;

11
• G6=1/R6;
• G7=1/R7;

All G variables defined will look like the following

Figure 20. Defining the G variables.

3.1.4 Defining Current (I) Variables

In the same manner as above, define the following


• I1 = 5;
• I2 = 3;

These variables defined will look like the following figure

Figure 21. Defining the I variables.

12
3.2 Creating Matrices
This section will guide you through creating a matrix in Matlab and then show
you how to solve the system of equations presented at the beginning of the
chapter.

Matrices are defined differently in Matlab than in Mathcad.


A matrix like the following

 R1 R2 
R R4 
 3
Figure 22. A matrix.

Will be defined in Matlab like this


R = [R1 R2; R3 R4];

The semicolon between R2 and R3 separates the different rows.

1. To define the conductance matrix, enter the following

Figure 23. The G matrix defined.


2. To define the current (I) matrix, enter the following

Figure 24. The I matrix defined.


3. To evaluate for the voltage (V) matrix, enter the following Note: Do NOT put the
semicolon at the end
of the V definition so
that the answer will
display.

Figure 25. The V matrix defined and evaluated.

13
CHAPTER 4

Two-Dimensional Plots Using Mathcad

The following function is going to be plotted with three different values of R all on the
same plot.
I 2*
v(t) = * * exp( * t) * sin( d * t) * u(t)
G 1  2

I is current.
G is the inverse of resistance (1/R).
 is the greek letter for zeta.
 is the attenuation coefficient
d is the frequency in radians/sec.

4.1 Defining Variables


Start Mathcad.
Define the following variables. Define them in the order given below

14
Figure 26. All variables defined.

15
4.2 Creating Graphs
1. To create a graph, view the graph toolbar (ViewToolbarsGraph) and click
on 2-D graph

Figure 27. Graph toolbar.

You can also use the keyboard shortcut Shift+2 for a 2-D graph.

2. Once you click on 2-D graph you will get the following

Figure 28. Two-dimensional plot.

The middle placeholder on the x and y-axis is for defining the axis. The
placeholders outside of the middle one are for defining the range of that axis

3. Define the x-axis like the figure below

16
Figure 29. X-axis defined.

4. Define the y-axis like the figure below

Figure 30. Y-axis defined.

o To get multiple functions on the same graph, first place the cursor
in the middle placeholder.
o Then enter v(t,3*k)
o Hit comma on the keyboard
o Another placeholder will appear to enter the second function
o Enter v(t,1*k) followed by a comma
o Enter v(t, 500*)

5. Click outside the graph so Mathcad will plot the function

17
Figure 31. Damped sine plots.

4.3 Adding Labels to graphs


• Place the cursor inside the graph and double click
• Go to the Labels tab
• Type “v(t) vs. t” for the title
• Type “time” for x-axis label
• Type “voltage” for y-axis label

Your graph will then look like the figure below

18
Figure 32. Damped sine plots with labels.

Close Mathcad.

19
CHAPTER 5

Two-Dimensional Plots Using Matlab

The following function is going to be plotted with three different values of R all on the
same plot.

I is current. G is the inverse of resistance (1/R).  is the greek letter for zeta.  is the
attenuation coefficient. d is the frequency in radians/sec.

5.1 Defining Variables


Start Matlab.
Define all the variables shown in the figure below

Figure 33. All variables defined.

19
For definition of
5.2 Creating Graph linspace type help
linspace in matlab
• First define the vector t for the time variable as shown below window or look in
the glossary.

Figure 34. Time vector.

• Define the voltage functions of time as shown below

Figure 35. Voltage functions.

• Do the following in the figure below to graph all 3 functions on one graph

Figure 36. Argument to plot the voltage.


• The graph will look like Figure 37, shown below

20
Figure 37. Damped sine waves.

5.3 Adding Labels to Plots


Note: Do not close
To add labels do the following out the plot before
adding labels
otherwise you will
have to plot the three
functions again to add
labels to it.
Figure 38. Adding labels to plot.

The graph will then look like the following figure

21
Figure 39. Damped sine wave plot with labels.

Close Matlab.

22
CHAPTER 6

Creating Bode Plots in Mathcad

Bode plots are used to plot the magnitude and phase of transfer functions in decibels and
degrees of frequency. The magnitude is also considered the gain of the function, which is
plotted on a linear scale. The frequency is plotted on a log scale.

The following transfer function is going to be plotted

6.1 Defining Variables


• Start Mathcad
• Define the following variables in Mathcad

Figure 40. Variables defined.


• Define the following transfer function

23
Figure 41. Transfer function defined.

• Now define the magnitude and phase functions as shown below

Figure 42. Magnitude and phase functions.

24
6.2 Creating Vectors
Instead of using the default number of points like we did in the last chapter, we
are going to create a frequency vector.

Define the following in Mathcad

i is an array. Keyboard
shortcut for this is i, colon,
0, semicolon, N-1

The i on f is NOT a subscript. It is an array


index. The keyboard shortcut is the left square
bracket. On the Matrix toolbar it is the Xn icon.
Figure 43. Vectors in Mathcad.

25
6.3 Creating Bode Plot
1. Start the 2D graph by the shortcut shift+2
2. Set the x-axis as shown below

Figure 44. Setting the x-axis.


3. Set the y-axis as shown below

Recall: Add multiple


functions on one graph by
hitting comma after entering
the first one and another
placeholder will appear for a
second function.

Note: fi on the x-axis and y-axis is not a


subscript. It is an array. Use the Xn icon
on the matrix toolbar to enter this.

Figure 45. Setting the y-axis.

4. The following plot will appear

26
Figure 46. The plot on a linear scale.

5. Place the pointer on the graph and double click. This will appear

Figure 47. Changing the x-axis scale.

6. Click on Log Scale under X-axis section.


7. Click Ok. The graph will now look like the following

27
Figure 48. The bode plot with a log scale.

Now we’re going to add the phase function to the same plot.
1. Double click on the plot
2. Check the box to enable the secondary y-axis

Figure 49. Adding a second y-axis.

28
3. Click Ok and the following will appear

Figure 50. The second y-axis on the plot.


4. Set the second y-axis as shown below

Figure 51. Setting the labels for the second y-axis.

The following is the Bode plot

29
Figure 52. Magnitude and phase plot.

If you want, you can add labels to your plot in the same manner that was done in Chapter
4.3.

Close Mathcad.

30
CHAPTER 7

Creating Bode Plots in Matlab

Bode plots are used to plot the magnitude and phase of transfer functions in decibels and
degrees of frequency. The magnitude is also considered the gain of the function, which is
plotted on a linear scale. The frequency is plotted on a log scale.

The following transfer function is going to be plotted

In Mathcad, the bode plot was made in a 2D graph. In Matlab there is a built in function
called bode that will automatically plot the graph.

7.1 Defining Variables


Start Matlab
Define the following variables

Figure 53. Defined variables.

31
7.2 Defining Transfer Functions
In Matlab there is a built-in function that evaluates transfer functions. This built-
in function is tf(X,Y). X and Y are the parameters of the function. X is the
numerator and Y is the denominator.

Define the following in Matlab to evaluate the three transfer functions that will be
plotted

Figure 54. Defining transfer functions.

Each of the values in the array for N, D1, D2, D3 are the coefficients of s from
highest power to lowest.

In this example, N = [K*(1/wo)^2 0 K*1];


K*(1/wo)^2 is the coefficient of the s^2 term
0 is the coefficient of the s term
1*K is the coefficient of the s^0 term.

32
7.3 Creating Bode Plots
Do the following to plot the transfer functions

Figure 55. Using the bode function.


This will appear

Figure 56. The bode plot.

33
7.4 Changing Units
To change the units double click on the plot and the following will appear.

Figure 57. Property editor of the plot.


Then click on the Units tab and select Hz for frequency. The graph will now look
like the following

34
Figure 58. Bode plot with frequency in Hz.

35
CHAPTER 8

Troubleshooting

8.1 Problems With Matrix Multiplication


Problem: You get the following error in Matlab
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Solution: This means that the inner dimensions of the matrices that you’re trying
to multiply don’t match. When you multiply matrices place a period before the
asterisk.

Doing this will give you the mtimes error:


V1 = (I*R1)*(2*z1/sqrt(1-z1^2))*exp(-a1*t)*sin(wd1*t);

To fix it, do this:


V1 = (I*R1)*(2*z1/sqrt(1-z1^2))*exp(-a1*t).*sin(wd1*t);
The period before the asterisk after exp() allows the matrices to be
multiplied without an error

This error may happen when working the problem in Chapter 5.

8.2 Problems With Defined Variables


Problem: You defined a variable in either Matlab or Mathcad and are getting an
error when using it in an equation.
In Matlab the error will be undefined variable.
In Mathcad the variable will turn red and say, “This variable is
undefined.”

Solution: Make sure your variable is defined before you use it. It needs to be
above where you use it. Also make sure that it is the right case. Both Matlab and
Mathcad are case sensitive.

36
8.3 Problems With Units in Mathcad
Problem: You defined variables with units and some without. This then leads to
an error in an equation that uses a combination of those variables.

Solution: When you add units to variables, Mathcad will check to make sure the
units match when you use those variables. Either add units to all of your variables
or remove the units from all of the variables to get rid of the error message.

37
APPENDIX A

Glossary

B
Bode(sys)
Bode is a built in function in Matlab that draws the bode plot of the linear-time-
invariant system sys.

Bode Plot
Consists of a magnitude and phase plot. The magnitude plot is a plot of log
magnitude versus frequency of a complex transfer function. The frequency is a
log scale. The magnitude is measured in decibels (20*log(gain)). The phase plot is
a function of the phase versus frequency, also plotted on a log-frequency axis.
The phase plot is used to show home much the transfer function is shifted.

C
Conductance (G)
It is the inverse of resistance (R).

Current
Flow of electric charge. Symbolized by I or i. Measured in Amps.

D
Defining Variables in Mathcad
To define variables in Mathcad:
Choose the variable (e.g. R)
Enter the variable (e.g. R) + colon or select := on the evaluation toolbar.

38
Enter the value for the variable
Ex. Define R = 100.
Keyboard sequence for this would be R, colon, 100.

Evaluation Toolbar
To display the evaluation toolbar, go to viewToolbarsEvaluation

The evaluation toolbar is a toolbar used to define variables and functions.

G
Greek Toolbar
In Mathcad it is the toolbar with all the greek symbols in lowercase and
uppercase. It looks like the following

39
I
inv()
This Matlab built-in function calculates the inverse of the inputted argument.

L
Linear Time Invariant System
LTI system theory examines the response of a linear, time-invariant system to an
arbitrary input signal. LTI systems can be characterized by the system’s transfer
function. The system’s transfer function is the Laplace transform of the system’s
impulse response.

Linspace
Linearly spaced vector.
LINSPACE(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.

LINSPACE(X1, X2, N) generates N points between X1 and X2.


For N < 2, LINSPACE returns X2.
P
Plot(X,Y)
Built in Matlab function that plots a two-dimensional plot. X is the variable
plotted on the x-axis. Y is the variable plotted on the y-axis.
R
Resistance
Degree to which an object opposes an electric current. Measured in ohms.

S
Sqrt(x)
Built in function in matlab that calculates the square root of x.

T
tf(N,D)
tf(N,D) creates a continuous-time transfer function with numerator N and
denominator D.
N is a vector with the coefficients of s in the numerator from highest to lowest
power.

40
D is a vector with the coefficients of s in the denominator from highest to lowest
power.

title(“x”)
Built in Matlab function that adds a title to a figure.

Transfer Function
A mathematical representation of the relation between the input and output of a
(linear time-invariant) system.

V
Voltage
Difference of electric potential between two points of an electric circuit and is
measured in volts (V).

X
Xlabel()
Built-in Matlab function that labels the x-axis.

Y
Ylabel()
Built-in Matlab function that labels the y-axis.

41

You might also like