Boolean expression in linear program - boolean

I have to express an AND condition in linear program. The Boolean variable z takes a value 1 if both Boolean variables x and y takes a value 1. Otherwise, z takes a value 0. How do I write it in linear program?

In a pure linear program boolean expressions are not possible.
If you are in an (mixed-)integer program and x,y,z are all binary variables then you can implement the AND by the following.
z >= x+y-1
z <= x
z <= y
Here the first ensures z=1 if x=y=1 and the last two forces z=0 if any of the two is not 1.
As #Erwin Kalvelagen pointed out in the comments this is better relaxed than the formulation using 2z <= x+y.

Related

Use of 'ArrayValued' in Matlab numerical integration

Why when performing numerical integration in Matlab with integral does this case need 'ArrayValued' to be set to true:
f = #(x) 5;
integral(f,0,2,'ArrayValued',true)
... while in this case the option isn't needed?:
f = #(x) x;
integral(f,0,2)
From the documentation for integral describing the integrand argument:
For scalar-valued problems, the function y = fun(x) must accept a
vector argument, x, and return a vector result, y. This generally
means that fun must use array operators instead of matrix operators.
For example, use .* (times) rather than * (mtimes). If you set the
'ArrayValued' option to true, then fun must accept a scalar and return
an array of fixed size.
So, a constant function like f = #(x) 5 does not return a result the same size as x if x is a vector. The integral function requires this because under the hood it is vectorized for scalar functions for performance – it actually evaluates the integrand at multiple points simultaneously with a single function call.
You can make your constant function compliant and not require 'ArrayValued' to be true with something like this:
f = #(x) 5+0*x;
integral(f,0,2)

multipoint boundary value ode with varying coefficients in matlab

I have a second order differential equation given as:
d²u/dz²=a+bu, where a and b are constants that vary in different intervals and z∈[0,Zn]
Intervals are given as I1:z∈[0,Z1), I2:z∈[Z1, Z2), ..., I(n):z∈[Z(n-1), Z(n)] and {a,b} constants varies as {a1,b1} in I1, {a2,b2} in I2,...,{an,bn} in I(n).
Boundary condition is given as u(z=0)=U0, u(z=Zn)=Umax.
A continuous graph of u vs z is required.
I thought of using bvp5c in matlab but bvp5c can solve multipoint boundary value problems where a = a0 < a1 < a2 < ... < an = b in the interval [a,b]. The points a1,a2, ... ,an–1 represent interfaces that divide [a,b] into regions. The problem is my constants(a,b in above ode) is also varying in different intervals.
You can do it. If you read the bvp5c docs, you will see that it takes a function handle (odefun) which takes the z, and u arguments. Simply return the correct value for the RHS based on value of the argument z. Note that you have to convert your 2nd order equation to a 1st order one.
du/dz = v
dv/dz = a(z) + b(z)u
You need to return the vector [v, a(z) + b(z)u] from the two arguments z and [u,v] that your odefun will be called with.

Evaluating an integral numerically using Matlab

fun= #(x)exp(- a*(d+1).*(t-x)./(d-(t-x)) ) *b.*exp(-b*x);
int= integral(fun,0,t);
Since I did not find a closed form solution, I am using the above code in Matlab to numericaly evaluate the integral.
I am evaluating this integral for different values of d.
The problem is that when I take d<t I get inf. Any idea what is the problem ? and what approach can be used to evaluate the integral in this case ?
Note that a, b, d, and t are all positive. Ex: a=0.1, b=1, t=4.
If you look at the denominator of the first term of fun you see that it depends on t, x and d. So what happens if d == t and x == 0? The denominator goes to 0.0. If d > t there is no positive value for x that will cause the denominator to go to 0.0.
If we let d == t and plot that first term for values of x = 3:.001:5 we see this:
That discontinuity causes the values to be in the range [-Inf, Inf]. Now if we plot exp of these values we see this:

differences between x = y and x = y == 1

Suppose we have logical image y and we want to make a copy of it. What is differences between the following statements:
x = y;
x = y==1;
x = y is an assignment. It sets the variable x to the value currently contained in variable y.
x==y is a logical operator asking "Is x equal to y"?
The statement x=y==1 sets all parts of x to true where the corresponding value of y is equal to 1.
The difference between the two statements you pose is thus that in the first statement, x=y, x becomes an exact copy of y. In the second statement however, x becomes a logical matrix with boolean values. 1 where y contains a 1 and 0 where y contains anything but 1.
In your specific case, where y already is a logical matrix (thus containing only 1 and 0) both statements are thus equivalent as per the above and then the first statement will be faster as the equality check is redundant and thus adds unnecessary overhead.

Matlab log2 equivalent in fortran

I need to translate a code from matlab to fortran 90. What is the best way to implement an equivalent fortran code for the matlab log2 function that dissect floating-point numbers into exponent and mantissa. I need to compute E and F that are described in the matlab documentation :
"[F,E] = log2(X) returns arrays F and E. Argument F is an array of real values, usually in the range 0.5 <= abs(F) < 1. For real X, F satisfies the equation: X = F.*2.^E. Argument E is an array of integers that, for real X, satisfy the equation: X = F.*2.^E."
The Fortran standard has EXPONENT and FRACTION intrinsics that do this dissection. They are elemental, so if you pass them an array you get an array back.