3d model of the Hawaiian island chain, rendered in OpenSCAD.

After a lot of hours of experimentation I’ve finally settled on a workable method for generating large-scale 3d terrain.

**Data** from the NGDC’s Grid Extraction tool. The ETOPO1 (bedrock) option gives topography and bathymetry. You can select a rectangle from a map, but it can’t be too big and, which is quite annoying, you can’t cross the antimeridian.

The ETOPO1 data is downloaded as a GeoTIFF, which can be easily converted to a png (I use ImageMagick convert).

The Hawaiian data with the downloaded grayscale.

**Adjusting the color scale**. One interesting property of the data is that it uses a grayscale to represent the elevations that tops out at white at sea-level, then switches to black and starts from there for land (see the above image). While this makes it easy to see the land in the image, it needs to be adjusted to get a good heightmap for the 3d model. So I wrote a python script that uses matplotlib to read in the png image as an array and then I modify the values. I use it to output two images: one of the topography and one of just land and water that I’ll use as a mask later on.

Hawaiian Islands with adjusted topography and ocean-land.

The images I export using matplotlib as grayscale png’s, which can be opened in OpenSCAD using the surface command, and then saved as an stl file. Bigger image files are take longer. A 1000×1000 image will take a few minutes on my computer to save, however the stl file can be imported into 3d software to do with as you will.

Note: H.G. Deitz has a good summary of free tools for Converting Images Into OpenSCAD Models

**Citing this post**: **Urbano**, L., 2017. Generating 3d Terrain, Retrieved December 11th, 2017, from *Montessori Muddle*: http://MontessoriMuddle.org/ .

**Attribution (Curator's Code )**: Via: ᔥ Montessori Muddle; Hat tip: ↬ Montessori Muddle.

Posted in Makerspace, TechShopNo Comments » - Tags: 3d models, programming, python, TechShop

For some of my students with devices like Chromebooks, it has been a little challenging finding ways for them to do coding without a simple, built-in interpreter app. One interim option that I’ve found, and like quite a bit is the TutorialsPoint Coding Ground, which has online interfaces for quite a number of languages that are great for testing small programs, including Python.

Screen capture of Python coding at Tutorial Point’s Coding Ground.

**Citing this post**: **Urbano**, L., 2015. Coding Online on the Coding Ground, Retrieved December 11th, 2017, from *Montessori Muddle*: http://MontessoriMuddle.org/ .

**Attribution (Curator's Code )**: Via: ᔥ Montessori Muddle; Hat tip: ↬ Montessori Muddle.

Posted in ProgrammingNo Comments » - Tags: programming, python

Working on algebra over the summer, we tried a little programming. This is O’s first program, which gives the chirp rate of crickets based on the temperature.

t = float(raw_input('enter temperature> '))
n=4*t-160
print "number of chirps=", n
print "t=", t

**Citing this post**: **Urbano**, L., 2014. A First Program, Retrieved December 11th, 2017, from *Montessori Muddle*: http://MontessoriMuddle.org/ .

**Attribution (Curator's Code )**: Via: ᔥ Montessori Muddle; Hat tip: ↬ Montessori Muddle.

Posted in Mathematics, ProgrammingNo Comments » - Tags: algebra, programming, python

Numerically integrating the area under the curve using four trapezoids.

I gave a quick introduction to programming for my calculus class, which has been working on numerical integration.

Numerical integration is usually used for functions that can’t be integrated (or not easily integrated) but for this example we’ll use a simple parabolic function so we can compare the numerical results to the analytical solution (as seen here).

With the equation:

To find the area under the curve between *x = 1* and *x = 5* we’d find the definite integral:

which gives the result:

For numerical integration, we break the area of concern into a number of trapezoids, find the areas of all the trapezoids and add them up.

We’ll define the left and right boundaries of the area as **a** and **b**, and we can write the integral as:

The left and right boundaries of the area we’re interested in are defined as *a* and *b* respectively. The area of each trapezoid is defined as A_{n}.

We also have to choose a number of trapezoids (**n**) or the width of each trapezoid (**dx**). Here we choose four trapezoids (*n = 4*), which gives a trapezoid width of one (*dx = 1*).

The area of the first trapezoid can be calculated from its width (dx) and the height of the two upper ends of the trapezoid (f(x_{0}) and f(x_{1}).

So if we define the x values of the left and right sides of the first trapezoids as *x*_{0} and *x*_{1}, the area of the first trapezoid is:

For this program, we’ll set the trapezoid width (*dx*) and then calculate the number of trapezoids (*n*) based on the width and the locations of the end boundaries *a* and *b*. So:

and the sum of all the areas will be:

We can also figure out that since the x values change by the same value (dx) for every trapezoid, it’s an arithmetic progression, so:

and,

so our summation becomes:

Which we can program with:

*numerical_integration.py*

# the function to be integrated
def func(x):
return -0.25*x**2 + x + 4
# define variables
a = 1. # left boundary of area
b = 5. # right boundary of area
dx = 1 # width of the trapezoids
# calculate the number of trapezoids
n = int((b - a) / dx)
# define the variable for area
Area = 0
# loop to calculate the area of each trapezoid and sum.
for i in range(1, n+1):
#the x locations of the left and right side of each trapezpoid
x0 = a+(i-1)*dx
x1 = a+i*dx
#the area of each trapezoid
Ai = dx * (func(x0) + func(x1))/ 2.
# cumulatively sum the areas
Area = Area + Ai
#print out the result.
print "Area = ", Area

And the output looks like

>>>
Area = 17.5
>>>

While the programming is pretty straightforward, it was a bit of a pain getting Python to work for one of my students who is running Windows 8. I still have not figured out a way to get it to work properly, so I’m considering trying to do it using Javascript.

# Update

The javascript functions for numerical integration:

function numerically_integrate(a, b, dx, f) {
// calculate the number of trapezoids
n = (b - a) / dx;
// define the variable for area
Area = 0;
//loop to calculate the area of each trapezoid and sum.
for (i = 1; i <= n; i++) {
//the x locations of the left and right side of each trapezpoid
x0 = a + (i-1)*dx;
x1 = a + i*dx;
// the area of each trapezoid
Ai = dx * (f(x0) + f(x1))/ 2.;
// cumulatively sum the areas
Area = Area + Ai
}
return Area;
}
//define function to be integrated
function f(x){
return -0.25*Math.pow(x,2) + x + 4;
}
// define variables
a = 1; // left boundary of area
b = 5; // right boundary of area
dx = 1; // width of the trapezoids
// print out output
alert("Area = "+ numerically_integrate(a, b, dx, f));

This is a demonstration of a full html file that uses the function, and should work in any modern browser (download files: numerical-integration.zip).

# Update 2

I've added the above javascript code to the embeddable graphs to allow it to calculate and display numerical integrals: you can change the values in the interactive graph below.

**Citing this post**: **Urbano**, L., 2013. Programming Numerical Integration with Python (and Javascript), Retrieved December 11th, 2017, from *Montessori Muddle*: http://MontessoriMuddle.org/ .

**Attribution (Curator's Code )**: Via: ᔥ Montessori Muddle; Hat tip: ↬ Montessori Muddle.

Posted in Calculus, Mathematics, ProgrammingNo Comments » - Tags: calculus, embeddable graphs, integration, javascript, math and programming, numerical methods, programming, python