General

For Teachers

Lesson 1: Introduction to Coding in Tychos

Lesson Overview and Objectives

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

- Students will learn how to navigate the Tychos interface.
- Students will learn how to create, use and modify comments, variables and built in functions.
- Students will learn about matrices and how to perform basic calculations (add, subtract) with matrices.

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.

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.

The simulation tool that you will be using throughout this course will help you test your understanding of the real world, and will even lead to discoveries about how the real world works.

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.

Tychos â€“ Hackable Simulations

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:1

# This is a comment.

Copied!

The second line of code will actually break the simulation engine. The software won't understand that second line of code, generating an error message. We will talk in more detail about how to interpret these errors later. For right now, just know that you can't communicate with the software using normal human speech - you are restricted to using the rules of language that the tool uses.

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:

1

x = 1

Copied!

This defines a variable named

`x`

that initially has a value of `1`

. You can define more than one variable as well:1

# This is one variable

2

x = 1

3

â€‹

4

# This is another variable

5

y = 1

Copied!

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 (/):

1

# Basic math operations

2

1+1

3

2-5

4

3*2.5

5

5/2

Copied!

These calculations get evaluated in the pane, and the resulting value is displayed on the right hand side of the pane in blue.

You can also use variables in your calculations like this:

1

# Use a variable instead

2

x = 2

3

x+2

4

x-2

5

x*3

6

x/3

Copied!

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

1

# Use two variables instead

2

x = 2

3

y = 3

4

x+y

5

x-y

6

x*y

7

x/y

8

x%y

9

# Wonder what that last line does?

Copied!

Using Special 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, and we will learn more of these in the future:

1

# This function computes the result of a base value raised to a power

2

pow(2,3)

3

â€‹

4

# This function computes a square root

5

sqrt(16)

Copied!

1

# This function converts a degree value to a radian value

2

deg_to_rad(180)

3

â€‹

4

# This function computes the sine of an angle in radians

5

sin(.57)

Copied!

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, and we will cover many of these this year. For a listing of others, you can check out the Tychos Language Referenceâ€‹

Defining and Using Matrices

Being that we need to simulate 2 dimensional motion, then we need a way to represent two dimensional quantities in our simulated universe. Tychos allows you to represent two dimensional quantities by using a matrix form for vectors.

Defining A Matrix

For now let's just see how you define a matrix:

1

# This is a two number (or element) matrix

2

m1 = [0,0]

Copied!

As you can see above, you define a matrix by simply identifying the set of numbers inside two brackets "[" and "]" and then separate the numbers with a comma.

Matrix Calculations

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

1

# This is a two number (or element) matrix

2

m1 = [0,0]

3

# These are some simple calculations with a matrix

4

m1+2

5

m1-2

6

m1*2

7

m1/2

Copied!

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

1

# Here are two matrices

2

m1 = [1,2]

3

m2 = [2,1]

4

â€‹

5

# Add and subtract matrices

6

m1+m2

7

m1-m2

Copied!

Be Aware Of Some Matrix Oddities...

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

1

# Here are two matrices

2

m1 = [1,2]

3

m2 = [2,1]

4

â€‹

5

# Try this for some strange results...

6

m1*m2

Copied!

Matrix multiplication is an advanced topic that we will cover soon. There are special matrix multiplication functions that we will discuss later.

Accessing An Individual Matrix Element

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

1

m1 = [0,0]

2

m1[1] = 2

Copied!

Try this and look at the value of the matrix identified by the blue text 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:

Could not load image

LaTeX: \vec{s}\:=\:\left(40\:m,\:120^\circ\right)

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 matrices, each element in a 2D matrix 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 matrix 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:

1

# X component:

2

x = 40*cos(deg_to_rad(120))

3

â€‹

4

# Y component:

5

y = 40*sin(deg_to_rad(120))

Copied!

The function

*deg_to_rad*

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 matrix 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:

1

vec = [10, 5]

2

â€‹

3

# Find the length

4

m = mag(vec)

5

â€‹

6

# Find the heading

7

angle = atan(5/10)

Copied!

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

*atan*

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

1

# Input the length and angle

2

vec = polar(10, 45)

Copied!

â€‹

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 matrices and perform basic arithmetic calculations with those matrices.

Last modified 1yr ago