class: center, middle, inverse, title-slide .title[ # Solving Polynomial Equations using Julia ] .subtitle[ ## Julia Workshop ] --- <style type="text/css"> body{ font-size: 20pt; } </style> Julia offers several ways to solve polynomial equations, each with its strengths and weaknesses. Here's a breakdown of the common methods and when to use them: **1. Using the `roots` Function (For Polynomials in Standard Form):** The `roots` function from the `Polynomials` package is the most straightforward way to find all roots (including complex roots) of a polynomial expressed in standard form (e.g., `ax^2 + bx + c`). --- ```julia using Polynomials # Define the polynomial (e.g., x^2 - 5x + 6) p = Polynomial([6, -5, 1]) # Coefficients in ascending order (constant term first) # Find the roots r = roots(p) println(r) # Output: [2.0, 3.0] # Example with complex roots: x^2 + 1 p_complex = Polynomial([1, 0, 1]) r_complex = roots(p_complex) println(r_complex) # Output: [-1.0im, 1.0im] ``` --- **Key Points about `roots`:** * **Standard Form:** The polynomial must be in standard form (coefficients arranged in ascending order of powers). * **All Roots:** It finds all roots, including real and complex roots. * **Numerical:** The roots are found numerically. * **Package:** You need to install the `Polynomials` package: `] add Polynomials` **2. Using `solve` (For Symbolic Solutions - Limited):** The `solve` function from the `Symbolics` package can sometimes find symbolic solutions to polynomial equations. However, symbolic solutions are often only possible for relatively simple polynomials. For higher-degree polynomials or more complex equations, `solve` might return a symbolic representation of the roots or fail to find a closed-form solution. --- ```julia using Symbolics @variables x # Define the equation (e.g., x^2 - 5x + 6 = 0) eq = x^2 - 5x + 6 ~ 0 # ~ is used for defining equations # Solve the equation sol = solve(eq, x) println(sol) # Output: [2, 3] # Example where symbolic solutions may not be feasible eq_complex = x^5 + x + 1 ~ 0 sol_complex = solve(eq_complex, x) println(sol_complex) # Symbolic representation of roots, likely not a closed-form solution. ``` --- **Key Points about `solve`:** * **Symbolic:** It attempts to find symbolic solutions. * **Limited:** Symbolic solutions are not always possible. * **Package:** You need to install the `Symbolics` package: `] add Symbolics` **3. Numerical Solvers (For General Equations):** For equations that are not strictly polynomials or when symbolic solutions are not feasible, you can use numerical solvers from packages like `NLsolve` or `Roots`. These solvers can find approximate solutions to a wide range of equations. --- ### using `NLsolve` ```julia using NLsolve # Example: Find a root of x^3 - 2x - 5 = 0 (Newton-Raphson method) f(x) = x^3 - 2x - 5 df(x) = 3x^2 - 2 # Derivative result = nlsolve((x) -> (f(x), df(x)), 3.0) # Start with an initial guess (3.0) root = result.zero println(root) ``` --- ### using `Roots` ```julia # Using Roots.jl for polynomial equations. using Roots f(x) = x^3 - 2x - 5 root = find_zero(f, 3.0) # Find a zero near x = 3 println(root) #Find all roots using polynomial interpolation. f_poly(x) = Polynomial([5,2,0,1]) # Coefficients in ascending order. roots_poly = find_zeros(f_poly,-3.0,3.0) println(roots_poly) ``` --- **Key Points about Numerical Solvers:** * **Numerical:** They find approximate solutions. * **General:** They can handle a wider range of equations, not just polynomials. * **Initial Guess:** Many numerical solvers require an initial guess for the solution. * **Packages:** You might need to install packages like `NLsolve` (`] add NLsolve`) or `Roots` (`] add Roots`). **Which Method to Choose:** * **`roots` (from `Polynomials`):** Best for finding all roots of polynomials in standard form. * **`solve` (from `Symbolics`):** Use this if you need symbolic solutions and the polynomial is simple enough. * **Numerical Solvers (`NLsolve`, `Roots`):** Use these for general equations (including polynomials) when symbolic solutions are not available or practical. `Roots` is particularly well-suited for finding zeros of functions, including polynomials. --- ### Summary For most undergraduate probability problems involving polynomials, the `roots` function from the `Polynomials` package will be the most convenient and efficient. If you have more complex equations or need symbolic solutions, then `solve` or numerical solvers would be more appropriate.