Tutorial 1
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
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:
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.
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]:=
Out[3]=
The data is stored in the “stack”. We access it with this command: %
In[5]:=
Out[5]=
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]:=
Out[6]=
We can make a note (comment) of its unit like this:
Out[7]=
Or, perhaps we can define a variable with explicit undefined variable “Volt”
In[8]:=
Out[8]=
We can retrieve the value anytime we call it:
In[12]:=
Out[12]=
Consider this statement:
In[28]:=
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]:=
Out[17]=
In[18]:=
Out[19]=
We can also use this:
In[20]:=
Out[21]=
Out[23]=
Example of Remove
In[24]:=
Out[25]=
Out[27]=
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]:=
Out[1]=
In[2]:=
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]:=
Out[1]=
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]:=
Out[3]=
END exercise
2.2 Formula definition
We are familiar with spreadsheet operation like the below
spreadsheet example
In[1]:=
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]:=
Out[2]=
If we query totalpower, the result is not a number but a formula
In[3]:=
Out[3]=
Like spreadsheet, it will execute the formula when we assign values to the variables:
In[4]:=
Out[5]=
Like spreadsheet, it will update whenever new values are assigned to the variables:
In[6]:=
Out[7]=
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]:=
Out[11]=
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]:=
Out[14]=
Out[15]=
Out[16]=
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
Out[11]=
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]:=
Out[17]=
Exercise 2.6
Find the product of (Cos[φ]-Sin[φ]) and (Cos[φ]+Sin[φ]). Let φ be unassigned.
Answer
In[52]:=
Out[53]=
We don’t see anything different because it cannot be computed numerically. But we can use function Expand[] to do symbolic manipulation:
In[1]:=
Out[2]=
We can also use function TrigReduce[]:
In[4]:=
Out[5]=
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]:=
Out[22]=
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]:=
Out[24]=
/. is known as ReplaceAll: it is a function allowing us to replace some symbol (unassigned variable) with something. Consider this example:
In[23]:=
Out[24]=
However, if we already assign a numerical value of b:
In[25]:=
Out[26]=
The replacement doesn’t work above because b is numerical. If it is something else, it will work:
In[29]:=
Out[30]=
If a or q or both are numerical:
In[31]:=
Out[32]=
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]:=
Out[5]=
Out[6]=
Out[7]=
END exercise
2.5 Power
Similar to most languages, power operation is done with ^
In[1]:=
Out[4]=
convert into μW
In[5]:=
Out[5]=
convert into W
In[6]:=
Out[6]=
If we want to see decimal instead of whole number fraction
In[7]:=
Out[7]=
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 for n=2,3,4
Answer
In[6]:=
Out[7]=
Out[8]=
Out[9]=
Exercise 2.9
Let a and b be two undefined variables, define ; use Expand[] to find q for n=2,3,4 with replacement method for n.
Answer
In[16]:=
Out[18]=
In[19]:=
Out[19]=
In[20]:=
Out[20]=
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]:=
Out[16]=
Out[17]=
Out[18]=
Out[19]=
Exercise 2.10
Convert Volt/mA to kΩ for the above example
Answer
In[25]:=
Out[28]=
In[29]:=
Out[29]=
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]:=
Out[12]=
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:
Mass is a most important physical entity that is essential to the definition of energy:
However, since 1899 (or 1900, depending on
historical interpretation), Planck discovered a fundamental
constant of nature: h,
found to be:
with a relative uncertainty of .
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]:=
Out[30]=
Based on this, write a formula to define the kg such that PlanckConstant is exactly
Answer
We will use Solve[] for this question
In[32]:=
Out[33]=
But there is a problem with the above. We have a better solution this way, using whole number:
In[56]:=
Out[57]=
Problem 2.1b
What is a meter? A meter is defined to be
Find the kg definition in terms of the SpeedOfLight and PlanckConstant
Answer
In[1]:=
Out[1]=
In[2]:=
Out[2]=
END exercise
3. Basic functions
Here, we will study three most important basic functions: Exp, Log, and Sinusoidal
3.1 Exponential
In[13]:=
Out[13]=
In[14]:=
Out[14]=
In[15]:=
Out[15]=
In[17]:=
Out[17]=
In[148]:=
Out[148]=
In[144]:=
Out[144]=
In[145]:=
Out[145]=
In[146]:=
Out[146]=
Exercise 3.1
Define variables:
for a=153222455.,
for b= 153222417, and pab=xa*xb.
Find xa, xb, and pab
Answer
In[161]:=
Out[162]=
In[163]:=
Out[164]=
In[165]:=
Out[165]=
However, if we do this:
In[166]:=
Out[167]=
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]=
Answer
From KCL:
Or:
In[123]:=
Out[123]=
Thus, voltage is:
where
is the initial voltage at t=0.
The current is: i=-q'[t]
In[127]:=
Out[128]=
Thus:
Now, we can substitute:
In[134]:=
Out[134]=
In[131]:=
Out[131]=
END exercise
In[135]:=
Out[135]=
In[136]:=
Out[136]=
Exercise 3.3
Find the exponential of for x=-2.5, using this code
Answer
In[168]:=
Out[168]=
END exercise
3.2 Log
In[137]:=
Out[137]=
In[138]:=
Out[138]=
In[139]:=
Out[139]=
In[140]:=
Out[140]=
In[141]:=
Out[141]=
In[142]:=
Out[142]=
In[143]:=
Out[143]=
Exercise 3.4
Find the Log of x for x=-2.5, using this code
In[169]:=
Answer
In[170]:=
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:
then:
And;
or:
In[171]:=
Out[171]=
It take 1.61 sec.
END exercise
In[174]:=
Out[174]=
3.3 Sinusoidal
Out[190]=
In[175]:=
Out[175]=
In[176]:=
Out[176]=
Exercise 3.6
Consider an AC voltage:
where v0 is 120 V, f=60 Hz. What is the voltage value at t=13.5
ms.
Answer
In[187]:=
Out[188]=
In[189]:=
Out[189]=
Exercise 3.7
Consider an EM wave
where f=2450 MHz; .
What is the E field at z=0 for t=1, 2, 3, 4 sec
Answer
In[3]:=
Out[4]=
Out[5]=
Out[6]=
Out[7]=
In[8]:=
Out[9]=
Out[10]=
Out[11]=
Out[12]=
END exercise
What is the difference between the two approaches of calculation? Why the resuts are different?