Code and Graph Explanation
### Code Explanation (Line by Line)
Step 1: Importing Libraries
- numpy: Used for numerical operations and handling arrays.
- matplotlib.pyplot: A library for creating visualizations and graphs.
Step 2: Define Variables
x = np.linspace(0, 10, 1000)
- Generates an array of 1000 equally spaced points between 0 and 10.
- These x values will serve as the independent variable for plotting functions.
Step 3: Define Functions
y1 = np.sin(x)
y2 = np.cos(x)
y3 = y1 + y2
- Computes sine, cosine, and their sum element-wise, creating three separate waveforms.
Step 4: Create Plot
plt.figure(figsize=(10, 6))
- Sets up a figure of size 10 inches wide and 6 inches tall.
Step 5: Plot Individual Curves
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.plot(x, y3, label='sin(x) + cos(x)', linestyle='--')
- Plots the sine, cosine, and combined waveforms, assigning a dashed style to the sum.
Step 6: Customize Graph
plt.title('Trigonometric Functions')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.legend()
- Adds a title, axis labels, and a legend to identify each line.
Step 7: Display Graph
plt.grid(True)
plt.show()
- Adds a grid and renders the final graph.
### Graph Explanation
1. Sine Wave (sin(x) in Blue):
- A smooth oscillating wave starting at 0 and oscillating between -1 and 1.
2. Cosine Wave (cos(x) in Orange):
- Similar to the sine wave but starts at 1 (a phase shift of 90 degrees).
3. Combined Wave (sin(x) + cos(x) in Dashed Green):
- The result of adding sine and cosine values at each x, creating a new waveform.
4. Grid:
- Helps analyze the behavior of functions.
5. Legend:
- Identifies each line on the graph.
### Methods Used
1. numpy methods:
- np.linspace(): Generates an array of evenly spaced numbers.
- np.sin() and np.cos(): Compute trigonometric functions.
2. matplotlib methods:
- plt.plot(): Creates line plots for the given data.
- plt.title(), plt.xlabel(), plt.ylabel(): Add labels and a title.
- plt.legend(): Adds a legend to the graph.
- plt.grid(): Adds a grid to the graph.
- plt.show(): Displays the final plot.