Problem 56313. Find Air Temperature from Cricket Stridulation Rate
3 次查看(过去 30 天)
显示 更早的评论
%Stridulation is the process that creates a cricket's “chirp” by rubbing their wings or legs. According to the Old Farmer's Almanac (https://www.almanac.com/predict-temperature-cricket-chirps), the sum of the number of chirps in 14 seconds and 40 is the air temperature in degrees Fahrenheit.
%Crickets generally do not sing at temperatures below 55 F or above 100 F. (https://entomology.unl.edu/k12/crickets/temperature.htm)
%Use this formula to find the approximate outdoor temperature.
% Above is the given question and 4 test provided to answer that are as
% following
%test 1 input is x = 20; output is y_correct = 60;
% test 2 input is x = 30; output is y_correct = 70;
%test 3 input is x = 40; output is y_correct = 80;
%test 4 input is x = 50; output is y_correct = 90;
%for that given test below is my answer
function y = getTemperature_F(nchirps_in_14s)
x=[20 30 40 50]
F=0;
y=x;
y=fliplr(y);
while F<=50
F=F+110;
y=F-y;
end
end
%whats wrong with my answer i am not getting it.
3 个评论
Image Analyst
2024-4-25
Not sure what your edit was, but my answer, scroll down below John's comment, still stands.
回答(2 个)
Image Analyst
2024-4-24
Not sure why you have a while loop. It will enter the while loop because F=0 is less than 50 but then you add 110 to it so the next F is 110 and it will not enter the while loop again because F is more than 50.
And there is no need for x since you just set y equal to it. You could just set y equal to those numbers directly and not even have x. In other words instead of
x=[20 30 40 50]
F=0;
y=x;
y=fliplr(y);
have
F = 0;
y = [50, 40, 30, 20];
Another problem is that you do not have any comments in your code like all good programmers put in.
1 个评论
Image Analyst
2024-4-25
Why not simply add 40 like it said?
y_correct = getTemperature_F(20)
y_correct = getTemperature_F(30)
y_correct = getTemperature_F(40)
y_correct = getTemperature_F(50)
function y_correct = getTemperature_F(nchirps_in_14s)
y_correct = nchirps_in_14s + 40;
if nchirps_in_14s == 0
y_correct = 0;
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Test Model Components 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!