If statement dictating acceptable range is not functioning properly, why?
Show older comments
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 Comments
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
c = cosd(35);
c2 = cos(deg2rad(35));
[c; c2]
ct = cotd(35);
ct2 = cot(deg2rad(35));
[ct; ct2]
% Check: cot(x) = cos(x)/sin(x)
check = [ct; c./s]
Aly Osman
on 10 Jun 2022
Accepted Answer
More Answers (0)
Categories
Find more on Historical Contests 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!