CS 378 Assignment 3: Physics and Code

Due: March 9, 2021.

Files: cs378.clj     formulas.clj     test3.clj     asgtest.java

The following functions that operate on trees should be recursive. Some useful functions that you may need are provided in the file cs378.clj, and you will need some of your functions from the previous assignments.

  1. Physical principles often are described by sets of equations. Since the solve function from the previous assignment is able to solve a single equation, we would like to extend it to solve problems involving multiple equations.

    Write a function (solveqns eqns vals v) that attempts to solve the list of equations eqns for variable v given an association list of values vals.

    1. If the desired variable v has a value defined in vals, the problem is solved, and the value can be returned.

    2. Otherwise, look through the list of equations to see if there is an equation that has exactly one unknown. If so, the equation can be solved for that variable using solve, and the value of the variable can be found using myevalb with vals. Add the new variable and value to vals and call solveqns recursively to try again.

    3. If all the equations have been examined and none of them can be solved, return nil.

    Use your program to answer the following questions. Input data is provided in the file formulas.clj.

    1. What is the terminal voltage of a battery with a current of 0.3 amp and internal resistance of 4 ohms and voltage of 12 volts ?
    2. What is the angular momentum of a circular motion with radius 4 m and mass 2 kg and velocity 3 m/s ?
    3. What is the magnification of a converging lens with subject distance = 6 cm and focal length = 9 cm ?
    4. What is the power of a lift with weight 700 nt and height 8 m and time 10 sec ?
    See http://www.cs.utexas.edu/users/novak/cgi/physdemod.cgi.

  2. A related task is to use a set of equations to write a program to find a desired variable given the values of other variables.

    1. Write a program (solveqnsc codelist equations knowns var) that is given a list of equations, a list of variables whose values are known, and a desired variable var. codelist is initially empty (nil or '()). If var is among the knowns, return the codelist. Otherwise, look for an equation that involves exactly one unknown and solve it for that unknown. cons the new equation onto the front of the codelist, add the unknown that was solved to the list of knowns, and try again, recursively. Note that solveqnsc will produce a list of equations in backwards order, since cons is used to add a new equation to the front of the list.

    2. solveqnsc may produce some equations that are valid but that are not needed in order to find the desired variable. Write a function (filtercode codelist needed) that removes unnecessary equations from a (backwards) equation list as follows.

      needed is a list of variables whose values are needed by equations that will appear later in the code; initially, needed will be a list of the single var that is the goal of the function.

      Go through the (backwards) equation list one equation at a time. If the lhs of the equation is a member of needed, then we need that equation, and it should be kept; in this case, any variables in the rhs of the equation will also be needed and should be added to needed (using union) for the recursive call. If the lhs of an equation is not a member of needed, that equation can be eliminated, and needed is unchanged. The variables in needed are called busy or live variables, and this process is called dead code elimination.

  3. Write a program (solvecode name equations inputs var) that is given the name of a desired function, a list of equations, a list of variables whose values are inputs, and a desired variable var. The result should be a list of strings that when printed form a program in Java. You may need to add a case to your tojava function to turn a call (expt x 2) into Math.pow(x,2) .

    1. cons a return line and closing bracket onto the (backwards) code list to form the end of the Java function.

    2. Produce a function header line (as a string) from the given name and inputs; we will assume that all variables are double:
         public static double foo(double x, double y, double z) {
      
      A useful function is (arglist inputs) that calls a helper function and produces a string of double and input names separated by commas for use as an argument list.

    3. You can print the result of solvecode using:
         (doseq [item strings] (println item))
      

    4. A test file asg3test.java is provided for testing. Copy your generated code into this file and test it in Java.