开发者

Equation Solving Algorithm

开发者 https://www.devze.com 2023-02-06 06:36 出处:网络
Hello I am trying to develop a physics problem solver. The program specifically will be written for android but that\'s not important. I have these three equations that will be familiar to anyone who

Hello I am trying to develop a physics problem solver. The program specifically will be written for android but that's not important. I have these three equations that will be familiar to anyone who knows physics.

vf = vi + a*t

y= vi*t + .5*t^2

vf^2=vi^2 + 2*a*y

I would like to develop an开发者_StackOverflow application that uses these three equations to solve the other values that the user wants given any known variables that the user has. This is where I run into problems. If I knew each time what variables the user would have, I would be fine. But since I don't, I need my program to be able to rearrange the equations and substitute individual variables if necessary to evaluate them.

I know the answer is probably that I will need to write out a separate set of code for each combination of user input, but I would appreciate it if anyone knew another way so I don't need to write out code all 100 or so variations.

Thanks for any help.


if(vfField.getText() != "") && (viField.getText() != "") && (aField.getText() != "") && (tField.getText() != "") {
printf("you've already solved it!!") 
}

if(vfField.getText() != "") && (viField.getText() != "") && (aField.getText() != "") && (tField.getText() == "") {
time = vf / (vi + a)
printf(time) 
}

etc.. :)


Writing your own program that can rearrange equations is extremely time consuming in my experience (weeks of coding). You should avoid it in this case.

I would however also not suggest coding permutations manually, as then there's a lot of code to check for mistakes. Like missing a from the second equation in the question. Also it makes it hard to add new equations.

I think you should use an external equation solver program like maxima, matlab, mathematika etc. Feed all the possible combinations unknowns into them to solve for with the 2 existing equations. (the third equation is dependent on the first 2, so you'd leave it)

The maxima code would look like this

solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vi,vf]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vi,y]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vi,t]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vi,a]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vf,y]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vf,t]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [vf,a]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [y,t]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [y,a]);
solve([vf = vi + a*t, y= vi*t + .5*a*t^2], [t,a]);

As there are quadratics involved, you get lots of +- answers, with many obviously incorrect (like returning negative time). You could choose the correct +- solution by going through manually as there are not many.

Or alternatively, the automatic solution would be to start with example numbers, solve the original equations, and discard any solution equation that does not give the same numbers.

0

精彩评论

暂无评论...
验证码 换一张
取 消