Frage

n*log2(n) = A, with A a known value. How do I solve for n in Matlab? Note that n isn't necessary an integer.

War es hilfreich?

Lösung

Or solve the equation analytically and use that:

n = A*log(2)/lambertw(A*log(2))

Andere Tipps

Not the most elegant solution, but you can use fmincon

n = fmincon(@(N) abs(N*log2(N)-A),10, [],[],[],[],1,Inf)

Just use fzero:

solution = fzero(@(n) n.*log2(n)-A, A/5);

I found the initial guess empirically by examining the solution's behaviour on the interval 0-1000; you might want to adjust it for your use case.

If you have Symbolic Math Toolbox installed, all you need is:

solve('n*log2(n)=A', 'n')

ans =
    (A*log(2))/lambertw(0, A*log(2))

You can also use solve with syms:

syms n A

solve(n*log2(n)==A, n)

After syms n A you can also define the value of A:

A = 0
solve(n*log2(n)==A, n)

ans =
    1

A = 2
solve(n*log2(n)==A, n)

ans =
    2

A = 3
solve(n*log2(n)==A, n)

ans =
    (3*log(2))/lambertw(0, 3*log(2))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top