I understand that this formula is super complicated to read, but I guess that the mistake that I'm making is super simple. This is a plot for dissimilarity index for segregation for two districts. The error is "missing value on the left"
plot (
mod (((count turtles with [ditrict-in = "high" color = blue])/ count turtles with [ color = blue ]) -
((count turtles with [ditrict-in "high" color orange])/ count turtles with [ color = orange ]))
+ mod(((count turtles with [ditrict-in = "0" color = blue])/ count turtles with [ color = blue ]) -
((count turtles with [ditrict-in = "0" color = orange])/ count turtles with [ color = orange ]))
)
You have a few errors in your code. The message you're seeing is from the mod operator. You're using mod as if it takes a single argument to the right like mod 10. But if we look at the docs for mod what we find is that mod works like a mathematical operator (+ or -) and takes one argument on the left and one on the right. 15 mod 4 gives 3. So at this point I'm not 100% sure what you want the mod operator to be doing, so I'll leave it to you to adjust how it's used (or maybe you want a different operator).
You also are missing some and operators and = checks in your code, too, though. Sometimes when I'm having trouble tracking down issues, I'll split out complex expressions into their pieces to make it easier to see what's going on. Here is how I split up your code, which still gives the error as you'll need to change the mod portion.
let blueCount (count turtles with [ color = blue ])
let orangeCount (count turtles with [ color = orange ])
let highBlueCount (count turtles with [ditrict-in = "high" and color = blue])
let highOrangeCount (count turtles with [ditrict-in = "high" and color = orange])
let zeroBlueCount (count turtles with [ditrict-in = "0" and color = blue])
let zeroOrangeCOunt (count turtles with [ditrict-in = "0" and color = orange])
plot (
mod ((highBlueCount / blueCount) - (highOrangeCount / orangeCount))
+ mod ((zeroBlueCount / blueCount) - (zeroOrangeCOunt / orangeCount))
)
Related
I'm new to Julia programming I managed to solve some 1st order DDE (Delay Differential Equations) and ODE. I now need to solve a second order delay differential equation but I didn't manage to find documentation about that (I previously used DifferentialEquations.jl).
The equation (where F is a function and τ the delay):
How can I do this?
Here is my code using the given information, it seems that the system stay at rest which is incorrect. I probably did something wrong.
function bc_model(du,u,h,p,t)
# [ u'(t), u''(t) ] = [ u[1], -u[1] + F(ud[0],u[0]) ] // off by one in julia A[0] -> A[1]
γ,σ,Q = p
ud = h(p, t-σ)[1]
du = [u[2], + Q^2*(γ/Q*tanh(ud)-u[1]) - u[2]]
end
u0 = [0.1, 0]
h(p, t) = u0
lags = [σ,0]
tspan = (0.0,σ*100.0)
alg = MethodOfSteps(Tsit5())
p = (γ,σ,Q,ω0)
prob = DDEProblem(bc_model,u0,h,tspan,p; constant_lags=lags)
sol = solve(prob,alg)
plot(sol)
The code is in fact working! It seems that it is my normalization constants that are not consistent. Thank you!
You get a state space of dimension 2, containing u = [u(t),u'(t)]. Consequently the return vector of the right-side function is [u'(t),u''(t)]. Then if ud is the delayed state [u(t-τ),u'(t-τ)] the right side function can be formulated as
[ u'(t), u''(t) ] = [ u[1], -u[1] + F(ud[0],u[0]) ]
I am using both SageMath and Wolfram Alpha to entertain myself over the weekend.
I found this SageMath demo of solving simultaneous equations:
var('x y p q')
eq1 = p+q==9
eq2 = q*y+p*x==-6
eq3 = q*y^2+p*x^2==24
solve([eq1,eq2,eq3,p==1],p,q,x,y)
And it gave me this result:
[
[ p == 1
, q == 8
, x == -4/3*sqrt(10) - 2/3
, y == 1/6*sqrt(10) - 2/3
]
,[p == 1
, q == 8
, x == 4/3*sqrt(10) - 2/3
, y == -1/6*sqrt(10) - 2/3
]
]
I tried this syntax on Alpha:
solve p+q==9 , q*y+p*x==-6 , q*y^2+p*x^2==24 , p==1
It works well.
Question:
How to operate Alpha so I assign each equation to a variable and then supply that variable to solve as a parameter?
I want to simplify my call to solve() so it looks like this:
solve eq1, eq2, eq3, p==1
instead of this:
solve p+q==9 , q*y+p*x==-6 , q*y^2+p*x^2==24 , p==1
So, Crazy Ivan's comment really answers this. WolframAlpha is not a programming language. It is a glorified online smart calculator. You can only do very limited computations in it. Check out the Wolfram Language for the full-fledged programming language.
I want to use the data of [x] to fill in [test] based on certain sequence:
x = matrix(rnorm(330),165,2)
origins = 130:157
horizon = 8
col = 1:2
test = array(0, c(length(origins)*length(col), horizon))
for( origin in origins){
for (c in col){
test[which(origin==origins), ] = x[(origin+1):(origin+8), c]
}
}
However, this code only helps extract the second column of [x] to fill in the first 28 rows of [test]. The following picture is only a part of a complete [test] table, showing the ineffective filling from row 29 to row 56.
enter image description here
Anyone who can help me fill in them completely? Thank you very much.
Here is a possible solution, but it is still not clear what you want the result to be. better to make much smaller data and show desired result.
The left hand side of the assignment, in the original code, does not vary with c, so each time through the loop for c the same rows of test will be overwrittem,
x = matrix(rnorm(330),165,2)
origins = 130:157
horizon = 8
col = 1:2
test = array(0, c(length(origins)*length(col), horizon))
for( origin in origins){
for (c in col){
# the left hand side must vary somehow with c as well.
test[(which(origin==origins)-1) + (c - 1) * length(origins) + 1, ] = x[(origin+1):(origin+8), c]
}
}
I have an MxNx2 array of 2D points, where each point represents the center of a measured property of a grid. The graphical representation is below, with white points being the positions:
The point structure is like this (shape: MxNx2):
[[[xij, yij], [xij, yij], ...]],
[[xij, yij], [xij, yij], ...]],
[[xij, yij], [xij, yij], ...]],
[ ..., ...., ............... ],
[[xij, yij], [xij, yij], ...]]]
The desired output would be like this:
[[[x1, x2], [y1, y2]],
[[x1, x2], [y1, y2]],
....................,
[[x1, x2], [y1, y2]]
So that I could plot every segment one by one (using each pair of x,y positions) like this:
I have trying something similar to:
segments = []
for row in xrange(a.shape[0] - 1):
for col in xrange(a.shape[1] - 1):
here = a[row, col]
below = a[row+1, col]
right = a[row, col+1]
segments.extend(((here, right), (here, below)))
but that leaves the right and bottom edges uncovered. Also, I suspect this is a somewhat "dumb", non-vectorized, brute-force way of doing it, it seems to be a common enough problem to have perhaps a mesh-creating function for it.
Any suggestion is welcome!
It can be done by adding segments separately for axis:
for row in xrange(a.shape[0]):
segments.extend( (a[row, col], a[row, col+1]) for col in xrange(a.shape[1] - 1) )
for col in xrange(a.shape[1]):
segments.extend( (a[row, col], a[row+1, col]) for row in xrange(a.shape[0] - 1) )
Or with zip():
s1 = (a.shape[0]*(a.shape[1]-1), 2)
s2 = (a.shape[1]*(a.shape[0]-1), 2)
segments = list(zip( a[:,:-1].reshape(s1), a[:,1:].reshape(s1))) + \
list(zip( a[:-1,:].reshape(s2), a[1:,:].reshape(s2)))
In case someone is interested, I modified the code I was using and it now works, perhaps not so elegantly or eficiently, but...
pairs = []
for row in xrange(pointarray.shape[0]):
for col in xrange(pointarray.shape[1]):
here = pointarray[row, col]
if row < pointarray.shape[0]-1:
below = pointarray[row+1, col]
pairs.append((here, below))
if col < pointarray.shape[1]-1:
right = pointarray[row, col+1]
pairs.append((here, right))
I'm having trouble understanding this line.
[Pid2 ! {delete, V1a}
|| {Pid1a, V1a} <- PV1a, Pid2 <- P2, Pid1a /= Pid2
],
Here is what I understand:
anything before the double pipe "||" is done repeatedly, according what's after the double pipe. so messages with delete atom is repeated sent to Pid2.
I know '/=' mean inequality. I don't understand what '<-' means, and ultimately what the whole line means.
[something(X) || X <- L], is a list comprehension. L is a list of elements, and this expression creates a list of new elements, forming each element by invoking something() on it.
[something(X,Y) || X <-L, Y<-M] is similar, but an element is created for the Cartesian product of each element in X and Y.
[something(X) || X <-L, Expr] is a filter expression. Same as the first one, but it is executed only for elements of L, where Expr is true for the given X.
[something(X) || {X,..} <-L, Expr] is another kind of filter. In the list comprehension only those elements are taken that can be matched by the element.
One more thing to know is that this can not only be used for generating another list, but also for executing a command for each element. If the result of the list comprehension is not matched, the compiler will know not to generate a list at all. This behavior can be used to mimic foreach from other languages.
Some examples:
1> [ X*2 || X <- [1,2,3] ].
[2,4,6]
2> [ X*Y || X <- [1,2], Y <- [3,4,5] ].
[3,4,5,6,8,10]
3> [ X*3 || X <- [1,2,3,4], X rem 2 == 0 ].
[6,12]
4> [ X || {a,X} <- [{a,1},{a,2},{b,3},{c,4}] ].
[1,2]
So your code generates the Cartesian product of all {Pid1a, V1a} elements from PV1a and Pid2 elements from P2, except for those elements where Pid1a equals Pid2, and for each of these pairs sends the {delete, V1a} message to Pid2.
I don't know Erlang, but this looks just like list comprehensions from a bunch of languages I do know. Hopefully this guess will help you until somebody who knows Erlang can answer:
[Pid2 ! {delete, V1a} || {Pid1a, V1a} <- PV1a, Pid2 <- P2, Pid1a /= Pid2],
Translates to imperative-style pseudocode:
For each item in PV1a, unpacking item to {Pid1a, V1a}
For each Pid2 in P2
If Pid1a /= Pid2
Pid2 ! {delete, V1a}
In other words, for each Pid in PV1a and P2, send the message delete V1a to Pid2 as long as Pid1 and Pid2 are not the same Pid.
It is a list comprehension and the <- operator is used for generators.
Look at a more popular introduction example for LCs; to find triangles where the squares of the integer sides equals the square of the integer hypotenuse, but for a given range of integers Ns.
Ns = [1,2,3,4,5,6,7,8,9].
[{X,Y,C} || X <- Ns, Y <- Ns, C <- Ns, X*X + Y*Y == C*C].
This gives us the following list as output.
[{3,4,5},{4,3,5}]
Which seems correct:
3² + 4² = 5²
9 + 16 = 25
25 = 25
So the list comprehension can be read as give us every X,Y and C such that X is taken from Ns, Y is taken from Ns and C is taken from Ns, and X² + Y² equals C².