1 a core course on modeling examples street lanterns planets and gravity week 4 – the function of...
TRANSCRIPT
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
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
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 …
5
A Core Course on ModelingWeek 4 – The Function of Functions
Street Lanterns
ji
(i,j)
dL
h roadLengthroadWidth
lW
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
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]
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]
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]
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])
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])
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])
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
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
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
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)
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
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
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
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/
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
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
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))
24
A Core Course on ModelingWeek 4 – The Function of Functions
Planets and Gravity
http://www.opencourse.info/astronomy/introduction/05.motion_planets/
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
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' …], …]
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
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: …], …]
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
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.
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
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: …] , …]
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]))
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
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
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
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)
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]
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)
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]
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)
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)
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)
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
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
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)
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]