Am I doing this right?

7 Ansichten (letzte 30 Tage)
Nathan
Nathan am 30 Sep. 2012
I have to use the while loop to write a function that will ask for a given height of an object and return the time it takes for the object to reach that height given that h=t+5*sin(t), with a time increment of 0.001, and it has to output "The time for the object to reach a height of (h) is (answer) seconds" so far ive got this:
h= input('input height of object:')
ctr = 0; t = 0:0.001:100;
while t > 0;
t = t + 0.001
h = t + 5*sin(t);
ctr = t + 1;
end
h
ctr
this is my first programming class and i have zero experience could anyone tell me what im doing wrong? i don't know how to the output expression, but the code i have currently isnt correct.

Akzeptierte Antwort

per isakson
per isakson am 30 Sep. 2012
Bearbeitet: per isakson am 30 Sep. 2012
Here is the output of running a modified version of your script:
input height of object:123
h =
123
x =
123.0039
ctr =
121.0200
t =
120.0200
where your modified script is
h = input('input height of object:');
ctr = 0;
t = 0;
x = -999; % to make sure to get into the loop
while x < h
t = t + 0.001;
x = t + 5*sin(t);
ctr = t + 1;
end
h
x
ctr
t
Note that
  • t is a scalar that is incremented by 0.001 in each loop.
'
--- In response to comments ---
This line, which I copied from Image Analysts comment and adapted, should do the trick
fprintf('\n\nThe time for the object to reach a height of %.3f is %.3f seconds!\n', x, t);
add it to the end of your script.
Some more comments:
  • programming well is difficult - IMO
  • there are at least two reasons why short variable names are often used: i) quicker to type and ii) not that long ago the cost of processing long names did matter. I have once changed names to squeeze a program into an "Apple II"
  • it is a good idea to use self explaining names
  • "while tentative_height < target_height" is easier to understand
  • ctr in the script could be removed
  • there are certainly more efficient ways to solve this equation
  3 Kommentare
Image Analyst
Image Analyst am 30 Sep. 2012
I guess you haven't seen my answer yet.
By the way, I think you should use explicit variable names. Originally I thought ctr meant "center" like where did the object peak in height. But now I think it means "counter" but if it is, adding 1 to t does not make it a loop counter, it makes it a time - and a weird, meaningless time at that. What is the purpose of ctr? Is it supposed to be like counter in my code?
Nathan
Nathan am 30 Sep. 2012
i dont know. I have absolutely no experience with programming and i was trying to do it based on the example my professor did. he had it in there so i put it in, i don't know what it does, i dont understand any of this and as the assignment is due monday i can't ask him for help because he isnt available on weekends. I did see you you program, and it confuses the hell out of me because i have no clue what any of it means, the only thing with your program is it outputs every value of h and t on the increment, i just want the program to display the time for the hieght i input

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 30 Sep. 2012
Bearbeitet: Image Analyst am 30 Sep. 2012
Nice try, but not quite right. You can try again but get rid of the "t = 0:0.001:100" line. Plus I have no idea what you're doing with ctr.
Anyway, there's a more MATLABish way of doing it:
h = input('input height of object (< 5):') ;
t = 0:0.001:100;
element_exceeds_h = find(5*sin(t) > h, 1, 'first')
time_that_happens = t(element_exceeds_h)
Note that 5*sint(t) is the height as a function of time, and T can be a whole vector - it does not need to be a single value. That's the benefit of MATLAB! element_exceeds_h is the element of your t array where the height exceeds the user's input. The value of t at that element is when it happens.
  2 Kommentare
Nathan
Nathan am 30 Sep. 2012
that may be a better way to do it but my assignment is to use a while loop, the exact assignment is:
The height of an object is defined by the following equation:
h = t + 5*sin(t)
Where “h” is the height and “t” is time. Use a time increment of .001
Write a program using the WHILE LOOP that will ask for a given height (h), and return the time that the object reaches the height. Test your program for h at 8, 15, and 20. You only have to show the solution at 8, 15, or 20; not all three.
Graph the height verses time.
When you print the command window it must include your name, date, and the line where it asks for the input, the input value, and the output the following sentence:
“The time for the object to reach a height of (h) is (answer) seconds”
Image Analyst
Image Analyst am 30 Sep. 2012
Bearbeitet: Image Analyst am 30 Sep. 2012
Close, you almost got it, but you need to keep track of time and the height separately in an array so you can plot them. Plus you also have the actual height at time t plus the desired height, so there are two heights, not just one.
clc;
clearvars;
desiredHeight = input('Input height of object:')
counter = 1;
t = 0;
while t >= 0;
t = t + 0.001
timeAxis(counter) = t;
height(counter) = t + 5*sin(t);
fprintf('\n\nHeight = %.2f at t = %.2f!\n', height(counter), t);
if height(counter) >= desiredHeight
fprintf('\n\nThe time for the object to reach a height of %.3f is %.3f seconds!\n', height(counter), t);
break;
end
counter = counter + 1;
end
plot(timeAxis, height, 'b-');
grid on;
counter
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by