Tutorial 1

         ECE Generic - University of Houston

              Han Q Le (c) -copyrighted 

Intro - Cell and cell types: this is Section

This is subsection

This is subsubsection

This is text. We can use it just like a word processor

Tutorial_1_answer_1.png

Tutorial_1_answer_2.png

The objective of this section is to learn how to create, edit a Mathematica Notebook. If you are familiar with Jupyter notebook, you will see similarity.

0. A very basic note to start

In Mathematica, the most important keyboard operation is:  Tutorial_1_answer_3.gif

This is known as Shift+Enter. Hold Shift key and hit Enter. It ONLY works on an “input cell,” which is highlighted as light yellow in all these lessons.
What does it do? it executes the codes in the selected input cell.

Same as Jupyter, R, etc.
Tutorial_1_answer_4.gif

1. Data and variables - basic

1.1 Data on the internal stack (work space)

Example of data: voltage = 5.2 volt. Here is the representation of the data

In[3]:=

Tutorial_1_answer_5.png

Out[3]=

Tutorial_1_answer_6.png

The data is stored in the “stack”. We access it with this command: %

In[5]:=

Tutorial_1_answer_7.png

Out[5]=

Tutorial_1_answer_8.png

It is more convenient to assign a name to a data so that we can “call” it whenever needed. This “name” represents a “variable,” and this is how a “variable” is created and assigned:

1.2 Assigment of value to a variable

In[6]:=

Tutorial_1_answer_9.png

Out[6]=

Tutorial_1_answer_10.png

We can make a note (comment) of its unit like this:

Tutorial_1_answer_11.png

Out[7]=

Tutorial_1_answer_12.png

Or, perhaps we can define a variable with explicit undefined variable “Volt

In[8]:=

Tutorial_1_answer_13.png

Out[8]=

Tutorial_1_answer_14.png

We can retrieve the value anytime we call it:

In[12]:=

Tutorial_1_answer_15.png

Out[12]=

Tutorial_1_answer_16.png

Consider this statement:

In[28]:=

Tutorial_1_answer_17.png

Why don’t we see any output? It is because of “;”. Semi-colon is a statement terminator: it terminates a statement and suppresses the printout of the output. The output is still on the internal stack. It is simply not printed out on the user interface.

When is it useful? When we don’t want to see irrelevant intermediate calculation results of a program, or when the output is too much to look at. We should use it almost all the time, except for the final output we wish to see.

Exercise 1.1

Define a variable named “current” and assign it the value of 2.5 (unit: mA). Then, show you can retrieve its value.

Answer

END exercise

1.3 Clear or Remove a variable

What if we want to “undo” the assignment of a variable? We can use Clear, ClearAll, Remove, or assignment =.

For the time being, we don’t have to worry the difference between these functions.

In[17]:=

Tutorial_1_answer_18.png

Out[17]=

Tutorial_1_answer_19.png

In[18]:=

Tutorial_1_answer_20.gif

Out[19]=

Tutorial_1_answer_21.png

We can also use this:

In[20]:=

Tutorial_1_answer_22.gif

Out[21]=

Tutorial_1_answer_23.png

Out[23]=

Tutorial_1_answer_24.png

Example of Remove

In[24]:=

Tutorial_1_answer_25.gif

Out[25]=

Tutorial_1_answer_26.png

Out[27]=

Tutorial_1_answer_27.png

Exercise 1.2

Create and assign a value (any value of your choice, e. g. 5 Watt, to variable power, clear the variable power that you define.

Answer

In[1]:=

Tutorial_1_answer_28.png

Out[1]=

Tutorial_1_answer_29.png

In[2]:=

Tutorial_1_answer_30.png

END exercise

2. Basic math operations

2.1 Addition and subtraction

Exercise 2.1

We have the following devices at home, all plugged in the same circuit: 1600-W toaster, 1800-W fryer, and 1500-W water boiler, all are running at their max-rated power, what is the total power consumption?  

Answer

In[1]:=

Tutorial_1_answer_31.png

Out[1]=

Tutorial_1_answer_32.png

Exercise 2.2

Define a variable for each item in exercise 2.1, define variable totalpower as the total power consumption, calculate totalpower.

Answer

In[2]:=

Tutorial_1_answer_33.gif

Out[3]=

Tutorial_1_answer_34.png

END exercise

2.2 Formula definition

We are familiar with spreadsheet operation like the below

spreadsheet example

In[1]:=

Tutorial_1_answer_35.gif

Out[1]=

What we have in cell C1 is a formula. It is convenient to do the same in Mathematica: defining a variable using a formula like this:

In[1]:=

Tutorial_1_answer_37.gif

Out[2]=

Tutorial_1_answer_38.png

Tutorial_1_answer_39.png

If we query totalpower, the result is not a number but a formula

In[3]:=

Tutorial_1_answer_40.png

Out[3]=

Tutorial_1_answer_41.png

Tutorial_1_answer_42.png

Like spreadsheet, it will execute the formula when we assign values to the variables:

In[4]:=

Tutorial_1_answer_43.gif

Out[5]=

Tutorial_1_answer_44.png

Like spreadsheet, it will update whenever new values are assigned to the variables:

In[6]:=

Tutorial_1_answer_45.png

Out[7]=

Tutorial_1_answer_46.png

Exercise 2.3

Used a defined formula, calculate the total power for 1250-W toaster, 1450-W fryer, and 1375 W boiler.

Answer

In[8]:=

Tutorial_1_answer_47.gif

Out[11]=

Tutorial_1_answer_48.png

END exercise

2.3 Multiplication and symbolic algebraic manipulation

Given the voltage and current in section 1 above, which are associated with a circuit element, can we find its power?
power = voltage x current, or: P= V I.

In[14]:=

Tutorial_1_answer_49.gif

Out[14]=

Tutorial_1_answer_50.png

Out[15]=

Tutorial_1_answer_51.png

Out[16]=

Tutorial_1_answer_52.png

Exercise 2.4

Define a formula to calculate the power of a device based on its current and voltage, then illustrate with an example.

Answer

Tutorial_1_answer_53.gif

Out[11]=

Tutorial_1_answer_54.png

Exercise 2.5

Calculate the total power of 12 identical devices with current and voltage above: a) when they are in series; b) when they are parallel (a portion of this question requires knowledge of circuit theory and if you don’t know, just raise your hand and ask the instructor, hint: if each device is run at max power, does it matter if they are parallel or serial?)

Answer

In[13]:=

Tutorial_1_answer_55.gif

Out[17]=

Tutorial_1_answer_56.png

Exercise 2.6

Find the product of (Cos[φ]-Sin[φ]) and (Cos[φ]+Sin[φ]). Let φ be unassigned.

Answer

In[52]:=

Tutorial_1_answer_57.gif

Out[53]=

Tutorial_1_answer_58.png

We don’t see anything different because it cannot be computed numerically. But we can use function Expand[] to do symbolic manipulation:

In[1]:=

Tutorial_1_answer_59.gif

Out[2]=

Tutorial_1_answer_60.png

We can also use function TrigReduce[]:

In[4]:=

Tutorial_1_answer_61.gif

Out[5]=

Tutorial_1_answer_62.png

END exercise

2.4 Using non-numerical or unassigned variables with ReplaceAll

In the above, we obtain answers with unit mA Volt. But we want to transform the unit to mW. How do we do it?

In[18]:=

Tutorial_1_answer_63.gif

Out[22]=

Tutorial_1_answer_64.png

In the above, mA and Volt are variables without any numerical assignment. We do so deliberately because we want to treat them as units.

This is how to can transform variables:

In[24]:=

Tutorial_1_answer_65.png

Out[24]=

Tutorial_1_answer_66.png

/. is known as ReplaceAll: it is a function allowing us to replace some symbol (unassigned variable) with something. Consider this example:

In[23]:=

Tutorial_1_answer_67.gif

Out[24]=

Tutorial_1_answer_68.png

However, if we already assign a numerical value of b:

In[25]:=

Tutorial_1_answer_69.gif

Out[26]=

Tutorial_1_answer_70.png

The replacement doesn’t work above because b is numerical. If it is something else, it will work:

In[29]:=

Tutorial_1_answer_71.gif

Out[30]=

Tutorial_1_answer_72.png

If a or q or both are numerical:

In[31]:=

Tutorial_1_answer_73.gif

Out[32]=

Tutorial_1_answer_74.png

Exercise 2.7

Calculate the power of an LED in unit of mW with the following voltages and currents:
- 3.6 V and 25 mA
- 2.5 V and 30 mA
- 1.9 V and 42 mA
using formula and substitution method (/.)

Answer

In[4]:=

Tutorial_1_answer_75.gif

Out[5]=

Tutorial_1_answer_76.png

Out[6]=

Tutorial_1_answer_77.png

Out[7]=

Tutorial_1_answer_78.png

END exercise

2.5 Power

Similar to most languages, power operation is done with ^

In[1]:=

Tutorial_1_answer_79.gif

Out[4]=

Tutorial_1_answer_80.png

convert Tutorial_1_answer_81.png into μW

In[5]:=

Tutorial_1_answer_82.png

Out[5]=

Tutorial_1_answer_83.png

convert Tutorial_1_answer_84.png into Tutorial_1_answer_85.pngW

In[6]:=

Tutorial_1_answer_86.png

Out[6]=

Tutorial_1_answer_87.png

If we want to see decimal instead of whole number fraction

In[7]:=

Tutorial_1_answer_88.png

Out[7]=

Tutorial_1_answer_89.png

This is a feature of Mathematica: by default, if we assign a value without decimal point, it is a whole number (integral or rational). With the decimal point, it is a real number with “floating point”. What is a “floating point” number? we will learn about this later. For now, just think of it as a real number.

Exercise 2.8

Let a and b be two undefined variables, use Expand[] to find Tutorial_1_answer_90.png for n=2,3,4

Answer

In[6]:=

Tutorial_1_answer_91.gif

Out[7]=

Tutorial_1_answer_92.png

Out[8]=

Tutorial_1_answer_93.png

Out[9]=

Tutorial_1_answer_94.png

Exercise 2.9

Let a and b be two undefined variables, define Tutorial_1_answer_95.png; use Expand[] to find q for n=2,3,4 with replacement method for n.

Answer

In[16]:=

Tutorial_1_answer_96.gif

Out[18]=

Tutorial_1_answer_97.png

In[19]:=

Tutorial_1_answer_98.png

Out[19]=

Tutorial_1_answer_99.png

In[20]:=

Tutorial_1_answer_100.png

Out[20]=

Tutorial_1_answer_101.png

END exercise

2.6 Division

If the voltage and current across a resistor are 5.2 Volt and 2.5 mA, what is the resistor value?

In[15]:=

Tutorial_1_answer_102.gif

Out[16]=

Tutorial_1_answer_103.png

Out[17]=

Tutorial_1_answer_104.png

Out[18]=

Tutorial_1_answer_105.png

Out[19]=

Tutorial_1_answer_106.png

Exercise 2.10

Convert Volt/mA to kΩ for the above example

Answer

In[25]:=

Tutorial_1_answer_107.gif

Out[28]=

Tutorial_1_answer_108.png

In[29]:=

Tutorial_1_answer_109.png

Out[29]=

Tutorial_1_answer_110.png

Exercise 2.11

What is the rms current for the circuit with 3 appliances in the question above: 1600-W toaster, 1800-W fryer, and 1500-W water boiler, assuming that the voltage is 120-V AC. (irms=P/V)

Answer

In[8]:=

Tutorial_1_answer_111.gif

Out[12]=

Tutorial_1_answer_112.png

Would this break the circuit? Yes, most kitchen circuit is limited to 25 A, some may have 40 A, and these three devices will break

Problem 2.1a

One of the most recent important unit definition is the kilogram. Previously, the kilogram is defined as the mass of this:

Tutorial_1_answer_113.gif

Mass is a most important physical entity that is essential to the definition of energy:

Tutorial_1_answer_114.gif

However, since 1899 (or 1900, depending on historical interpretation), Planck discovered a fundamental constant of nature: h, found to be: Tutorial_1_answer_115.png with a relative uncertainty of Tutorial_1_answer_116.png.
However, a fundamental constant of nature with physical unit should not have any uncertainty, because the unit is a man-made arbitrary choice, like the platinum block above. Hence, on November 16, 2018, the international community agreed to define exactly:

In[30]:=

Tutorial_1_answer_117.png

Out[30]=

Tutorial_1_answer_118.png

Based on this, write a formula to define the kg such that PlanckConstant is exactly Tutorial_1_answer_119.png

Answer

We will use Solve[] for this question

In[32]:=

Tutorial_1_answer_120.gif

Out[33]=

Tutorial_1_answer_121.png

But there is a problem with the above. We have a better solution this way, using whole number:

In[56]:=

Tutorial_1_answer_122.gif

Out[57]=

Tutorial_1_answer_123.png

Problem 2.1b

What is a meter? A meter is defined to be

Tutorial_1_answer_124.png

Find the kg definition in terms of the SpeedOfLight and PlanckConstant

Answer

In[1]:=

Tutorial_1_answer_125.png

Out[1]=

Tutorial_1_answer_126.png

In[2]:=

Tutorial_1_answer_127.png

Out[2]=

Tutorial_1_answer_128.png

END exercise

3. Basic functions

Here, we will study three most important basic functions: Exp, Log, and Sinusoidal

3.1 Exponential

In[13]:=

Tutorial_1_answer_129.png

Out[13]=

Tutorial_1_answer_130.png

In[14]:=

Tutorial_1_answer_131.png

Out[14]=

Tutorial_1_answer_132.png

In[15]:=

Tutorial_1_answer_133.png

Out[15]=

Tutorial_1_answer_134.png

In[17]:=

Tutorial_1_answer_135.png

Out[17]=

In[148]:=

Tutorial_1_answer_137.png

Out[148]=

Tutorial_1_answer_138.gif

In[144]:=

Tutorial_1_answer_139.png

Out[144]=

Tutorial_1_answer_140.png

In[145]:=

Tutorial_1_answer_141.png

Out[145]=

Tutorial_1_answer_142.png

In[146]:=

Tutorial_1_answer_143.png

Tutorial_1_answer_144.png

Out[146]=

Tutorial_1_answer_145.png

Exercise 3.1

Define variables:  Tutorial_1_answer_146.png for a=153222455., Tutorial_1_answer_147.png for  b= 153222417, and pab=xa*xb.
Find xa, xb, and pab

Answer

In[161]:=

Tutorial_1_answer_148.gif

Out[162]=

Tutorial_1_answer_149.png

In[163]:=

Tutorial_1_answer_150.gif

Tutorial_1_answer_151.png

Out[164]=

Tutorial_1_answer_152.png

In[165]:=

Tutorial_1_answer_153.png

Out[165]=

Tutorial_1_answer_154.png

However, if we do this:

In[166]:=

Tutorial_1_answer_155.gif

Out[167]=

Tutorial_1_answer_156.png

We obtain a very different value for pab. Why?

Exercise 3.2

A 500-μF capacitor is fully charged with 10 V. At time t=0, it is allowed to suddenly discharge into a 2-kΩ resistor. What is the voltage and current at time t=1.5 sec?

Out[111]=

Tutorial_1_answer_157.gif

Answer

From KCL: Tutorial_1_answer_158.png
       Or:      Tutorial_1_answer_159.png

In[123]:=

Tutorial_1_answer_160.png

Out[123]=

Tutorial_1_answer_161.png

Thus, voltage is:   Tutorial_1_answer_162.png
where Tutorial_1_answer_163.png is the initial voltage at t=0.
The current is:  i=-q'[t]

In[127]:=

Tutorial_1_answer_164.png

Out[128]=

Tutorial_1_answer_165.png

Thus:  Tutorial_1_answer_166.png

Now, we can substitute:

In[134]:=

Tutorial_1_answer_167.png

Out[134]=

Tutorial_1_answer_168.png

In[131]:=

Tutorial_1_answer_169.png

Out[131]=

Tutorial_1_answer_170.png

END exercise

In[135]:=

Tutorial_1_answer_171.png

Out[135]=

Tutorial_1_answer_172.gif

In[136]:=

Tutorial_1_answer_173.png

Out[136]=

Tutorial_1_answer_174.gif

Exercise 3.3

Find the exponential of Tutorial_1_answer_175.png for x=-2.5, using this code

Tutorial_1_answer_176.png

Answer

In[168]:=

Tutorial_1_answer_177.png

Out[168]=

END exercise

3.2 Log

In[137]:=

Tutorial_1_answer_179.png

Out[137]=

Tutorial_1_answer_180.png

In[138]:=

Tutorial_1_answer_181.png

Out[138]=

Tutorial_1_answer_182.png

In[139]:=

Tutorial_1_answer_183.png

Out[139]=

Tutorial_1_answer_184.png

In[140]:=

Tutorial_1_answer_185.png

Out[140]=

Tutorial_1_answer_186.png

In[141]:=

Tutorial_1_answer_187.png

Out[141]=

Tutorial_1_answer_188.png

In[142]:=

Tutorial_1_answer_189.png

Out[142]=

Tutorial_1_answer_190.png

In[143]:=

Tutorial_1_answer_191.png

Out[143]=

Tutorial_1_answer_192.png

Exercise 3.4

Find the Log of x for x=-2.5, using this code

In[169]:=

Tutorial_1_answer_193.png

Answer

In[170]:=

Tutorial_1_answer_194.png

Out[170]=

Exercise 3.5

For circuit in Exercise 3.2, how long does it take from the moment of discharge to the time when the capacitor voltage is 2 V?

Answer

From the above, the voltage is:   Tutorial_1_answer_196.png
then:  Tutorial_1_answer_197.png
And;  Tutorial_1_answer_198.png
or:    Tutorial_1_answer_199.png

In[171]:=

Tutorial_1_answer_200.png

Out[171]=

Tutorial_1_answer_201.png

It take 1.61 sec.

END exercise

In[174]:=

Tutorial_1_answer_202.png

Out[174]=

Tutorial_1_answer_203.gif

Tutorial_1_answer_204.png

3.3 Sinusoidal

Out[190]=

In[175]:=

Tutorial_1_answer_206.png

Out[175]=

Tutorial_1_answer_207.png

In[176]:=

Tutorial_1_answer_208.png

Out[176]=

Tutorial_1_answer_209.png

Exercise 3.6

Consider an AC voltage: Tutorial_1_answer_210.png
where v0 is 120 V, f=60 Hz. What is the voltage value at t=13.5 ms.

Answer

In[187]:=

Tutorial_1_answer_211.gif

Out[188]=

Tutorial_1_answer_212.png

In[189]:=

Tutorial_1_answer_213.png

Out[189]=

Tutorial_1_answer_214.gif

Exercise 3.7

Consider an EM wave Tutorial_1_answer_215.png
where f=2450 MHz; Tutorial_1_answer_216.png. What is the E field at z=0 for t=1, 2, 3, 4 sec

Answer

In[3]:=

Tutorial_1_answer_217.gif

Out[4]=

Tutorial_1_answer_218.png

Out[5]=

Tutorial_1_answer_219.png

Out[6]=

Tutorial_1_answer_220.png

Out[7]=

Tutorial_1_answer_221.png

In[8]:=

Tutorial_1_answer_222.gif

Out[9]=

Tutorial_1_answer_223.png

Out[10]=

Tutorial_1_answer_224.png

Out[11]=

Tutorial_1_answer_225.png

Out[12]=

Tutorial_1_answer_226.png

END exercise

What is the difference between the two approaches of calculation? Why the resuts are different?




Created with the Wolfram Language