Matlab Function to Calculate Roots
This guide explains how to write MATLAB functions to calculate roots of equations, including polynomial roots, quadratic roots, and numerical methods. We'll cover the built-in MATLAB functions, create custom functions, and demonstrate practical examples.
Basic MATLAB Functions for Roots
MATLAB provides several built-in functions to find roots of equations. The most commonly used functions are:
roots()- Finds all roots of a polynomialfzero()- Finds a root of a function of one variablefsolve()- Solves systems of nonlinear equations
These functions use different algorithms to find roots, and the choice depends on the type of equation you're working with.
Formula Overview
The general approach to finding roots in MATLAB involves:
- Defining the equation or polynomial
- Using an appropriate MATLAB function
- Analyzing the results
Calculating Polynomial Roots
For polynomials, MATLAB's roots() function is particularly useful. It finds all roots of a polynomial with real coefficients.
Example: Find the roots of the polynomial x³ - 6x² + 11x - 6 = 0
MATLAB Code Example
% Define the polynomial coefficients
p = [1 -6 11 -6];
% Find the roots
r = roots(p);
% Display the roots
disp('The roots are:');
disp(r);
The output will show the roots of the polynomial. Note that MATLAB may return complex roots even for polynomials with real coefficients.
Note
The roots() function works best for polynomials of degree 10 or less. For higher-degree polynomials, consider using numerical methods.
Quadratic Equation Roots
For quadratic equations (ax² + bx + c = 0), MATLAB provides the quadraticFormula() function or you can implement it directly.
Quadratic Formula
The roots of a quadratic equation are given by:
x = [-b ± √(b² - 4ac)] / (2a)
Example: Find the roots of x² - 5x + 6 = 0
MATLAB Code Example
% Define coefficients
a = 1;
b = -5;
c = 6;
% Calculate discriminant
discriminant = b^2 - 4*a*c;
% Calculate roots
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
% Display results
fprintf('Root 1: %.2f\n', root1);
fprintf('Root 2: %.2f\n', root2);
Numerical Methods for Roots
For more complex equations, numerical methods like the Newton-Raphson method can be implemented in MATLAB.
Newton-Raphson Method
The iterative formula is:
xₙ₊₁ = xₙ - f(xₙ)/f'(xₙ)
Example: Find the root of eˣ - 2x = 0 near x = 1
MATLAB Code Example
% Define the function and its derivative
f = @(x) exp(x) - 2*x;
df = @(x) exp(x) - 2;
% Initial guess
x0 = 1;
% Tolerance and maximum iterations
tol = 1e-6;
max_iter = 100;
% Newton-Raphson iteration
x = x0;
for i = 1:max_iter
x_new = x - f(x)/df(x);
if abs(x_new - x) < tol
break;
end
x = x_new;
end
% Display result
fprintf('Approximate root: %.6f\n', x);
Note
Numerical methods require careful selection of initial guesses and may not always converge to a solution.
Example MATLAB Functions
Here are some complete MATLAB functions for calculating roots:
Function to Find Polynomial Roots
function r = findPolynomialRoots(coeffs)
% Finds all roots of a polynomial given its coefficients
% Input: coeffs - vector of polynomial coefficients [a_n ... a_0]
% Output: r - vector of roots
r = roots(coeffs);
end
Function to Find Quadratic Roots
function [root1, root2] = quadraticRoots(a, b, c)
% Calculates roots of a quadratic equation ax² + bx + c = 0
% Inputs: a, b, c - coefficients of the quadratic equation
% Outputs: root1, root2 - the two roots
discriminant = b^2 - 4*a*c;
if discriminant >= 0
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
else
root1 = complex(-b/(2*a), sqrt(abs(discriminant))/(2*a));
root2 = complex(-b/(2*a), -sqrt(abs(discriminant))/(2*a));
end
end
Frequently Asked Questions
What is the difference between roots() and fzero() in MATLAB?
The roots() function finds all roots of a polynomial, while fzero() finds a single root of a function of one variable. roots() is typically used for polynomials, while fzero() is better for more general functions.
How do I handle complex roots in MATLAB?
MATLAB automatically handles complex roots. When you use functions like roots() or fzero(), they will return complex numbers if the equation has complex roots. You can use the real() and imag() functions to extract the real and imaginary parts.
What should I do if my polynomial has a high degree?
For high-degree polynomials, consider using numerical methods or specialized algorithms. MATLAB's roots() function may not be accurate for polynomials of degree 10 or higher. You might need to use root-finding algorithms like the Jenkins-Traub method.
How can I verify the roots I found are correct?
You can verify roots by plugging them back into the original equation. For example, if you found a root x = r, then f(r) should be very close to zero. You can also use MATLAB's polyval() function to evaluate the polynomial at the root.