This is just mathematics.
n ALWAYS appears as 2^(-n/2). So calling that number x, first write the problem as
x - r/s*(1-x) <= 0.05
Can you simplify that? Of course you can! Solve for x. We find:
x *(1 + r/s) <= 0.05 + r/s
or, isolating x we see
x <= (0.05 + r/s)/(1 + 1/s)
In this case, x can be seen to be no larger than
>> (0.05 + r/s)/(1 + 1/s)
Now remember what was x. We have
x = 2^(-n/2) <= (0.05 + r/s)/(1 + 1/s)
We can extract n using the log to the base 2. log2 is a monotonic increasing transformation. So it will not change the direction of the inequality. Therefore,
-n/2 <= log2((0.05 + r/s)/(1 + 1/s))
Negating does change the inequality, so we have
n >= -2*log2((0.05 + r/s)/(1 + 1/s))
Therefore, we have n as the smallest even integer that exceeds:
-2*log2((0.05 + r/s)/(1 + 1/s))
So n is 10.
Are you flat out wrong in your computation and your claim that n should be 8? Yes. Sorry, but you are.
>> n = (1:10)';
>> [n,1./(2.^(n/2)) + (r/s)*(1 - 1./(2.^(n/2)))]
Which even integer value of n has a result that is less than or equal to 0.05? (Hint: It aint 6.64, nor is it 8. Try 10.)
The solution is 10.
Do you want to use MATLAB to find the solution in another way? Another simple way is just a brute force while loop. Even that will suffice. We can verify my result.
r = 0.001;
s = 7;
n = 0;
flag = true;
n = n + 2;
if 1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2)))<=0.05
flag = false;
Yes, using a loop is a bit much here, but for a simple problem a loop is entirely adequate.
As you can see, it agrees with what I claimed to be the solution. Ameer already showed you how to use the optimization toolbox to solve the problem, though fmincon is not the correct choice. GA would work. We could also use the symbolic toolbox.
>> syms n real positive integer
>> nsol = solve(1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2)))<=0.05,'returnconditions',true)
struct with fields:
n: [1×1 sym]
parameters: [1×1 sym]
conditions: [1×1 sym]
9 <= k & in(k, 'integer')
So the solution is n must be an integer greater then or equal to 9. If n must also be even, that must be 10. "Even" seems not to be valid as a constraint in the call to syms. Life is hard. :)