If statement dictating acceptable range is not functioning properly, why?
4 次查看(过去 30 天)
显示 更早的评论
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));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027195/image.png)
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);
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]
采纳的回答
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!