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

4 次查看（过去 30 天）
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.
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);
if davg<=lim2 && davg>=lim1...
&& value2<value1
b = [b,[value1;value2]];
end
end
end
b'
##### 2 个评论显示 无隐藏 无
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);
ans = 2×1
0.573576436351046 0.573576436351046
c = cosd(35);
[c; c2]
ans = 2×1
0.819152044288992 0.819152044288992
ct = cotd(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 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 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);
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
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
where you convert each value in b from degrees to radians, and then you pass them into the (almost identical) equation
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 个评论显示 -1更早的评论隐藏 -1更早的评论
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!

### 类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by