If statement dictating acceptable range is not functioning properly, why?

6 ビュー (過去 30 日間)
Aly Osman
Aly Osman 2022 年 6 月 10 日
コメント済み: Aly Osman 2022 年 6 月 10 日
I'm working on a project to where I have to find pairs of values that provide the same output when inputted into an equation. I want only combinations that provide an output within a defined range, so for example, say I want all the possible combinations of values that sum to 6 +/- 0.5 so combinations that would sum to 5.5, 6 or 6.5 are all acceptable, however if there's a combination that sums to 7, I want that to be excluded. Getting a code working that at least satisfies the process of finding combinations is something I already have, thanks to the gracious people on here in this thread , so my issue is setting the condtions that I just spoke of to exclude certain combinations.
In my test code however, it doesn't seem like this is being done for some reason. I want all combinations that give me a value that is between lim1 and lim2, however when plugging the combinations that are output back into the original equation to find davg, I find that it has cominations that output values way beyond the conditions that I set (for example davg(1)= 89.3, when I set a condition for no combinations that would result in davg being greater than 59.663). So is something off with my if statement, or am I off base here completely? I would appreciate some insight regarding this.
value1= deg2rad(35);
value2=deg2rad(27);
davg_initial= rad2deg(atan((cot(value2)+cot(value1))*0.5));
lim1=(davg_initial-0.5);
lim2= (davg_initial+0.5);
a = [0:0.5:35];
n = length(a);
b = [];
for i = 1:n
value1 = a(i);
for j = 1:n
value2 = a(j);
davg= rad2deg(atan((cot(value2)+cot(value1))*0.5));
if davg<=lim2 && davg>=lim1...
&& value2<value1
b = [b,[value1;value2]];
end
end
end
b'
x=deg2rad(b(1,:));
y=deg2rad(b(2,:));
davg= rad2deg(atan((cot(y)+cot(x))*0.5));
  2 件のコメント
Steven Lord
Steven Lord 2022 年 6 月 10 日
Just a suggestion: rather than repeatedly converting back and forth between degrees and radians I recommend using the degree-based trig functions. Here's a simple example:
format longg
s = sind(35);
s2 = sin(deg2rad(35));
[s; s2] % Compare answers using radians and degrees
ans = 2×1
0.573576436351046 0.573576436351046
c = cosd(35);
c2 = cos(deg2rad(35));
[c; c2]
ans = 2×1
0.819152044288992 0.819152044288992
ct = cotd(35);
ct2 = cot(deg2rad(35));
[ct; ct2]
ans = 2×1
1.42814800674211 1.42814800674211
% Check: cot(x) = cos(x)/sin(x)
check = [ct; c./s]
check = 2×1
1.42814800674211 1.42814800674211
Aly Osman
Aly Osman 2022 年 6 月 10 日
Good suggestion! I've used cosd, sind in the past, but for some reason my mind blanked on using them here since originally I was doing everything in radians and to understand what each number meant I was just doing rad2deg, regardless, I'll switch to this now, much cleaner and shorter! Thank you!

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

採用された回答

Geoff Hayes
Geoff Hayes 2022 年 6 月 10 日
@Aly Osman - what are the units for a and so the units for b? In your looping code, that includes the if statement, you do
for i = 1:n
value1 = a(i);
for j = 1:n
value2 = a(j);
davg= rad2deg(atan((cot(value2)+cot(value1))*0.5));
if davg<=lim2 && davg>=lim1...
&& value2<value1
b = [b,[value1;value2]];
end
end
end
where you take pairs of values from a and pass them into the equation
davg= rad2deg(atan((cot(value2)+cot(value1))*0.5));
If the criteria is met, then you save value1 and value2 to the b array. Once the outer loop completes, then you seem to do a check or validation of the data
x=deg2rad(b(1,:));
y=deg2rad(b(2,:));
davg= rad2deg(atan((cot(y)+cot(x))*0.5));
where you convert each value in b from degrees to radians, and then you pass them into the (almost identical) equation
davg= rad2deg(atan((cot(y)+cot(x))*0.5));
So why is there a conversion from degress to radians of the data in b? Shouldn't you have done that also when extracting value1 and value2 from a?
  1 件のコメント
Aly Osman
Aly Osman 2022 年 6 月 10 日
Wow, I'm a dummy it seems. You are right, value 1 and value 2 are in radians instead of being in degrees, which is what messed everything up. This fixed it and now the numbers are actually within the range I designated in the if statement. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by