3
\$\begingroup\$

Physics Challenge

This program should be able to solve for ANY two of the following variables the user wants to solve.

  1. s (displacement)
  2. v (velocity)
  3. u (initial velocity)
  4. a (acceleration)
  5. t (time)

At start the user must choose two variables they want to solve for and what variables they can supply. You are limited to using the following equations.

Equations Permitted

Physics Equations

(http://en.wikipedia.org/wiki/Equations_of_motion)

The entrant with the least amount of code and posts the quickest will win.

Rules

  1. This is code-golf. The shortest code wins.
  2. Your program must terminate in reasonable time for all reasonable inputs.
  3. You are allowed to transform the equations algebraically if need be
  4. The program must be able to interpret a variable of which the user cannot supply (See Example 2)

Input Example

The program will start and ask which variable the user will like to calculate: (s, v, u, a, t) - the user selects this via a char type or string. It then supplies all values for the other unknowns and the program will solve for the variables.

Example 1:

*What would you like to calculate? (s, v, u, a, t)*
Input: a, u     // This could also be s,v s,u, s,a v,u (ANY COMBINATION)

*Enter value for s:*
Input: 7        // The input will always be random

*Enter value for v:*
Input: 5

// Repeat for others

//program calculates

Var a = answer
Var u = answer

Example 2:

*What would you like to calculate? (s, v, u, a, t)*
Input: a, t

*Enter value for u:*
Input: 5

*Enter value for s:*
Input: UNKNOWN

*Enter value for v:*
Input: 7

*The answers for a and t are 464 and 742*

Edit

I think everyone here is overcomplicating the question. It simply asks to solve any 2 variables from the list of (s, v, u, a, t) and solve them using the equations. As the programmer you must be able to allow values to be taken for the variables that are not being solved. I will clarify that the program must be able to solve for any variable supplied by the user on startup. I also removed the somewhat correct equation s=v/t because people are becoming confused by it.

\$\endgroup\$
16
  • 3
    \$\begingroup\$ How is the input received? What format is it in? You've said it's [code-golf], but then added that solving multiple variables will increase the chance of winning - I'd have thought that would take more code and actually reduce their chance of winning. Probably the best way to add an extra bonus without going against the tag you've used is to add a bounty and award it to the (first?) answer that meets the extended spec. \$\endgroup\$
    – Gareth
    Jan 1, 2014 at 10:37
  • 1
    \$\begingroup\$ Adding to @Gareth's remarks: can you please provide some test cases? And I don't understand Rule 3. \$\endgroup\$
    – Howard
    Jan 1, 2014 at 11:56
  • \$\begingroup\$ @Gareth You're right. I changed the challenge to be more difficult. Input is received by any way you would like it to be BUT the program must be able to calculate any case. What do you mean by which format? \$\endgroup\$
    – JOG-Design
    Jan 1, 2014 at 13:50
  • 2
    \$\begingroup\$ The line "and also s = v/t (a simple equation to find displacement)" is just plain wrong. It's dimensionally inconsistent and contradicts the other given equations. \$\endgroup\$
    – r.e.s.
    Jan 1, 2014 at 14:15
  • 1
    \$\begingroup\$ it should be s=v*t \$\endgroup\$
    – Avi
    Jan 1, 2014 at 14:28

3 Answers 3

7
\$\begingroup\$

Maple, 41 chars

s:=i->solve({v=u+a*t,s=(u+v)*t/2,op(i)}):

(Formulas used: #1 and #3. These are sufficient to let Maple derive the rest.)

This defines a function s that takes in a set of initial conditions and solves the equations of motion for those initial values. For example, the command:

> s({ u = 1, v = 2, t = 3 });

outputs:

                          {a = 1/3, s = 9/2, t = 3, u = 1, v = 2}

Notes:

You generally need to specify three initial values for a unique solution; if you don't provide enough, Maple will try to give you a partial solution:

> s({ u = 1, v = 2 });

                                        3 t
                          {a = 1/t, s = ---, t = t, u = 1, v = 2}
                                         2

(Yes, that's an ASCII art fraction there. Even in text mode, Maple tries to typeset your math nicely.)

No solution is return for overspecified or otherwise unsolvable inputs; for example, s({ u = 1, v = 2, t = 0 }); outputs nothing.

If multiple solutions exist, Maple will generally try to return all of them:

> s({ u = 1, s = 0, a = -1 });

                          {a = -1, s = 0, t = 2, u = 1, v = -1},
                          {a = -1, s = 0, t = 0, u = 1, v = 1}

Sometimes, the output may contain RootOf() expressions, as in:

> s({ u = 10, s = 1, a = -1 });
                                                         2
                {a = -1, s = 1, t = RootOf(2 - 20 _Z + _Z , label = _L3), u = 10,
                                               2
                 v = 10 - RootOf(2 - 20 _Z + _Z , label = _L3)}

As far as Maple's concerned, this is a perfectly good solution, but if you want to turn it into something readable for us mere humans, use allvalues():

> allvalues(s({ u = 10, s = 1, a = -1 }));   
                                            1/2                 1/2
                {a = -1, s = 1, t = 10 - 7 2   , u = 10, v = 7 2   },
                                            1/2                  1/2
                {a = -1, s = 1, t = 10 + 7 2   , u = 10, v = -7 2   }

Alternatively, you can specify the initial conditions as floating-point values, e.g. u = 10.0, which will cause Maple to return purely numerical solutions instead:

> s({ u = 10.0, s = 1, a = -1 });

                {a = -1., s = 1., t = 0.1005050634, u = 10., v = 9.899494937},
                {a = -1., s = 1., t = 19.89949494, u = 10., v = -9.899494937}

Finally, note that, since Maple is a computer algebra system, it allows you to specify arbitrary equations in the input. For example, if we didn't care about the specific initial velocity, but only about the change in velocity, we could just specify e.g. v = u + 1 and get the partial solution:

> s({ v = u + 1 });

                {a = 1/t, s = t v - 1/2 t, t = t, u = v - 1, v = v}

We can even extend the physics model if we want, as in:

> s({ f = m * a, u = 0, f = 1, m = 10, t = 5 });

                {a = 1/10, f = 1, m = 10, s = 5/4, t = 5, u = 0, v = 1/2}
\$\endgroup\$
1
  • \$\begingroup\$ Nice. I especially like that equations 2, 4 and 5 are inferred from equations 1 and 3. \$\endgroup\$
    – Gareth
    Jan 1, 2014 at 16:28
3
\$\begingroup\$

Golf-Basic 84, 58 characters

Executed from a TI-84 calculator

i`Cp`S,V,U,A,T@C=1d`U+AT@C=2d`UT+.5AT²@C=1d`"V="@C=2d`"S="

Sample Run

?1
?S=UNKNOWN
?V=UNKNOWN
?U=5
?A=4
?T=2
V=
              13
\$\endgroup\$
1
\$\begingroup\$

GTB, 43

`C,S,V,U,A,T@C=1$~"V="~U+AT#~"S="~UT+.5AT²&

Sample Runs

Solve for V

?1
?UNKNOWN
?UNKNOWN
?5
?4
?2
V=
          13

Solve for S

?2
?UNKNOWN
?UNKNOWN
?5
?4
?2
S=
          18
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.