Question

This basic problem demonstrates the while-loop. (USE MATLAB)

Write a Matlab function called steps that takes one scalar as an input argument and returns two scalars as output arguments. If it is called this way [n, d_left] = steps(d), then it sets n equal to the minimum number of steps required to move from a distance of one foot away from a wall to a distance less than d feet from the wall. The first step is 1/2 foot. If a second step is required, it is 1/4 foot. If a third step is required, it is 1/8 foot, and so forth, with each subsequent step being half the distance of the step before. The second output, d_left, is the distance to the wall after n steps. Here is an example run:

>>[n,d_left] = steps(0.001)

n =

10

d_left =

9.7656e-04

Your Function 1 function [n,d_left] = steps(d) ----input-error check- %--- if 1 error(Distance must be less than 1 foot.) e

0 0
Add a comment Improve this question Transcribed image text
Answer #1
MATLAB CODE------------------------------


function [n,d_left]=steps(d) 
% Error check
if(d>=1) 
error('distance must be less than 1 feet') 
end

%initiate output
n=0;
d_left=1;
dnew=1;
while(dnew>d) 
dnew=dnew/2;
n=n+1;
end
d_left=abs(d-dnew*2) 
end

OUTPUT------------

steps(0.001)
d_left =    9.5312e-04
ans =  10
Add a comment
Know the answer?
Add Answer to:
This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT