Write a program in Matlab where the program plays a hot and cold game. The user
ID: 3575977 • Letter: W
Question
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Calculate the distance to the object. Pick another point, calculate the distance to the object. Asking only which point was closer, you pick your next location and calculate distance. Keep going until distance is less than .5.
Explanation / Answer
x=input('what is the x location of the object? ');
y=input('what is the y location of the object?');
lowx = 0;
highx = 1000;
lowy = 0;
highy = 1000;
x1 = (highx-lowx).*rand(1, 1) + lowx;
x2 = (highx-lowx).*rand(1, 1) + lowx;
y1 = (highy-lowy).*rand(1, 1) + lowy;
y2 = (highy-lowy).*rand(1, 1) + lowy;
while sqrt((y1-y)^2+ (x1-x)^2)>0.5 && sqrt((y2-y)^2+ (x2-x)^2)>0.5
if sqrt((y1-y)^2+ (x1-x)^2) > sqrt((y2-y)^2+ (x2-x)^2)
y1 = (y2+y1)/2.0;
x1 = (x2+x1)/2.0;
else
y2 = (y2+y1)/2.0;
x2 = (x2+x1)/2.0;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.