I have a working function that finds the derivative of a given function with respect to a given variable. I also have a simplify function (define (simplify expr) that takes the output of the differentiation function (define (diff x expr) and simplifies it into an easy to read form. I am trying to implement a system that allows the user to enter an expression that they would like to find the derivative for and return the simplified derivative. What do I need to change in the 6th line of code to make this work?
#racket
(display "Please enter an expression that you would like to differntiate: ")
(define expr (read))
(display expr)
(display " the derivative is ")
(display (simplify (diff 'x expr)))
(newline)
My simplify
and diff
functions
#lang racket/base
(define (diff x expr)
; differentiation function which gives derivative given a variable
(cond ((not (list? expr)) (if (equal? x expr) 1 0)) ; d/dx(x) = 1, d/dx(c) = 0
(else
(let ((operation (car expr)) ; expr is (operation u v)
(u (cadr expr))
(v (caddr expr)))
(case operation
((+) (list '+ (diff x u) (diff x v))) ; d/dx (u+v) = d/dx(u) + d/dx(v)
((-) (list '- (diff x u) (diff x v))) ; d/dx (u-v) = d/dx(u) - d/dx(v)
((*) (list '+ ; d/dx (u*v) = u * d/dxuv) + v * d/dx(u)
(list '* u (diff x v))
(list '* v (diff x u))))
((/) (list '/ (list '- ; power rule
(list '* v (diff x u))
(list '* u (diff x v)))))
((^) (list '* v (list '*
(list '^ u (- v 1))
(diff x u)))))))))
(define (simplify expr)
(if (not (list? expr))
expr
(let ((operation (car expr))
(a (simplify (cadr expr)))
(b (simplify (caddr expr))))
(case operation
((+) (cond ((and (number? a) (number? b)) (+ a b)) ;; use Racket's ability to add
((number? a)
(cond ((zero? a) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((-) (cond ((and (number? a) (number? b)) (- a b)) ;; use Racket's ability to subtract
((number? a)
(cond ((zero? a) (- (simplify b)))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((*) (cond ((and (number? a) (number? b)) (* a b)) ;; use Racket's ability to mulitpy
((number? a)
(cond ((zero? a) 0)
((= a 1) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 0)
((= b 1) (simplify a))
(else (list operation (simplify a)(simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((/) (cond ((and (number? a) (number? b)) (/ a b)) ;; use Racket's ability to divide
((number? a)
(cond ((zero? a) 0)
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (error "Divison by 0, statement undefined!"))
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else
(list operation (simplify a) (simplify b)))))
((^) (cond ((and (number? a) (number? b)) (expt a b)) ;; use Racket's ability to exponentiate
((number? a)
(cond ((= a 1) 1) ;; ((zero? a) 0) ;; depends on b [b < 0 then undefined]
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 1) ;; depends on a [a = 0 then undefined]
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))))))