1 a core course on modeling examples street lanterns planets and gravity week 4 – the function of...

47
1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

Upload: susan-bell

Post on 20-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

1

A Core Course on Modeling

• Examples

• street lanterns

• planets and gravity

Week 4 – The Function of Functions

Page 2: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

2

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

image: http://www.westberks.gov.uk/index.aspx?articleid=3785

Page 3: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

3

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

Purpose:

• how much energy is needed to illuminate road

• intensity everywhere is between acceptable borders

Interesting quantities:

• totPower

• maxInt

• minInt

Page 4: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

4

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

Purpose:

• how much energy is needed to illuminate road

• intensity everywhere is between acceptable borders

Interesting quantities:

• totPower

• maxInt

• minInt

simple: totPower = nLanterns * p

not so simple …

Page 5: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

5

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

ji

(i,j)

dL

h roadLengthroadWidth

lW

Page 6: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

6

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

ji

dL

h

roadWidth

lW

lfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

roadLength

Page 7: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

7

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,slices , maxIntensityInSlice(i), max)

maxIntensityInSlice(i)=#(j,cellsInSlice(i),cellInt(i,j),max)

cellInt(i,j)=lfG(i,j)

=lfG[i][j]

Page 8: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

8

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,slices ,#(j,cellsInSlice(i),cellInt(i,j) ,max),max)

maxIntensityInSlice(i)=#(j,cellsInSlice(i),cellInt(i,j),max)

cellInt(i,j)=lfG(i,j)

=lfG[i][j]

Page 9: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

9

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,slices ,#(j,cellsInSlice(i),lfG[i][j], max),max)

maxIntensityInSlice(i)=#(j,cellsInSlice(i),cellInt(i,j),max)

cellInt(i,j)=lfG(i,j)

=lfG[i][j]

Page 10: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

10

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,slices ,#(j,cellsInSlice(i),lfG[i][j], max),max)

slices: lfG consists of slices;

vDom('domain') gives the sequence of indices of an aggregation

e.g., vDom(['a','b','c','d'])=[0,1,2,3], vDom(['x':17,'y':1])=['x','y']

so: slices = vDom(lfG), and cellsInSlice(i)=vDom(lfG[i])

Page 11: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

11

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,vDom(lfG),#(j,cellsInSlice(i),lfG[i][j], max),max)

slices: lfG consists of slices;

vDom('domain') gives the sequence of indices of an aggregation

e.g., vDom(['a','b','c','d'])=[0,1,2,3], vDom(['x':17,'y':1])=['x','y']

so: slices = vDom(lfG), and cellsInSlice(i)=vDom(lfG[i])

Page 12: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

12

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

maxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]), lfG[i][j], max),max)

slices: lfG consists of slices;

vDom('domain') gives the sequence of indices of an aggregation

e.g., vDom(['a','b','c','d'])=[0,1,2,3], vDom(['x':17,'y':1])=['x','y']

so: slices = vDom(lfG) and cellsInSlice(i)=vDom(lfG[i])

Page 13: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

13

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanterns

maxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max)

minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)

To be done:

•definition of lfG

•definition of p

•definition of nLanterns

Page 14: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

lfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

Defining lfG :

lfG=#(i,slices,lumSlice(i),vAppend)

E.g., vAppend([10,20,30],40)=[10,20,30,40]

E.g., vAppend(vAppend([],[40,50]),[60,70])=[[40,50],[60,70]]

so: lfG=[lumSlice(0),lumSlice(1),lumSlice(2),…]

14

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

ji

lumSlice(i) is a function that produces the i-th luminated slice

Page 15: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

15

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanterns

maxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max)

minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)

lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)

To be done:

•definition of p

•definition of nLanterns

•definition of lumOneSlice()

•definition of roadWidth

Page 16: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

16

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

ji

lumAll(i,j) is a function that produces the illumination of cell

(i,j), due to 'all' lanterns

lfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

Defining lumSlice(i) :

lumSlice(i)=#(j,cellsPerSlice,lumAll(i,j),vAppend)

Page 17: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

17

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanterns

maxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max)

minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)

lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)

lumOneSlice(w)=#(l,vSeq(0,roadLength),lumAllLant(w,l),vAppend)

To be done:

•definition of p

•definition of nLanterns

•definition of lumAllLant()

•definition of roadWidth, roadLength

Page 18: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

18

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

Defining lumAll(w,l) :

lumAll(w,l)=#(n,allContributingLanterns,lum(w,l,n),add)

lum(w,l,n) is a function that produces the illumination of cell

(i,j)=(w,l), due to lantern nr. n

Page 19: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

19

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanterns

maxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max)

minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)

lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)

lumOneSlice(w)=#(l,vSeq(0,roadLength),lumAllLant(w,l),vAppend)

lumAllLant(w,l)=#(n,vSeq(-1,nLanterns),lum(w,l,n),add)

To be done:

•definition of p

•definition of nLanterns

•definition of lum()

•definition of roadWidth, roadLength, nLanterns

Page 20: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

20

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternslfG: aggregation of slices

slice: aggregation of cells (i=const)

cell: receives light from all lanterns

Defining lum(w,l,n):

lum(w,l,n)=p/(h*h+pow(abs(l-n*dL),2)+pow(abs(w-lW),2))

h

l - n dLw - lW

image: http://www.ph.unimelb.edu.au/~ddolce/

Page 21: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

21

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanternsmaxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max) minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)lumOneSlice(w)=#(l,vSeq(0,roadLength),lumAllLant(w,l),vAppend) lumAllLant(w,l)=#(n,vSeq(-1,nLanterns),lum(w,l,n),add)lum(w,l,n)=p/(h*h+pow(abs(l-n*dL),2)+pow(abs(w-Wh),2))

To be done:

•definition of p

•definition of nLanterns

•definition of roadWidth, roadLength, nLanterns, h, dL, Wh

Page 22: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

22

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanternsmaxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max) minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)lumOneSlice(w)=#(l,vSeq(0,roadLength),lumAllLant(w,l),vAppend)lumAllLant(w,l)=#(n,vSeq(-1,nLanterns),lum(w,l,n),add)lum(w,l,n)=p/(h*h+pow(abs(l-n*dL),2)+pow(abs(w-Wh),2))p=slider(200,100,2000)nLanterns=1+roadLength/dLdL=slider(25,5,50) h=slider(5,1.5,30)roadLength=40 roadWidth=10 Wh=roadWidth/2

To be done:

•represent constraints

•more intuitive output (graphics

Page 23: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

23

A Core Course on ModelingWeek 4 – The Function of Functions

Street LanternstotPow=p*nLanternsmaxInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],max),max) minInt=#(i,vDom(lfG),#(j,vDom(lfG[i]),lfG[i][j],min),min)lfG=#(i,vSeq(0,roadWidth),lumOneSlice(i),vAppend)lumOneSlice(w)=#(l,vSeq(0,roadLength),lumAllLant(w,l),vAppend)lumAllLant(w,l)=#(n,vSeq(-1,nLanterns),lum(w,l,n),add)lum(w,l,n)=p/(h*h+pow(abs(l-n*dL),2)+pow(abs(w-Wh),2))p=slider(200,100,2000)nLanterns=1+roadLength/dLdL=slider(25,5,50) h=slider(5,1.5,30)roadLength=40 roadWidth=10 Wh=roadWidth/2a=descartes([[image:[scaleX:2,scaleY:2,mapR:lfG,mapG:lfG,mapB:cons]]])maxP=150minP=10cons=if(lfG<minP,0,if(lfG>maxP,255,128))

Page 24: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

24

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravity

http://www.opencourse.info/astronomy/introduction/05.motion_planets/

Page 25: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

25

A Core Course on ModelingWeek 4 – The Function of Functions

Street Lanterns

Purpose:

• analyse the motions of planets

Interesting quantities:

• for each planet, its location r = [x:xlocation,y:ylocation]

as it develops over time

Page 26: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

26

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])

r = r{1} + v * dt if time > 1 take dt = 1 for simplicity – although this is dimensionally wrong!

= initial locations if time 1

So r=[[x: ..,y: …], [x: ..,y: …], [x: ..,y: …], …] and

v=[[x: ..,y: …], [x: ..,y: …], [x: ..,y' …], …]

Page 27: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

27

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])

To do:

*define v: a vector of velocities (one for each planet), each velocity being a vector

Page 28: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

28

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]])

v = v{1} + a * dt if time > 1 take dt = 1 for simplicity – although this is dimensionally wrong!

= initial velocities if time 1

So v=[[x: ..,y: …], [x: ..,y: …], [x: ..,y: …], …] and

a=[[x: ..,y: …], [x: ..,y: …], [x: ..,y: …], …]

Page 29: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

29

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]])

To do:

*define a: a vector of accelerations (one for each planet), each acceleration being a vector

Page 30: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

30

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)

f = ma, so a = f/m

Since a is a vector, f and m must be vectors with same amount of elements.

Every element of F is a [x: …, y: …] vector;

every element of m is a number, so

every element of a is a [ x: …, y: …] vector.

Page 31: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

31

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)

To do:

*define f: a vector of forces (one for each planet), each force being a vector

Page 32: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

32

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)

To do:

*define forceOnOnePlanet() (=the force excerted by all planets in the form of a vector of forces: one force for each of the planets; every force itself is also a vector)

Forces (=vectors [x: …, y: …] are appended, i.e. combined into a vector-of-vectors: [[x: …, y: …], [x: ..., y: …], [x: …,y: …] , …]

Page 33: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

33

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)

To do:

*define newton (function calculating the force from one planet onto another planet)

Condition i != j : planets don't attract themselves.Mapping: [a,b]+c=[a+c,b+c], hence

if(i!=j,newton(i,j),0)

instead of

if(i!=j,newton(i,j),[x:0,y:0]))

Page 34: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

34

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)

Gravity force: Fij = g mi mj / ||ri - rj||2, directed along ri – rj. So:

Fij = g mi mj (ri – rj) / ||ri – rj||3

mi mj

Page 35: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

35

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))

To do:

*define vNormEuclid

*define distVec

Page 36: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

36

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*mm[i]*mm[j]*distVecdistVec(i,j)/(pow(vNormEuclid(distVecdistVec(i,j)),3))

built-in function:

vNormEuclid(a)=i ai 2

Page 37: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

37

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRangepRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*mm[i]*mm[j]*distVecdistVec(i,j)/(pow(vNormEuclid(distVecdistVec(i,j)),3))

To do:

*define distVec() (a function, computing the difference between two vectors, returning the difference vector)

Page 38: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

38

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]

Page 39: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

39

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/mm,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*mm[i]*mm[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]

To do:

*define m (a vector, containing the masses of the planets)

Page 40: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

40

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]

Page 41: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

41

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRangepRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRangepRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]

To do:

*define pRange (set of numbers, being the indices of the planets)

Page 42: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

42

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)

To do:

*define g (gravity)

Page 43: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

43

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)g=slider(0.002,0,0.01)

Page 44: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

44

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(timetime>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(timetime>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(timetime>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)g=slider(0.002,0,0.01)

To do:

* timetime: number, increasing one every re-calculation

Page 45: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

45

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)g=slider(0.002,0,0.01)time=if(!butbut,time{1}+1,0)

To do:

* butbut: boolean, being true when user clicks button

Page 46: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

46

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)g=slider(0.002,0,0.01)time=if(!but,time{1}+1,0)but=button()

To do:

* nicer visuals (otherwise we see just varying numbers)

Page 47: 1 A Core Course on Modeling Examples street lanterns planets and gravity Week 4 – The Function of Functions

47

A Core Course on ModelingWeek 4 – The Function of Functions

Planets and Gravityr=cond(time>1,r{1}+v,[[x:0,y:0],[x:15,y:0],[x:7,y:10],[x:-7,y:10],[x:-15,y:0],[x:-7,y:-10],[x:7,y:-10]])v=cond(time>1,v{1}+a,[[x:0,y:0],[x:0,y:0.1],[x:-0.05,y:0.03],[x:-0.05,y:-0.03],[x:0,y:-0.1],[x:0.05,y:-0.03],[x:0.05,y:0.03]]) a=cond(time>1,f/m,0)f=#(i,pRange,forceOnOnePlanet(i),vAppend)forceOnOnePlanet(i)=#(j,pRange,if(i!=j,newton(i,j),0),add)newton(i,j)=g*m[i]*m[j]*distVec(i,j)/(pow(vNormEuclid(distVec(i,j)),3))

distVec(i,j)=r{1}[j]-r{1}[i]m=[80,2,2,2,2,2,2]pRange=vSeq(0,7)g=slider(0.002,0,0.01)time=if(!but,time{1}+1,0)but=button()plotResult=descartes([[locations:[tagy:-8,fill:'interior',data:#(i,pRange,[x:50+r[i].x,y:50+r[i].y,rad:2.5* pow(m[i],0.33),tag:names[i],pointSize:pointSizes[i]],vAppend)]]])

names=['sun','1','2','3','4','5','6']pointSizes=[5,3,3,3,3,3,3,3]