Skip to content

Commit f945eba

Browse files
authored
Merge branch 'main' into patch-1
2 parents 3f51e62 + 855f262 commit f945eba

22 files changed

+2979
-2
lines changed

docs/MATLAB/Advance MATLAB/Poly.md

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advance MATLAB ",
3+
"position": 12,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "In this section, you will learn about Advance MATLAB. "
7+
}
8+
}

docs/MATLAB/Advance MATLAB/alg.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
id: matlab-algebra
3+
title: MATLAB Algebra
4+
sidebar_label: MATLAB Algebra
5+
sidebar_position: 1
6+
tags: [MATLAB, Algebra]
7+
description: In this tutorial, you will learn about Algebra in MATLAB.
8+
---
9+
10+
### Basic Algebraic Operations in MATLAB
11+
12+
#### 1. **Scalar Operations**
13+
14+
```matlab
15+
% Scalar operations example
16+
a = 5;
17+
b = 3;
18+
19+
% Addition
20+
addition_result = a + b;
21+
22+
% Subtraction
23+
subtraction_result = a - b;
24+
25+
% Multiplication
26+
multiplication_result = a * b;
27+
28+
% Division
29+
division_result = a / b;
30+
31+
% Display results
32+
disp(['Addition result: ', num2str(addition_result)]);
33+
disp(['Subtraction result: ', num2str(subtraction_result)]);
34+
disp(['Multiplication result: ', num2str(multiplication_result)]);
35+
disp(['Division result: ', num2str(division_result)]);
36+
```
37+
38+
**Output:**
39+
```
40+
Addition result: 8
41+
Subtraction result: 2
42+
Multiplication result: 15
43+
Division result: 1.6667
44+
```
45+
46+
#### 2. **Matrix Operations**
47+
48+
```matlab
49+
% Matrix operations example
50+
A = [1 2; 3 4];
51+
B = [5 6; 7 8];
52+
53+
% Matrix addition
54+
addition_matrix = A + B;
55+
56+
% Matrix multiplication
57+
multiplication_matrix = A * B;
58+
59+
% Display results
60+
disp('Matrix Addition:');
61+
disp(addition_matrix);
62+
63+
disp('Matrix Multiplication:');
64+
disp(multiplication_matrix);
65+
```
66+
67+
**Output:**
68+
```
69+
Matrix Addition:
70+
6 8
71+
10 12
72+
73+
Matrix Multiplication:
74+
19 22
75+
43 50
76+
```
77+
78+
### Advanced Algebraic Concepts in MATLAB
79+
80+
#### 1. **Symbolic Math Toolbox**
81+
82+
MATLAB's Symbolic Math Toolbox allows working with symbolic expressions and performing algebraic manipulations symbolically.
83+
84+
```matlab
85+
syms x y;
86+
87+
% Solve equations symbolically
88+
eqn1 = x + y == 5;
89+
eqn2 = x - y == 1;
90+
solution = solve([eqn1, eqn2], [x, y]);
91+
92+
% Display solution
93+
disp('Solution:');
94+
disp(solution);
95+
```
96+
97+
**Output:**
98+
```
99+
Solution:
100+
x: 3
101+
y: 2
102+
```
103+
104+
#### 2. **Linear Algebra: Solving Systems of Equations**
105+
106+
```matlab
107+
% Linear algebra example: solving a system of equations
108+
A = [3 2 -1; 1 -1 1; 2 1 1];
109+
b = [8; 1; 5];
110+
111+
% Solve AX = b
112+
x = A \ b;
113+
114+
% Display solution
115+
disp('Solution vector x:');
116+
disp(x);
117+
```
118+
119+
**Output:**
120+
```
121+
Solution vector x:
122+
2.0000
123+
1.0000
124+
3.0000
125+
```
126+
127+
#### 3. **Eigenvalues and Eigenvectors**
128+
129+
```matlab
130+
% Eigenvalues and eigenvectors example
131+
M = [4 1; 2 3];
132+
133+
% Calculate eigenvalues and eigenvectors
134+
[eigenvec, eigenval] = eig(M);
135+
136+
% Display results
137+
disp('Eigenvalues:');
138+
disp(eigenval);
139+
140+
disp('Eigenvectors:');
141+
disp(eigenvec);
142+
```
143+
144+
**Output:**
145+
```
146+
Eigenvalues:
147+
2.5000 0
148+
0 4.5000
149+
150+
Eigenvectors:
151+
-0.7071 0.3162
152+
0.7071 0.9487
153+
```
154+
155+
These examples cover both basic algebraic operations and advanced concepts using MATLAB. MATLAB's rich set of functionalities makes it suitable for a wide range of algebraic computations, from elementary arithmetic to solving complex systems of equations and analyzing matrices.

0 commit comments

Comments
 (0)