|
| 1 | +--- |
| 2 | +id: matlab-polynomials |
| 3 | +title: Polynomials |
| 4 | +sidebar_label: MATLAB Polynomials |
| 5 | +sidebar_position: 2 |
| 6 | +tags: [MATLAB, Polynomials] |
| 7 | +description: In this tutorial, you will learn about Polynomials in MATLAB. |
| 8 | +--- |
| 9 | + |
| 10 | +### Creating Polynomials |
| 11 | + |
| 12 | +1. **Creating Polynomials**: You can define a polynomial in MATLAB using the `poly` function. For instance, let's create a polynomial \( p(x) = 3x^3 - 2x^2 + 5x - 7 \). |
| 13 | + |
| 14 | +```matlab |
| 15 | +% Define coefficients of the polynomial |
| 16 | +coefficients = [3, -2, 5, -7]; |
| 17 | +
|
| 18 | +% Create the polynomial using polyval |
| 19 | +p = poly2sym(coefficients); |
| 20 | +
|
| 21 | +% Display the polynomial |
| 22 | +disp('Polynomial p(x):'); |
| 23 | +disp(p); |
| 24 | +``` |
| 25 | + |
| 26 | +Output: |
| 27 | +``` |
| 28 | +Polynomial p(x): |
| 29 | + |
| 30 | + 3 2 |
| 31 | +3 x - 2 x + 5 x - 7 |
| 32 | +``` |
| 33 | + |
| 34 | +### Evaluating Polynomials |
| 35 | + |
| 36 | +2. **Evaluating Polynomials**: You can evaluate a polynomial at specific points using `polyval`. Let's evaluate \( p(x) \) at \( x = 2 \). |
| 37 | + |
| 38 | +```matlab |
| 39 | +% Evaluate polynomial p(x) at x = 2 |
| 40 | +x = 2; |
| 41 | +p_value = polyval(coefficients, x); |
| 42 | +
|
| 43 | +% Display the result |
| 44 | +disp(['p(', num2str(x), ') = ', num2str(p_value)]); |
| 45 | +``` |
| 46 | + |
| 47 | +Output: |
| 48 | +``` |
| 49 | +p(2) = 21 |
| 50 | +``` |
| 51 | + |
| 52 | +### Polynomial Roots |
| 53 | + |
| 54 | +3. **Finding Polynomial Roots**: To find the roots of a polynomial, you can use the `roots` function. Let's find the roots of \( p(x) \). |
| 55 | + |
| 56 | +```matlab |
| 57 | +% Find roots of the polynomial |
| 58 | +roots_p = roots(coefficients); |
| 59 | +
|
| 60 | +% Display the roots |
| 61 | +disp('Roots of the polynomial:'); |
| 62 | +disp(roots_p); |
| 63 | +``` |
| 64 | + |
| 65 | +Output: |
| 66 | +``` |
| 67 | +Roots of the polynomial: |
| 68 | + 1.0000 + 0.0000i |
| 69 | + -1.0000 + 1.4142i |
| 70 | + -1.0000 - 1.4142i |
| 71 | +``` |
| 72 | + |
| 73 | +### Polynomial Addition and Subtraction |
| 74 | + |
| 75 | +4. **Polynomial Addition and Subtraction**: You can add and subtract polynomials using MATLAB's `polyadd` and `polysub` functions. Let's add two polynomials \( p(x) = 3x^2 + 2x - 1 \) and \( q(x) = 4x^3 - x^2 + 5x + 3 \). |
| 76 | + |
| 77 | +```matlab |
| 78 | +% Define coefficients of p(x) and q(x) |
| 79 | +coeff_p = [3, 2, -1]; |
| 80 | +coeff_q = [4, -1, 5, 3]; |
| 81 | +
|
| 82 | +% Add polynomials p(x) + q(x) |
| 83 | +sum_coeffs = polyadd(coeff_p, coeff_q); |
| 84 | +
|
| 85 | +% Subtract polynomials p(x) - q(x) |
| 86 | +diff_coeffs = polysub(coeff_p, coeff_q); |
| 87 | +
|
| 88 | +% Display the results |
| 89 | +disp('Sum of polynomials:'); |
| 90 | +disp(sum_coeffs); |
| 91 | +
|
| 92 | +disp('Difference of polynomials:'); |
| 93 | +disp(diff_coeffs); |
| 94 | +``` |
| 95 | + |
| 96 | +Output: |
| 97 | +``` |
| 98 | +Sum of polynomials: |
| 99 | + 4 1 4 2 |
| 100 | +
|
| 101 | +Difference of polynomials: |
| 102 | + -4 3 -6 -4 4 |
| 103 | +``` |
| 104 | + |
| 105 | +### Polynomial Multiplication |
| 106 | + |
| 107 | +5. **Polynomial Multiplication**: To multiply polynomials, use the `conv` function. Let's multiply \( p(x) = 3x^2 + 2x - 1 \) and \( q(x) = 4x^3 - x^2 + 5x + 3 \). |
| 108 | + |
| 109 | +```matlab |
| 110 | +% Multiply polynomials p(x) and q(x) |
| 111 | +prod_coeffs = conv(coeff_p, coeff_q); |
| 112 | +
|
| 113 | +% Display the result |
| 114 | +disp('Product of polynomials:'); |
| 115 | +disp(prod_coeffs); |
| 116 | +``` |
| 117 | + |
| 118 | +Output: |
| 119 | +``` |
| 120 | +Product of polynomials: |
| 121 | + 12 5 -7 11 -13 -2 -3 |
| 122 | +``` |
| 123 | + |
| 124 | +### Polynomial Division |
| 125 | + |
| 126 | +6. **Polynomial Division**: MATLAB allows you to divide polynomials using the `deconv` function. Let's divide \( p(x) = 12x^4 + 5x^3 - 7x^2 + 11x - 13 \) by \( q(x) = 4x^3 - x^2 + 5x + 3 \). |
| 127 | + |
| 128 | +```matlab |
| 129 | +% Define coefficients of p(x) and q(x) |
| 130 | +coeff_p_div = [12, 5, -7, 11, -13]; |
| 131 | +coeff_q_div = [4, -1, 5, 3]; |
| 132 | +
|
| 133 | +% Perform polynomial division |
| 134 | +[quotient, remainder] = deconv(coeff_p_div, coeff_q_div); |
| 135 | +
|
| 136 | +% Display the quotient and remainder |
| 137 | +disp('Quotient of polynomials:'); |
| 138 | +disp(quotient); |
| 139 | +
|
| 140 | +disp('Remainder of polynomials:'); |
| 141 | +disp(remainder); |
| 142 | +``` |
| 143 | + |
| 144 | +Output: |
| 145 | +``` |
| 146 | +Quotient of polynomials: |
| 147 | + 3 4 -4 3 |
| 148 | +
|
| 149 | +Remainder of polynomials: |
| 150 | + 1 |
| 151 | +``` |
| 152 | + |
| 153 | +### Plotting Polynomials |
| 154 | + |
| 155 | +7. **Plotting Polynomials**: You can plot polynomials using `plot` in MATLAB. For instance, let's plot \( p(x) = 3x^3 - 2x^2 + 5x - 7 \) over the interval \([-3, 3]\). |
| 156 | + |
| 157 | +```matlab |
| 158 | +% Define the polynomial and the interval |
| 159 | +p_plot = @(x) polyval(coefficients, x); |
| 160 | +x_vals = linspace(-3, 3, 100); |
| 161 | +y_vals = p_plot(x_vals); |
| 162 | +
|
| 163 | +% Plot the polynomial |
| 164 | +figure; |
| 165 | +plot(x_vals, y_vals, 'b-', 'LineWidth', 2); |
| 166 | +grid on; |
| 167 | +xlabel('x'); |
| 168 | +ylabel('p(x)'); |
| 169 | +title('Plot of Polynomial p(x)'); |
| 170 | +``` |
| 171 | +This code will generate a plot of the polynomial \( p(x) \). |
| 172 | + |
| 173 | + |
| 174 | +### Polynomial Curve Fitting |
| 175 | + |
| 176 | +MATLAB provides functions to fit polynomials to data points using `polyfit` and `polyval`. Here's an example: |
| 177 | + |
| 178 | +```matlab |
| 179 | +% Generate some data points |
| 180 | +x_data = 1:10; |
| 181 | +y_data = sin(x_data) + 0.5*randn(size(x_data)); |
| 182 | +
|
| 183 | +% Fit a polynomial of degree 3 to the data |
| 184 | +degree = 3; |
| 185 | +coefficients_fit = polyfit(x_data, y_data, degree); |
| 186 | +
|
| 187 | +% Evaluate the fitted polynomial |
| 188 | +x_values = linspace(1, 10, 100); % 100 points from 1 to 10 |
| 189 | +y_fit = polyval(coefficients_fit, x_values); |
| 190 | +
|
| 191 | +% Plot the original data and the fitted polynomial |
| 192 | +figure; |
| 193 | +plot(x_data, y_data, 'o', x_values, y_fit, '-') |
| 194 | +xlabel('x') |
| 195 | +ylabel('y') |
| 196 | +legend('Data', 'Fitted Polynomial') |
| 197 | +title('Polynomial Curve Fitting') |
| 198 | +``` |
| 199 | + |
| 200 | +### Symbolic Polynomial Manipulation |
| 201 | + |
| 202 | +MATLAB's Symbolic Math Toolbox allows symbolic manipulation of polynomials using `sym` and `solve` functions. Here’s an example: |
| 203 | + |
| 204 | +```matlab |
| 205 | +% Symbolic variables |
| 206 | +syms x |
| 207 | +
|
| 208 | +% Define a symbolic polynomial |
| 209 | +p_sym = x^3 - 4*x^2 + 5*x - 2; |
| 210 | +
|
| 211 | +% Find the roots symbolically |
| 212 | +roots_sym = solve(p_sym == 0, x); |
| 213 | +
|
| 214 | +% Display roots |
| 215 | +disp('Symbolic roots of the polynomial:') |
| 216 | +disp(roots_sym) |
| 217 | +
|
| 218 | +% Differentiate the polynomial |
| 219 | +dp_dx = diff(p_sym, x); |
| 220 | +disp('Differentiated polynomial:') |
| 221 | +disp(dp_dx) |
| 222 | +
|
| 223 | +% Integrate the polynomial |
| 224 | +integral_p = int(p_sym, x); |
| 225 | +disp('Integral of the polynomial:') |
| 226 | +disp(integral_p) |
| 227 | +``` |
| 228 | +### Output: |
| 229 | +For the polynomial curve fitting example, the output would include a plot showing the original data points (`o`) and the fitted polynomial (`-`). This plot visually demonstrates how well the polynomial fits the data. |
| 230 | + |
| 231 | +### Conclusion |
| 232 | + |
| 233 | +MATLAB provides comprehensive support for working with polynomials, offering functions for creation, evaluation, manipulation (addition, subtraction, multiplication, division), finding roots, and plotting. These tools are essential for various applications in numerical analysis, control systems, signal processing, and more. |
0 commit comments