minimum value of one inequality
4 views (last 30 days)
Show older comments
danish ansari
on 24 Sep 2020
Commented: Ameer Hamza
on 24 Sep 2020
i need to calculate n which should give minimum even value from the expression. what function should i use to code this.
1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2)))<=0.1
if the value of r is known (say=0.001) and s=7;
then if i calculate manually i am getting n>=6.64
but i need minumum even integer so i should get n=8.
want to know how to code these kind of inequality
p.s: i am a beginner
0 Comments
Accepted Answer
John D'Errico
on 24 Sep 2020
Edited: John D'Errico
on 24 Sep 2020
This is just mathematics.
r = 0.001;
s = 7;
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)
ans =
0.043875
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))
ans =
9.02091412871505
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)))]
ans =
1 0.707148623074949
2 0.500071428571429
3 0.353645740108903
4 0.250107142857143
5 0.17689429862588
6 0.125125
7 0.0885185778843687
8 0.0626339285714286
9 0.0443307175136129
10 0.0313883928571429
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;
while flag
n = n + 2;
if 1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2)))<=0.05
flag = false;
end
end
n
n =
10
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)
nsol =
struct with fields:
n: [1×1 sym]
parameters: [1×1 sym]
conditions: [1×1 sym]
>> nsol.n
ans =
k
>> nsol.conditions
ans =
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. :)
More Answers (1)
Ameer Hamza
on 24 Sep 2020
One way is to use optimization toolbox
fmincon(@(n) n, 0, [], [], [], [], [], [], @nlcon)
function [c, ceq] = nlcon(n)
r = 0.001;
s = 7;
c = 1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2))) - 0.05;
ceq = [];
end
4 Comments
Ameer Hamza
on 24 Sep 2020
Oh! I missed the part about the solution being an integer. Yes, ga() will work here.
n = ga(@(n) n, 1, [], [], [], [], [], [], @nlcon, 1);
function [c, ceq] = nlcon(n)
r = 0.001;
s = 7;
c = 1/(2^(n/2)) + (r/s)*(1 - 1/(2^(n/2))) - 0.05;
ceq = [];
end
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!