39 replies to this topic
#1
Posted 21 January 2011 - 10:30 PM
Just an update, I will now be using the originally posted thread as an index to all the other events. I will still be linking to the previous and next topics at the beginning of each post as well.
Main Thread (Event Index) : http://forum.codecal...ing-events.html
Previous Event : http://forum.codecal...-event-1-a.html
Next Event : none (yet)
Title: Basic Kinematics
By: Skippy
Wait! Don't freak out! I know this says kinematics, but you aren't required to know any physics to do this activity. Just some algebra and programming skills, thats it. I promise.
Description: In this event you will be writing a program that solves equations. The equation you will be using is
d = vi * t + (1/2) * a * ( t ^ 2 ).
Where d is distance, vi is initial velocity, a is acceleration and t is time.
Since we have one equation with four variables, we are required to know any three variables to solve for the last variable.
This program must effectively:
Get the value of any three variables, then solve for the last variable and display the result. The three known variables must be dynamic. Meaning the user must be able to pick which three variables are known and which one is not known. It is up to you to figure out how to do this.
I am not posting requirements on this one, I want to see how you all decide to get the information from the user and display it back. You can use any method you want, command line arguments, file I/O, or terminal I/O (although all should atleast have terminal out). You can use classes, structures, or just have the whole solution in one function. Do whatever you think is the best way for you to do it. Spend some time on it though, make sure that the user is getting a nice interface and a fully functional program, and I want to read your source so try to keep it neat! Last event's participants did a great job at this.
Advanced:
For those interested in doing advanced functionality, I highly recommend coming up with a solution to this advanced section. Please make sure you code the original program first though, so you can help with other members that might have questions.
The four basic kinematic equations are:
Kinematic Equations and Problem-Solving
d = vi * t + (1/2) * a * t ^ 2
vf ^ 2 = vi ^ 2 + 2 * a * d
vf = vi + a * t
d = ( t / 2 ) * ( vi + vf )
where
d = distance
vi = initial velocity
vf = final velocity
t = time
a = acceleration
Notice that using these four equations, only one new variable was added (vf) yet 3 new functions were added. Also notice how each equation differs from all the others by exactly one variable.
d = vi * t + (1/2) * a * t ^ 2
d, vi, t, a
vf ^ 2 = vi ^ 2 + 2 * a * d
d, vi, vf, a
vf = vi + a * t
vf, vi, a, t
d = ( t / 2 ) * ( vi + vf )
d, vi, vf, a
With this new set of equations, the user can know exactly any three variables and solve for any other variable. So Instead of knowing three variables and solving for one unknown, a physicist can know three variables and solve for both unknowns.
Well in order to do this, the value obtained as the result of one equation will then be used in a different equation, etc.
For example lets say that we know d, vi, and a.
the program then needs to solve for t and vf.
since equation 1 has the three given values and t, t can be solved for in equation one.
Now the program can use any other equation to solve for vf.
Main Thread (Event Index) : http://forum.codecal...ing-events.html
Previous Event : http://forum.codecal...-event-1-a.html
Next Event : none (yet)
Title: Basic Kinematics
By: Skippy
Wait! Don't freak out! I know this says kinematics, but you aren't required to know any physics to do this activity. Just some algebra and programming skills, thats it. I promise.
Description: In this event you will be writing a program that solves equations. The equation you will be using is
d = vi * t + (1/2) * a * ( t ^ 2 ).
Where d is distance, vi is initial velocity, a is acceleration and t is time.
Since we have one equation with four variables, we are required to know any three variables to solve for the last variable.
This program must effectively:
Get the value of any three variables, then solve for the last variable and display the result. The three known variables must be dynamic. Meaning the user must be able to pick which three variables are known and which one is not known. It is up to you to figure out how to do this.
I am not posting requirements on this one, I want to see how you all decide to get the information from the user and display it back. You can use any method you want, command line arguments, file I/O, or terminal I/O (although all should atleast have terminal out). You can use classes, structures, or just have the whole solution in one function. Do whatever you think is the best way for you to do it. Spend some time on it though, make sure that the user is getting a nice interface and a fully functional program, and I want to read your source so try to keep it neat! Last event's participants did a great job at this.
Advanced:
For those interested in doing advanced functionality, I highly recommend coming up with a solution to this advanced section. Please make sure you code the original program first though, so you can help with other members that might have questions.
The four basic kinematic equations are:
Kinematic Equations and Problem-Solving
d = vi * t + (1/2) * a * t ^ 2
vf ^ 2 = vi ^ 2 + 2 * a * d
vf = vi + a * t
d = ( t / 2 ) * ( vi + vf )
where
d = distance
vi = initial velocity
vf = final velocity
t = time
a = acceleration
Notice that using these four equations, only one new variable was added (vf) yet 3 new functions were added. Also notice how each equation differs from all the others by exactly one variable.
d = vi * t + (1/2) * a * t ^ 2
d, vi, t, a
vf ^ 2 = vi ^ 2 + 2 * a * d
d, vi, vf, a
vf = vi + a * t
vf, vi, a, t
d = ( t / 2 ) * ( vi + vf )
d, vi, vf, a
With this new set of equations, the user can know exactly any three variables and solve for any other variable. So Instead of knowing three variables and solving for one unknown, a physicist can know three variables and solve for both unknowns.
Well in order to do this, the value obtained as the result of one equation will then be used in a different equation, etc.
For example lets say that we know d, vi, and a.
the program then needs to solve for t and vf.
since equation 1 has the three given values and t, t can be solved for in equation one.
Now the program can use any other equation to solve for vf.
|
|
|
#2
Posted 22 January 2011 - 04:44 PM
; Create four symbols (symbols are different than variables)
syms d v t a
; Define the formula in terms of the symbols
f = 'd = v*t + (a*t^2)/2';
w = input('d: ');
x = input('v: ');
y = input('a: ');
z = input('t: ');
; If 'w' has a value, replace the symbol 'd' with the value of 'w'
if(~isempty(w)) f = subs(f, d, w); end;
; If 'x' has a value, replace the symbol 'v' with the value of 'x'
if(~isempty(x)) f = subs(f, v, x); end;
; If 'y' has a value, replace the symbol 'a' with the value of 'y'
if(~isempty(y)) f = subs(f, a, y); end;
; If 'z' has a value, replace the symbol 't' with the value of 'z'
if(~isempty(z)) f = subs(f, t, z); end;
; At this point we have replaced the symbols with numeric values
; Now evaluate and simplify the equation.
simplify(f)
>> solver
d:
v: 1
a: 1
t: 1
ans =
d = 3/2
>> solver
d:
v:
a: 1
t: 1
ans =
d = v + 1/2
>> solver
d: 5
v: 1
a: 8
t:
ans =
t in {1, -5/4}
<3 MATLAB
Edited by John, 22 January 2011 - 07:05 PM.
#3
Posted 22 January 2011 - 05:38 PM
Dude I'm surprised you got it done in so little code can you comment on
syms d v t a
f = 'd = v*t + (a*t^2)/2';
and subs() please?
syms d v t a
f = 'd = v*t + (a*t^2)/2';
and subs() please?
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
#4
Posted 22 January 2011 - 07:04 PM
I've added comments to the code, for more information see MATLAB Programming/Symbolic Toolbox - Wikibooks, open books for an open world
And the only reason that it is short, is because the language is meant for mathematical operations.
And the only reason that it is short, is because the language is meant for mathematical operations.
#5
Posted 22 January 2011 - 07:08 PM
Wow, I will defiantly take a look at mat lab. I guess there is no "one best language". that is incredibly awesome compared to how I am doing it in c++
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
#6
Posted 22 January 2011 - 07:22 PM
In software engineering, there is no swiss-army knife. I know about twenty programming languages, and from my experience, there is definitely a right programming language for the job.
#7
Posted 23 January 2011 - 05:03 AM
Matlab is cheating :P
I actually found the hardest thing to find the t out of the function. I've done it literally tens of times while i still had math at school.
But now i couldn't see how to find x in a function like
30=x+x²
To all those like me:
(0=ax² + bx + c)

I selected the cell with the formula, just replace "WORTEL" by sqrt() for english versions.
2nd formula is the same, except that the first '+' is a '-'
Edit: and i'm stupid enough to not put the formula in the screencapture :glare:
Well, i hope the formula i pasted there is enough to solve it without having the formula of the cell :P
I actually found the hardest thing to find the t out of the function. I've done it literally tens of times while i still had math at school.
But now i couldn't see how to find x in a function like
30=x+x²
To all those like me:
(0=ax² + bx + c)

I selected the cell with the formula, just replace "WORTEL" by sqrt() for english versions.
2nd formula is the same, except that the first '+' is a '-'
Edit: and i'm stupid enough to not put the formula in the screencapture :glare:
Well, i hope the formula i pasted there is enough to solve it without having the formula of the cell :P
Edited by wim DC, 23 January 2011 - 05:57 AM.
#8
Posted 23 January 2011 - 05:48 AM
Java:
------
------
package event.event2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Calc {
private Map<String, Double> vars;
private String unknown;
[B][COLOR="blue"]public Calc() {[/COLOR][/B]
vars = new HashMap<String, Double>();
vars.put("a", 0.0);
vars.put("d", 0.0);
vars.put("vi", 0.0);
vars.put("t", 0.0);
}
[COLOR="blue"][B]private void solveD() {[/B][/COLOR]
vars.put("d", vars.get("vi") * vars.get("t") + 0.5 * vars.get("a") * Math.pow(vars.get("t"), 2));
}
[COLOR="blue"][B]private void solveVi() {[/B][/COLOR]
vars.put("vi", (vars.get("d") - 0.5 * vars.get("a") * Math.pow(vars.get("t"), 2)) / vars.get("t"));
}
[COLOR="blue"][B]private void solveA() {[/B][/COLOR]
vars.put("a", (vars.get("d") - vars.get("vi") * vars.get("t")) / Math.pow(vars.get("t"), 2));
}
[COLOR="blue"][B]private void solveT1() {[/B][/COLOR]
vars.put("t", (-1*vars.get("vi") + Math.sqrt(Math.pow(vars.get("vi"), 2) - 4 * 0.5 * vars.get("a") * (-1*vars.get("d")))) / (2 * 0.5 * vars.get("a")));
}
[COLOR="blue"][B]private void solveT2() {[/B][/COLOR]
vars.put("t", (-1*vars.get("vi") - Math.sqrt(Math.pow(vars.get("vi"), 2) - 4 * 0.5 * vars.get("a") * (-1*vars.get("d")))) / (2 * 0.5 * vars.get("a")));
}
[COLOR="blue"][B]private boolean askVars() {[/B][/COLOR]
Scanner scanner = new Scanner(System.in);
ArrayList<String> chosen = new ArrayList<String>();
while(chosen.size() != 3) {
System.out.println("Please give a symbol(d, vi, t a):");
String choice = scanner.nextLine();
if (vars.containsKey(choice) && !chosen.contains(choice)) {
System.out.println("Please give a value for " + choice);
vars.put(choice, scanner.nextDouble());
chosen.add(choice);
scanner.nextLine();
} else {
System.out.println("You already gave this variable a value, dummy");
}
}
for (String key : vars.keySet()) {
if (!chosen.contains(key)) {
unknown = key;
}
}
return chosen.size() == 3;
}
[COLOR="blue"][B]private void getResult() {[/B][/COLOR]
if (unknown.equals("d")) {
solveD();
System.out.println("d = " + vars.get("d"));
} else if (unknown.equals("vi")) {
solveVi();
System.out.println("vi = " + vars.get("vi"));
} else if (unknown.equals("a")) {
solveA();
System.out.println("a = " + vars.get("a"));
} else if (unknown.equals("t")) {
solveT1();
System.out.println("t1 = " + vars.get("t"));
solveT2();
System.out.println("t2 = " + vars.get("t"));
}
}
[COLOR="blue"][B]public static void main(String[] args) {[/B][/COLOR]
Calc calc = new Calc();
calc.askVars();
calc.getResult();
}
}
#9
Posted 23 January 2011 - 06:24 PM
Here is another ANSI C attempt:
#include <stdio.h>
#include <math.h>
void inputSymbols(void);
typedef struct {
double m_vD;
double m_vVI;
double m_vT;
double m_vA;
char unknown;
} KFORMULA;
KFORMULA arrVars[] =
{
{ 0, 0, 0, 0, 'x' },
};
int main(int argc, char *argv[])
{
KFORMULA* pFormula = arrVars;
inputSymbols();
switch(pFormula->unknown) {
case 'd':
printf("d = %0.3lf",
pFormula->m_vVI * pFormula->m_vT + (pFormula->m_vA * powf(pFormula->m_vT, 2) / 2));
break;
case 'v':
printf("vi = %0.3lf",
pFormula->m_vD - 0.5 * pFormula->m_vA * powf(pFormula->m_vT, 2) / pFormula->m_vT);
break;
case 't':
printf("t = %0.3lf",
-1 * pFormula->m_vVI - sqrt(powf(pFormula->m_vVI, 2) - 4 * 0.5 * pFormula->m_vA * (-1 * pFormula->m_vD)) * 2 * 0.5 * pFormula->m_vD);
break;
case 'a':
printf("a = %0.3lf",
pFormula->m_vD - pFormula->m_vVI * pFormula->m_vT / powf(pFormula->m_vT, 2));
break;
}
return 0;
}
void inputSymbols(void) {
KFORMULA* pFormula = arrVars;
double tmp;
printf("Enter values for formula. Type letter X for unknown.\n");
printf("d: ");
if(scanf("%lf", &tmp) != 1) {
pFormula->unknown = 'd';
} else {
pFormula->m_vD = tmp;
}
getchar();
printf("vi: ");
if(scanf("%lf", &tmp) != 1) {
pFormula->unknown = 'v';
} else {
pFormula->m_vVI = tmp;
}
getchar();
printf("t: ");
if(scanf("%lf", &tmp) != 1) {
pFormula->unknown = 't';
} else {
pFormula->m_vT = tmp;
}
getchar();
printf("a: ");
if(scanf("%lf", &tmp) != 1) {
pFormula->unknown = 'a';
} else {
pFormula->m_vA = tmp;
}
}
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#10
Posted 24 January 2011 - 01:15 AM
Good job everyone with the code. Looks like everyone is going the same route though (except for the example with matlab).
The code in most of the examples seems very static. The entire program is built around one goal, and nothing could be used in another project.
If I were to change the functions being used in the requirements, almost the entire program would have to be rewritten.
Has anyone tried to do something more dynamic? Make an "equation solver" class, or something of the sort?
The code in most of the examples seems very static. The entire program is built around one goal, and nothing could be used in another project.
If I were to change the functions being used in the requirements, almost the entire program would have to be rewritten.
Has anyone tried to do something more dynamic? Make an "equation solver" class, or something of the sort?
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
#11
Posted 24 January 2011 - 04:41 AM
Skippy said:
Good job everyone with the code. Looks like everyone is going the same route though (except for the example with matlab).
The code in most of the examples seems very static. The entire program is built around one goal, and nothing could be used in another project.
If I were to change the functions being used in the requirements, almost the entire program would have to be rewritten.
Has anyone tried to do something more dynamic? Make an "equation solver" class, or something of the sort?
The code in most of the examples seems very static. The entire program is built around one goal, and nothing could be used in another project.
If I were to change the functions being used in the requirements, almost the entire program would have to be rewritten.
Has anyone tried to do something more dynamic? Make an "equation solver" class, or something of the sort?
For simple functions like y=z+w, sure no problem.
- Oh the same variable can occur multiple times
+ Okay, this gets a bit more annoying but yea, I think that can work
- By the way, that variable that occurs more than once can have different powers (², ³,...)
+ K, that's it i'm bailing :P
Really, it gets so rediculously complex when the powers kick in.
Unless I would just start number-guessing untill my equatation is (close to) correct.
That would be a possible "solution", however with equations of a degree greater than 2, let's say 3, it COULD have 3 intersections where y=0, but it could also have only 1. And then it gets hard to know whether i need to continue the number-guessing or not.
#12
Posted 24 January 2011 - 05:53 AM
It does not get "ridiculously complex" when powers kick. You go through it like you would on paper, determine your known and unknown variables, solving for variables and then substituting to solve for your unknown values.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









