### MATLAB Project
**By: Student Name**
**Date: October 10, 2023**
**Course: Introduction to Scientific Computing**
---
### 1. Introduction to MATLAB
MATLAB (*Matrix Laboratory*) is a programming language for technical computing. It is used
for:
- Numerical computations
- Data visualization
- Algorithm development
- Solving engineering/scientific problems
---
### 2. Basic Operations
**Arithmetic Examples:**
```matlab
% Addition
>> 5 + 3
ans = 8
% Matrix multiplication
>> A = [1, 2; 3, 4];
>> B = [5; 6];
>> A * B
ans =
17
39
```
**Variables:**
```matlab
>> x = 7; % Scalar
>> y = [1, 2, 3]; % Row vector
>> z = y'; % Transpose (column vector)
```
---
### 3. Arrays & Matrices
**Creating Arrays:**
```matlab
>> vec = 1:5; % [1, 2, 3, 4, 5]
>> mat = zeros(2,3); % 2x3 matrix of zeros
>> randMat = rand(3); % 3x3 random matrix
```
**Indexing:**
```matlab
>> A = [10, 20, 30; 40, 50, 60];
>> A(2,3) % Row 2, Column 3 → 60
>> A(:,1) % All rows, Column 1 → [10; 40]
```
---
### 4. Plotting
**Simple 2D Plot:**
```matlab
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x, y, 'r-', 'LineWidth', 2);
>> title('Sine Wave');
>> xlabel('x');
>> ylabel('sin(x)');
>> grid on;
```
*Output:* Smooth red sine wave graph.
**3D Surface:**
```matlab
>> [X,Y] = meshgrid(-2:0.1:2);
>> Z = X .* exp(-X.^2 - Y.^2);
>> surf(X, Y, Z);
>> colormap('jet');
```
*Output:* 3D surface plot of a Gaussian function.
---
### 5. Control Flow
**`if-else` Statement:**
```matlab
% Check if number is positive
num = input('Enter a number: ');
if num > 0
disp('Positive');
elseif num < 0
disp('Negative');
else
disp('Zero');
end
```
**`for` Loop:**
```matlab
% Sum first 10 natural numbers
sum = 0;
for k = 1:10
sum = sum + k;
end
disp(['Sum = ', num2str(sum)]); % Output: Sum = 55
```
---
### 6. Functions
**User-Defined Function (`quadroots.m`):**
```matlab
function [x1, x2] = quadroots(a, b, c)
% Solves quadratic equation ax^2 + bx + c = 0
d = sqrt(b^2 - 4*a*c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end
```
**Usage:**
```matlab
>> [r1, r2] = quadroots(1, -3, 2);
>> disp(['Roots: ', num2str(r1), ', ', num2str(r2)]);
% Output: Roots: 2, 1
```
---
### 7. Application: Signal Processing
**Add Noise & Filter:**
```matlab
% Generate clean signal
t = 0:0.01:1;
clean = sin(2*pi*5*t);
% Add noise
noisy = clean + 0.5*randn(size(t));
% Moving average filter
window = 5;
smoothed = movmean(noisy, window);
% Plot results
plot(t, clean, 'b', t, noisy, 'g', t, smoothed, 'r', 'LineWidth', 1.5);
legend('Clean', 'Noisy', 'Filtered');
```
*Output:* Plot comparing clean, noisy, and filtered signals.
---
### 8. Conclusion
MATLAB simplifies complex mathematical tasks. Key strengths:
- Intuitive matrix operations
- Powerful visualization tools
- Extensive built-in libraries
- Rapid prototyping capabilities
**Challenges:**
- Licensing cost
- Slower than compiled languages (e.g., C++)
---
*End of Project*
*(Handwritten figures and graphs would be included in the original submission)*