Need help putting radial distant into reverse polish notion for a lisp program
The distance formula for polar coordinated is shown here: http://math.ucsd.edu/~wgarner/math4c/derivat开发者_开发百科ions/distance/distancepolar.htm
I've tried doing it this way:
(defun distant-formula (r1 r2 t1 t2)
(setq d ( sqrt (*(-(+ (expt r1 2)(expt r2 2))(* 2 r1 r2))
(cos(- t1 t2))))))
but the extra parenthesis between the subtraction and multiplication changes the formula, however I'm not quite sure how to do it without correctly, any help would be appreciated.
The variable D in your code is of no use. You should not assign values to undefined variables. Lisp also returns the value of the computation. That makes such an assignment especially useless.
You can write the formula like this:
(defun distant-formula (r1 r2 t1 t2)
(+ (expt r1 2)
(expt r2 2)
(* -2 r1 r2 (cos (- t1 t2)))))
Note that the layout of the code over three lines makes it easier to understand the formula, than using just one line. Use the automatic indentation of the editor to help with the layout.
Looking at the original formula:
sqrt(r1**2 + r2**2 - 2 * r1 * r2 * cos(t1 - t2))
You want to work from the outside to the inside:
(sqrt (+ (* r1 r1) (* r1 r1) (* -2 r1 r2 (cos (- t1 t2)))))
You need to be careful about the precedence of the operators. One way to do the conversion is to all all of the implicit parentheses in infix notation (so a * b + c
goes to ((a * b) + c)
) and then rearrange so the operators are at the front.
精彩评论