문제

The distance formula for polar coordinated is shown here: http://math.ucsd.edu/~wgarner/math4c/derivations/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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top