Example #1
0
        private void btnAnalyticalMcLaurin_Click(object sender, EventArgs e)
        {
            int  n;
            bool isNEntered = int.TryParse(nPolynomialTbx.Text, out n);

            if (isNEntered)
            {
                // First parse the expression, if any is entered.
                ParseAndPlotExpressions("Please enter an expression to use as basis for the MacLaurin Polynomial.");
                MacLaurinPolynomial macLaurinPolynomial = new MacLaurinPolynomial(expressionRoot.Copy(), n);
                // Create a graph of the highest order MacLaurin polynomial.
                Operand highestOrderPolynomial = macLaurinPolynomial.GetNthMacLaurinPolynomial(n);
                CreateGraphOfExpression(highestOrderPolynomial, "MacLaurinPolynomialAnalytically");

                // Plot all but the last which we do after the loop.
                for (int i = 0; i < n; i++)
                {
                    calculator = new CalculateForXHandler(macLaurinPolynomial.GetNthMacLaurinPolynomial(i).Calculate);
                    CreateChart($"MacLaurin Polynomial of degree {i}");
                }
                // Since we already used the simplified expression tree of the n-th order MacLaurin polynomial plot it after the loop.
                calculator = new CalculateForXHandler(highestOrderPolynomial.Calculate);
                CreateChart($"MacLaurin Polynomial of degree {n}");
            }
            else
            {
                MessageBox.Show("Please enter a valid N for the nth order MacLaurin Polynomial.");
            }
        }
Example #2
0
        private void differenceQuotientBtn_Click(object sender, EventArgs e)
        {
            ParseAndPlotExpressions("Please enter an expression to parse.");

            // Calculate the derivative and simplify all the excessive x^1, x*0 x+0, etc.
            // before creating a chart and graph of it.
            derivativeExpressionRoot = expressionRoot.Differentiate();
            derivativeExpressionRoot = derivativeExpressionRoot.Simplify();

            // Display the derivative in text.
            derivativeLb.Text = $"Derivative: {derivativeExpressionRoot.ToString()}";

            // Plot Newton's difference quotient as well.
            calculator = new CalculateForXHandler(DifferenceQuotient);
            CreateChart("Difference Quotient");

            CreateGraphOfExpression(derivativeExpressionRoot, "derivative");
        }
Example #3
0
        private void ParseAndPlotExpressions(string errorMessage)
        {
            // Reset
            ResetAll();

            // Try to obtain min and max values for the axis.
            ObtainAxisBoundaries();

            // For now the entered expression must always be parsed first.
            Parse(new NoExpressionEnteredException(errorMessage));

            // For now always make a plot of the entered expression
            // as it is used for regular parsing, the derivative and for the integral.

            // In the future we could sub an event and unsub this basic method
            // for the taylor polynomials etc if we need to.
            calculator = new CalculateForXHandler(expressionRoot.Calculate);
            CreateChart("Expression");
        }
Example #4
0
 private void plotPolynomialBtn_Click(object sender, EventArgs e)
 {
     polynomialCoordinatesPanel.Visible = false;
     if (polynomialCoordinatesList != null && polynomialCoordinatesList.Count > 0)
     {// Create a new solver based on the obtained coordinates.
         SystemSolver s = new SystemSolver(polynomialCoordinatesList);
         // Get the expression representation of the system.
         // And simplify it where possible.
         Operand poly = s.GetPolynomial().Simplify();
         // Put the expression string in the respective label.
         expressionLb.Text = $"Expression: {poly.ToString()}";
         // Create a graph of the polynomial.
         CreateGraphOfExpression(poly, "n-polynomial");
         // Plot the polynomial.
         calculator = new CalculateForXHandler(poly.Calculate);
         CreateChart("n-polynomial");
         // Set flag back to false not allowing any more extra coordinates.
         polynomialCoordinates = false;
     }
     else
     {
         MessageBox.Show("Please enter coordinates first.");
     }
 }
Example #5
0
        private void processBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ParseAndPlotExpressions("Please enter an expression to parse.");

                if (parseRbtn.Checked)
                {
                    CreateGraphOfExpression(expressionRoot, "expression");
                }
                else if (differentiateRbtn.Checked)
                {
                    // Calculate the derivative and simplify all the excessive x^1, x*0 x+0, etc.
                    // before creating a chart and graph of it.
                    derivativeExpressionRoot = expressionRoot.Differentiate();
                    derivativeExpressionRoot = derivativeExpressionRoot.Simplify();

                    // Display the derivative in text.
                    derivativeLb.Text = $"Derivative: {derivativeExpressionRoot.ToString()}";

                    // Assign the method to calculate the x'es for the derivative of the expression.
                    calculator = new CalculateForXHandler(derivativeExpressionRoot.Calculate);
                    CreateChart("Analytical Derivative");

                    CreateGraphOfExpression(derivativeExpressionRoot, "derivative");
                }
                else if (integralRbtn.Checked)
                {
                    bool isADouble = double.TryParse(intervalATbx.Text, out lower);
                    bool isBDouble = double.TryParse(intervalBTbx.Text, out upper);

                    if (!isADouble || !isBDouble)
                    {
                        throw new InvalidArgumentTypeException("Please enter valid numbers for the range from a through b");
                    }
                    calculator = new CalculateForXHandler(expressionRoot.Calculate);
                    expressionChart.Refresh(); // Ensure that the paint event is triggrred.
                    areaLb.Text += $"{Math.Round(sum, 2)} square units.";
                }
            }
            catch (InvalidExpressionException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (InvalidNumberException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (InvalidArgumentTypeException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (InvalidFactorialArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (NoExpressionEnteredException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (UndefinedExpressionException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (DivideByZeroException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }