Matlab log2 equivalent in fortran - matlab

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.

Related

Identify powers in an algebraic expression for a Buckingham Pi calculation in MatLab

This is a continuation of an earlier question I asked here.
If I create a symbolic expression in MatLab
syms L M T
F = M*L/T^2
I want to identify the powers of each dimension M, L, or T. In this case, the answer should be
for M, 1
for L, 1
for T, -2
There is a relatively easy way to do this if the expression F were a polynomial in MatLab employing the coeffs function. However, my expression is clearly not a polynomial as far as MatLab is concerned.
In the end, I will be working with at least two parameters so I will put them in a cell array since I anticipate cellfun will be useful.
V = L/T
param = {F,V};
The final output should be a table where the rows correspond to each dimension, L M and T and the columns are for each parameter F and V.
syms L M T
F = M*L/T^2
[C,T] = coeffs(expand(log(F),'IgnoreAnalyticConstraints',true))
[exp(T).' C.']
It returns the table:

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:

Use fminsearch to find local minimizer and the minima at that value

I am having trouble using fminsearch: getting the error that there were not enough input arguments for my function.
f = #(x1,x2,x3) x1.^2 + 3.*x2.^2 + 4.*x3.^2 - 2.*x1.*x2 + 5.*x1 + 3.*x2 + 2.*x3;
[x, val] = fminsearch(f,0)
Is there something wrong with my function? I keep getting errors anytime I want to use it as an input function with any other command.
I am having trouble using fminsearch [...]
Stop right there and think some more about the function you're trying to minimize.
Numerical optimization (which is what fminsearch does) is unnecessary, here. Your function is a quadratic function of vector x; in other words, its value at x can be expressed as
x^T A x + b^T x
where matrix A and vector b are defined as follows (using MATLAB notation):
A = [ 1 -1 0;
-1 3 0;
0 0 4]
and
b = [5 3 2].'
Because A is positive definite, your function has one and only one minimum, which can be computed in MATLAB with
x_sol = -0.5 * A \ b;
Now, if you're curious about the cause of the error you ran into, have a look at fuesika's answer; but do without fminsearch whenever you can.
It is exactly what Matlab is telling you: your function expects three arguments. You are passing only one.
Instead of
[x, val] = fminsearch(f,0)
you should call it like
[x, val] = fminsearch(f,[0,0,0])
since you define the function f to accept a three dimensional vector as input only.
You can read more about the specification of fminsearch in the online documentation at http://mathworks.com/help/matlab/ref/fminsearch.html:
x = fminsearch(fun,x0) starts at the point x0 and returns a value x
that is a local minimizer of the function described in fun. x0 can be
a scalar, vector, or matrix. fun is a function_handle.

matlab wrong modulo result when the divident is raised to a power

Just wondering... I tried doing by hand (with the multiply and square method) the operation (111^11)mod143 and I got the result 67. I also checked that this is correct, in many online tools. Yet, in matlab plugging:
mod(111^11,143)
gives 127! Is there any particular reason for this? I didn't find anything in the documentation...
The value of 111^11 (about 3.1518e+022) exceeds the maximum integer that is guaranteed to be represented exactly as a double, which is 2^53 (about 9.0072e+015). So the result is spoilt by insufficient numerical precision.
To achieve the correct result, use symbolic computation:
>> syms x y z
>> r = mod(x^y, z);
>> subs(r, [x y z], [111 11 143])
ans =
67
Alternatively, for this specific operation (modulo of a large number that is expressed as a product of small numbers), you can do the computation very easily using the following fact (where ∗ denotes product):
mod(a∗b, z) = mod(mod(a,z)∗mod(b,z), z)
That is, you can apply the modulo operation to factors of your large number and the final result is unchanged. If you choose factors sufficiently small so that they can be represented exactly as double, you can do the computation numerically without any loss of precision.
For example: using the decomposition 111^11 = 111^4*111^4*111^3, since all factors are small enough, gives the correct result:
>> mod((mod(111^4, 143))^2 * mod(111^3, 143), 143)
ans =
67
Similarly, using 111^2 and 111 as factors,
>> mod((mod(111^2, 143))^5 * mod(111, 143), 143)
ans =
67
from the matlab website they recommend using powermod(b, e, m) (b^e mod m)
"If b and m are numbers, the modular power b^e mod m can also be computed by the direct call b^e mod m. However, powermod(b, e, m) avoids the overhead of computing the intermediate result be and computes the modular power much more efficiently." ...
Another way is to use symfun
syms x y z
f = symfun(mod(x^y,z), [x y z])
f(111,11,143)

MATLAB: Sum of an infinite series, with Fibonacci coefficients

I need to find f(x), which the sum of the following infinite series:
f(x) = sum {C_{n}*x^{n}} ; sum goes from n = 0 to Inf
C[n] is the modified Fibonacci series such that, C[0] = 2, C[1] = 3, and C[n] = C[n-1] + C[n-2].
for such f(x), it converges to natural numbers for x in [0,1)
I need to find these natural numbers. How to go about it?
I initially tried it by first defining a vector C of length 1000, and then tried symsum for the series, MATLAB threw the following error:
Function 'subsindex' is not implemented for MuPAD symbolic objects.
Then I defined a symbolic f, in terns of C[n] and x using 'for' loop.
And then ran a loop over x to discover the natural numbers.
The challenge with this approach is that, it depends on the number of terms, and the precision I choose. Also,.. this gets very demanding.