Python Tutorial 1: Introduction to Coding in Tychos

Lesson Overview and Objectives

This lesson is a great place to start, and will help you, or your students get acquainted with Tychos quickly.

  • Learn how to navigate the Tychos interface.

  • Learn how to create, use and modify comments, variables and built in functions.

  • Learn about vectors and how to perform basic calculations (add, subtract) with vectors.

Simulation is the imitation of the operation of a real-world process or system over time.[1] The act of simulating something first requires that a model be developed; this model represents the key characteristics or behaviors/functions of the selected physical or abstract system or process. The model represents the system itself, whereas the simulation represents the operation of the system over time.

-Wikipedia

Scientists and engineers create computer simulations that allow them to create virtual universes so that they can better understand the real universe. If the virtual universe behaves similarly to the way nature behaves, then scientists can assume that their simulation is built on similar rules - what we call a model.

Coding Your Simulations

In order to build a simulation, you will first need to learn how to tell the underlying interpreter what you want your simulation to do. This requires learning the programming interface of the tool. A programming interface is essentially the language that the tool understands. Although this is not a computer programming course, you will be learning how to write some "code" that can be understood by the software that underlies the entire tool. When you write this code, you will be essentially writing a program that is executed by the simulation tool.

We are going to start slowly by covering some programming terminology that you will need to be familiar with to start creating simulations. Follow this guide below to help you familiarize yourself with some of the basic commands and syntax of the tool's programming interface.

The Tychos Environment

Click on the link below to open the Tychos scenario in a new browser tab where you can follow along.

Introduction to Tychos: Variables and Calculations

Code Syntax

To get started writing some code, you are going to need to go to the Initial State pane.

Computers speak in languages that are very specific and precise. In order to communicate with a computer, we need to follow very specific rules.

Writing Comments

Comments are really statements that are ignored by the computer. That might seem strange. Why would you use them? Well, this can be a handy way to put notes that can help describe what your code is doing because (as you will see) sometimes it is not obvious what a line of code is doing. Comments are also used to help others read your code. To identify a comment, you simply place a # character in front of the line:

# This is a comment.

Creating Variables

Just like in your Algebra class, the simulation tool can understand and use variables that are symbols whose value can change. To define a variable and give that variable a value you simply can type:

x = 1

This defines a variable named x that initially has a value of 1. You can define more than one variable as well:

# This is one variable
x = 1

# This is another variable
y = 1

The variables above have the same value, but they can have different values.

Using Simple Mathematical Operations

You can type simple calculations like the ones below, using the standard symbols for addition (+), subtraction (-), multiplication (*) and division (/):

# Basic math operations
a = 1+1
b = 2-5
c = 3*2.5
d = 5/2

Displaying output in the Console

These calculations get evaluated in the pane when the refresh button is clicked (look at the bottom of the screen on the right), but the result is not displayed anywhere, unless you specifically ask for the result to be printed to the Console.

To display the value of a variable or the result of a calculation, you simply use what is called the print command, like this:

a = 2 + 2
print(a)

If you then press the refresh button and then go to the Console, you will see that the number 4 is displayed:

The Console is actually a helpful tool for executing single lines of code which can be helpful in both debugging your code, as well as testing code quickly. We will return to it often when it can be helpful.

You can also use variables in your calculations like this:

# Use a variable instead
x = 2
print(x+2)
print(x-2)
print(x*3)
print(x/3)

And you can even use different variables in the same calculations:

# Use two variables instead
x = 2
y = 3
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
# Wonder what that last line does?

Using Built-in Functions

There are some functions that are defined by the programming language that you can also use to do some more complex calculations. Here are just a couple:

# This function computes the result of a base value raised to a power
x = pow(2,3)
print(x)

# This function computes a square root
y = sqrt(16)
print(y)
# This function converts a degree value to a radian value
x = radians(180)
print(x)

# This function computes the sine of an angle in radians
y = sin(.57)
print(y)

Notice in these lines of code that the function is defined by a name - like pow and sqrt and then you type an open parentheses "(" and then the input values for the function, and then a closed parentheses ")". The first function pow takes two inputs, the base and then the power. These are separated by a comma to identify the two inputs.

There are other functions that can be used to perform various calculations. For a listing of others, you can check out the Tychos Language Reference

Defining and Using Vectors

All of the above mathematical calculations we have performed are one dimensional or scalar calculations. Tychos also allows you to represent and calculate two dimensional quantities by using something called a vector.

Defining A Vector

For now let's just see how you define a vector using the built-in vec function:

# This is a two number (or element) vector
v1 = vec(0,0)

As you can see above, you define a vector by simply identifying the two values for each dimensions separated with a comma.

Vector Calculations

The software allows you to perform various vector calculations, very similar to the above calculations, but there are some important differences. Here are some ways in which you can perform calculations with vectors:

Adding, Subtracting, Multiplying and Dividing A Vector and A Scalar:

# This is a two number (or dimensional) vector
v1 = vec(0,0)
# These are some simple calculations with a vector
print(v1 + 2)
print(v1 - 2)
print(v1 * 2)
print(v1 / 2)

Can you see the pattern in the results? Notice how the calculation is performed on each number in the vector. Now this gets really interesting when we start adding a vector to another vector.

Vector Addition and Subtraction:

To add or subtract vectors, you can simply use the + operator or the - operator, like this:

# Here are two matrices
v1 = vec(1,2)
v2 = vec(2,1)

# Add and subtract matrices
print(v1 + v2)
print(v1 - v2)

When you check the Console you should see that the result is a new vector whose x and y components have been either added, or likewise subtracted from each of the vectors.

Be Aware Of Some Vector Oddities...

Now, not all calculations work like this due to the underlying mathematical principles that define vectors. We won't go over these just yet, but it might interesting for you to try the following:

# Here are two matrices
v1 = vec(1,2)
v2 = vec(2,1)

# Try this for some strange results...
print(v1 * v2)

Vector multiplication is an advanced topic. You should quickly see that multiplication and division don't quite make sense. Vector multiplication is beyond the scope pf this introductory tutorial.

Accessing An Individual Vector Element

You can change a single value inside a vector by accessing just that element. This is done by referencing the component or position in the vector. So for example, to change the first component in the vector, you simply type:

v1 = vec(0,0)
v1.x = 2
print(v1)

Try this and look at the value of the vector printed to the Console and you will notice that it has indeed changed. Try changing the second element instead.

Working with Vectors in Polar Form

Often times, in physics and engineering, vector quantities can be represented in polar form like this:

This would be a position vector that represents a distance of 40 meters pointing at a direction of 120 degrees.

There are two problems with vectors in this form. The first is that Tychos represents vectors as components, each element in a 2D vector being equivalent to the X and Y components of the 2D vector. The second is that Tychos by default represents angles in units of radians, not degrees.

It might be useful to know how to convert a vector given to you in the polar form above to a component form in Tychos. You can do that using some of the built in functions that are available.

To find the components, use these two lines of code:

# X component:
x = 40 * cos(radians(120))

# Y component:
y = 40 * sin(radians(120))

# The vector in component form
print(vec(x,y))

The function radians converts the angle in degrees to radians. The sin and cos functions are the trig functions from math to give us the lengths of the "legs" of the triangle where 40 is the length of the hypotenuse.

You can also go the other way if you wish. Let's say that you have a vector in component form and for some reason you would like to see the length and heading of that vector. You can use some other built in functions to assist:

v1 = vec(10, 5)

# Find the length
m = mag(vec)

# Find the heading
angle = atan(5/10)

Above we used the function mag that gives us the length of a vector and we used the atan function which is the arc tangent function from trigonometry.

Tychos also provides a helper function for converting polar form vectors to component vectors:

# Input the length and angle
vec = polar(10, 45)

Conclusion

You should now be able to:

  • Write comments into your code.

  • Create variables and give them values.

  • Perform basic calculations with variables and values using operators and functions.

  • Create 2D vectors and perform basic arithmetic calculations with those vectors.

Last updated