DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Rock physics math-thing [PHP]  (Read 8,600 times)

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Rock physics math-thing [PHP]
« on: January 19, 2011, 11:52 AM »
Yes, this is part of a homework which I've tried to do 2 days now...
I tried to translate it as good as I can


"if rock is thrown directly up from the ground with starting speed of 20 meters/second, rock's height s (meters) after t (seconds) can be count with this formula:
s = -5t2 + 20t

Make a program, which counts and prints rock's height on each second."

I've tried this with different changes but none of them have worked so far:

Code: (php)
$time = 1;
$height = 1;

while ($height > 0) {
$height = ((-5*$time*2)+(20*$time));
$time++;
echo "Rock's height is " . $height . " when " . $aika . " -seconds has passed.<br />";
}

And this prints out infinite lines of this:

Rock's height is 10 when -seconds has passed.
Rock's height is 20 when -seconds has passed.
Rock's height is 30 when -seconds has passed.
Rock's height is 40 when -seconds has passed.
Rock's height is 50 when -seconds has passed.
Rock's height is 60 when -seconds has passed.
Rock's height is 70 when -seconds has passed.
Rock's height is 80 when -seconds has passed.
Rock's height is 90 when -seconds has passed.
Rock's height is 100 when -seconds has passed.

It goes on and on.


So ... what am I doing wrong / not getting?

-RedPillow

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Rock physics math-thing [PHP]
« Reply #1 on: January 19, 2011, 11:57 AM »
it's not your code, it's your starting formula:

Quote
s = -5t2 + 20t

this is not correct.  at least not in our universe.

if s = -5t2 + 20t this is same as saying s = 10t, which is actually what your program shows, and is clearly not a formula of much value :)

it's probably supposed to be t^2 not t*2.
« Last Edit: January 19, 2011, 12:02 PM by mouser »

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
Re: Rock physics math-thing [PHP]
« Reply #2 on: January 19, 2011, 12:03 PM »
But the exercise told me that formula  :huh:
My teacher has messed these up before though ... any idea how to actually do this thing then?

Edit: Oh ... I'll try the t^2 one.

Am I right when I think, that the printout should be something like this:


Rock's height is 10 when 1-seconds has passed.
Rock's height is 20 when 2-seconds has passed.
Rock's height is 30 when 3-seconds has passed.
Rock's height is 40 when 4-seconds has passed.
Rock's height is 30 when 5-seconds has passed.
Rock's height is 20 when 6-seconds has passed.
Rock's height is 10 when 7-seconds has passed.
Rock's height is 0 when 8-seconds has passed.

« Last Edit: January 19, 2011, 12:08 PM by RedPillow »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: Rock physics math-thing [PHP]
« Reply #3 on: January 20, 2011, 12:17 AM »
I could be missing something... But I see nothing that would cause the rocks travel to apex and return. So line 4's while loop is (er...) infinite...because its always counting up away from its 0 cut off point.

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
Re: Rock physics math-thing [PHP]
« Reply #4 on: January 20, 2011, 01:41 AM »
It's the -t^2 bit that causes the rock to eventually fall back to earth. Looking at height as a function of t, i.e h(t) then the derivative h'(t) is

-10*t +20

At it's max/min h'(t) = 0 => -10*t + 20 = 0 => t = 2

So the rock should reach it's peak height at t = 2 seconds.

I'm pretty positive mouser is right in assuming it should be t^2, the typo looks to me like a classic copy and paste error. Something that is extremely careless given it's coming from a teacher.
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: Rock physics math-thing [PHP]
« Reply #5 on: January 20, 2011, 12:49 PM »
Okay, thanks

It's been (quit) a while since I've done anything with formulas, and can't for the life of me recall what ^ does/means/is telling it to do.

worstje

  • The Gent with the White Hat
  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 589
    • Coding Snacks
    • Participated in NANY Challenge Three Times
Re: Rock physics math-thing [PHP]
« Reply #6 on: January 20, 2011, 01:17 PM »
To put it really simply, ^ (english: to the power of) says how often something needs to be multiplied with itself.

x^1 = x
x^2 = x * x
x^3 = x * x * x
x^4 = x * x * x * x
x^5 = x * x * x * x * x
x^6 = x * x * x * x * x * x
etc

Of course, with negatives (x^(-2)) and non-whole numbers (x^pi) it isn't as simple or intuitive to demonstrate, but the same principles apply mathematically. However, negative powers can be rewritten like this:

x^-y  = 1 / (x^y)

At least, I hope my math isn't failing me as I haven't used this stuff in a while. :)
« Last Edit: January 20, 2011, 01:19 PM by worstje »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: Rock physics math-thing [PHP]
« Reply #7 on: January 20, 2011, 01:28 PM »
Oh yeah, that...

Crap  :-[ I shoulda remembered  that one.

Thank you,

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
Re: Rock physics math-thing [PHP]
« Reply #8 on: January 20, 2011, 04:03 PM »
Also the forum bbcode supports sup and sub, so things can be made look more mathsey

s = -5t2 + 20t  :Thmbsup:
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • Cody Award
    • Official Forum Clown
    • rotten turkey sandwich - Rotten Turkey Sandwich
Re: Rock physics math-thing [PHP]
« Reply #9 on: January 20, 2011, 04:47 PM »
Unusual though it may be to find a post from me that is anything other than facetious, fallacious, or frumentarious, mathematical formulae are something of a speciality of mine. I think some of you gentlemen would benefit from an example in order to keep your wits sharp. So, here is a challenging little example…

Find x
[attach]
Chris

worstje

  • The Gent with the White Hat
  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 589
    • Coding Snacks
    • Participated in NANY Challenge Three Times
Re: Rock physics math-thing [PHP]
« Reply #10 on: January 20, 2011, 06:37 PM »
That one is so simple and so standard I actually redid the math twice to make sure I wasn't getting it wrong (you implied something like challenging and that it is about sharp wits, and I'm afraid it ranks nowhere close to that for me. 8))

Original address: https://www.donationcoder.com/forum/index.php?topic=25333.0