In my calculus BC class, we had the option of taking time out of class to go work on building a calculator that can graph original functions and their derivatives. The entire process would have taken a lot less time if I didn’t have to write my own parser on converting a string to an equation, but nonetheless, it was worth the whole work because I was able to overcome a very difficult challenge in my short computer science career.
The Parser converted infix notation, the one that we are typically used to, to postfix notation and then solved the entire thing. Infix notation is where all the operators are in between the operands, and the equation uses parenthesis to define its orders of operation. In postfix notation, the operands are both next to each other and the operator comes after the two variables that it operates on. Depending on the position of the operator, it is easy to determine how the order of operations works in the system.
To set up the graphics program, I used the open source Stanford ACM graphics library. For me, it facilitated the process because I had been using it for most of the summer, and it made the entire process much smoother for me. I had to create a backdrop and draw my own axes, and then create a separate panel on the side where the user can input the function and set the left and right bounds (the screen will always remain a square). Then I let the user define their choice of color using an RGB style.
Plotting the points took an entire algorithm to shift the points onto the screen, but eventually after trial and error, it came out well. I used multiple classes that each handled different aspects of the process, and I was able to completely graph both the first and second derivative by taking very small intervals and finding the slope of each curve.
There still are a few bugs that I need to work out. First, each general function, such as sin(x+2) has its own way of solving within the parenthesis. Instead, the function could simply be recursive and return a numerical value within the function and then solve the function of the number. Next, the algorithm doesn’t continue after the function runs a first time.
There are also ways to make the calculator more aesthetically appealing. However, this is a great first step towards building useful applications in computer science for years to come. And to keep up with the theme, I now am continuing working on my two projects from Carnegie Mellon.