minimum value of one inequality

4 ビュー (過去 30 日間)
danish ansari
danish ansari 2020 年 9 月 24 日
コメント済み: Ameer Hamza 2020 年 9 月 24 日
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

採用された回答

John D'Errico
John D'Errico 2020 年 9 月 24 日
編集済み: John D'Errico 2020 年 9 月 24 日
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. :)
  1 件のコメント
danish ansari
danish ansari 2020 年 9 月 24 日
yes you're correct i didn't used 0.05 instead i used 0.1. that's why i am getting n=8.
John D'Errico Thank you very much for the detailed explaination.

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 24 日
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 件のコメント
John D'Errico
John D'Errico 2020 年 9 月 24 日
GA would work.
Ameer Hamza
Ameer Hamza 2020 年 9 月 24 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by