So in Mathematica, if I have a function lambda1 that is defined below, how do I create a condition that if lambda1 < 1e-300, then lambda1 = 1 and if lambda1 > 1e-300, it equals its true value?
x={1,5,9} and mx and sx are defined later in the code when I run a Monte Carlo integration.
f[x_,mx_,sx_] := (1/(sx*Sqrt[2*Pi]))*Exp[-((x-mx)^2)/(2*(sx^2))]
lambda1[xx1_,mx_,sx_,xx2_,xx3_,gamma1_] := 1/((f[x1,mx,sx]*f[x2,mx,sx]*f[x3,mx,sx]))
Related
I'm developing a simulation of a swarm of drones in OpenModelica. The problem is that a lot of scenary can happen in the simulation, and two of the most common are
battery at 0%
collision with other objects.
In both cases, i want the drone to fall, until it reaches the floor, which in my simulation is equal to z = 0, and it won't change its position.
I don't know how to do it because my code calculate velocity with derivative functions, here is an example of how my code works:
block drone
if(battery[i] <= 0 or droneDead)
Trustx := (tmpFx/K.m);
Trusty := (tmpFy/K.m);
Trustz := (tmpFz/K.m);
else
Trustx := if(z > 5) then -(K.m * K.g) else 0;
Trusty := if(z > 5) then -(K.m * K.g) else 0;
Trustz := if(z > 5) then -(K.m * K.g) else 0;
end if;
equation
der(Vx) = Trustx;
der(Vy) = Trusty;
der(Vz) = Trustz;
der(x) = Vx;
der(y) = Vy;
der(z) = Vz;
end drone;
If this example is not explaing the question or is not good to read, i will edit it soon or i will link the .mo file
I am new to Julia and trying to understand how things work.
Below is the sample code I just wrote.
(This is the baseline code and I am planning to add other lines one by one.)
I expected to see something like 1 2 3 4 5 6 7... from test = check(m)
However, I don't see any result.
Any help will be very much appreciated.
using Pkg
using Optim
using Printf
using LinearAlgebra, Statistics
using BenchmarkTools, Optim, Parameters, QuantEcon, Random
using Optim: converged, maximum, maximizer, minimizer, iterations
using Interpolations
using Distributions
using SparseArrays
using Roots
# ================ 1. Parameters and Constants ============================
mutable struct Model
# Model Parameters and utility function
δ::Float64
function Model(;
δ = 0.018,
)
new(
δ
)
end
end
function check(m)
it = 0
tol=1e-8
itmax = 1000
dif = 0
# Iteration
while it < itmax && dif >=tol
it = it + 1;
V = Vnew;
println(it)
end
return itmax
end
m=Model()
test = check(m)
dif = 0
tol = 1e-8
while it < itmax && dif >= tol
Now explain to me how
dif >= tol
I really hope someone can help with this...
This is what i have in the .mod file
set I := 1..10;
set J := 1..10;
set K := 1..2;
set W := 1..20;
param v{K, W};
param d{I, J};
var x1, integer;
var y1, integer;
var x2, integer;
var y2, integer;
var assist{W}, binary;
and this is the code generating error:
minimize nome: sum{w in W} (if (assist[w] == 0) then
(if (x1 >= v[1,w]) then
(if (y1 >= v[2,w]) then
(d[x1 - v[1,w],y1 - v[2,w]])....
where the error regards the last line and says:
Variables in subscripts are not yet allowed.
context: (d[x1 - v[1,w],y1 - >>> v[2,w]] <<< )
this is one of constraints (others are just the same):
subject to rangex1:
x1 > 0 && x1 <= 10;
As the error message says, you can't use decision variables within a subscript in AMPL. In this case x1 and y1 are decision variables, so d[x1 - v[1,w],y1 - v[2,w]] is not allowed. You'll need to reformulate the problem in a way that avoids this issue.
Some algorithms (allocate a binary tree...) need to compute a base 2 exponential. How to compute it for this native type?
newtype {:nativeType "uint"} u32 =
x: nat | 0 <= x < 2147483648
This is an obvious try:
function pow2(n: u32): (r: u32)
requires n < 10
{
if n == 0 then 1 else 2 * pow2(n - 1)
}
It fails because Dafny doubts that the product stays below u32's max value. How to prove that it's value is below 2**10?
In this case, it is more convenient to first define the unbounded version of the function, and then prove a lemma showing that when n < 10 (or n < 32, even) it is in bounds.
function pow2(n: nat): int
{
if n == 0 then 1 else 2 * pow2(n - 1)
}
lemma pow2Bounds(n: nat)
requires n < 32
ensures 0 <= pow2(n) < 0x100000000
{ /* omitted here; two proofs given below */ }
function pow2u32(n: u32): u32
requires n < 32
{
pow2Bounds(n as nat);
pow2(n as nat) as u32
}
Intuitively, we might expect the lemma to go through automatically, because there are only a small number of cases to consider: n = 0, n = 1, ... n = 31. But Dafny will not perform such case analysis automatically. Instead, we have a couple of options.
First proof
First, we can prove a more general property, which, by the magic of inductive reasoning, is easier to prove, despite being stronger than what we need.
lemma pow2Monotone(a: nat, b: nat)
requires a < b
ensures pow2(a) < pow2(b)
{} // Dafny is able to prove this automatically by induction.
The lemma then follows.
lemma pow2Bounds(n: nat)
requires n < 32
ensures 0 <= pow2(n) < 0x100000000
{
pow2Monotone(n, 32);
}
Second proof
Another way to prove it is to tell Dafny it should unroll pow2 up to 32 times, using a :fuel attribute. These 32 unrollings are essentially the same as asking Dafny to do case analysis on each possible value. Dafny can then complete the proof without additional help.
lemma {:fuel pow2,31,32} pow2Bounds(n: nat)
requires n < 32
ensures 0 <= pow2(n) < 0x100000000
{}
The :fuel attribute is (lightly) documented in the Dafny Reference Manual in Section 24.
A bit of a cheat, but with so narrow a domain, this works very well.
const pow2: seq<u32> :=
[0x1, 0x2, 0x4, 0x8, 0x10, 0x20];
lemma pow2_exponential(n: u32)
ensures n == 0 ==> pow2[n] == 1
ensures 0 < n < 6 ==> pow2[n] == 2 * pow2[n - 1]
{}
I have an AMPL model file that I'm working to convert to GLPK. It begins:
param n; # The number of nodes in the graph
set V := {1 .. n}; # The set of vertices in the graph
set E within V cross V; # The set of edges in the graph
set NE within V cross V := {i in V, j in V: i < j} diff E;
set FIXED within V cross V default {}; # The set of demand pairs with fixed flow
When running this, I get the following error:
_test.mod:5: set expression following default must have dimension 2 rather than 1
Context: : i < j } diff E ; set FIXED within V cross V default { } ;
MathProg model processing error
This must be a syntactic difference between MathProg and its superset, AMPL—running the code in AMPL works perfectly. How does one express a 2D empty set in MathProg?
Alright, the hack of a solution is this:
set FIXED within V cross V default {i in V, j in V: 1 < 0};
Put an obviously false condition. It'll have the dimensionality you want and still be empty.