How to write a function that returns the amount of steps that happened before running into a wall?
1 次查看(过去 30 天)
显示 更早的评论
Im trying to write a function that lets my object take a certain number_ of _steps in its current direction tracking how many steps it took before it runs into a wall. Then I want the function to return the number of steps taken before hitting the wall. So far I have this (code below) but its not working and im not sure what i did wrong.
function walk(number_of_steps)
steps_made_successfully= number_of_steps;
if steps 0
step_made_successfully 1
return(step_made_successfully)
end
0 个评论
采纳的回答
Image Analyst
2022-10-26
So many things wrong with this. number_of_steps should be returned from the function, and it is not (yet). The input should be the distance from the start to the wall, and the steps length (stride distance).
function number_of_steps = walk(distanceToWall, stepLength)
number_of_steps= 0; % Initialize
distanceSoFar = 0
while distanceSoFar < distanceToWall && number_of_steps < 999999 % 999999 is the fail safe to prevent infinite loop.
% Here is where you write some more code.....
end % of while
end % of function
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!