-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalculus.html
More file actions
1261 lines (1092 loc) · 56.6 KB
/
calculus.html
File metadata and controls
1261 lines (1092 loc) · 56.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculus - Math from Zero to CS - Better Dev</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<!-- Page Header -->
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> › Calculus</div>
<h1>Calculus</h1>
<p>The math of change and accumulation -- limits, derivatives, and integrals explained step by step for programmers.</p>
<div class="tip-box" style="margin-top: 1rem;">
<div class="label">Why Calculus Matters for CS</div>
<p>"Do I really need calculus as a programmer?" If you want to work in <strong>any</strong> of these areas, yes:</p>
<ul>
<li><strong>Machine learning / AI:</strong> Gradient descent -- the core algorithm behind training neural networks -- is literally "take the derivative, walk downhill." Every ML framework (PyTorch, TensorFlow) runs calculus under the hood.</li>
<li><strong>Computer graphics:</strong> Lighting, shadows, curves (Bezier curves in Photoshop/Figma), and physics simulations all use derivatives and integrals.</li>
<li><strong>Physics engines:</strong> Position is the integral of velocity, velocity is the integral of acceleration. Every game engine computes this every frame.</li>
<li><strong>Signal processing:</strong> Audio, video, and image compression (JPEG, MP3) use Fourier transforms -- a calculus concept.</li>
<li><strong>Algorithm analysis:</strong> Understanding growth rates, optimization, and approximation all rely on calculus intuition.</li>
<li><strong>Robotics / control systems:</strong> PID controllers use proportional, integral, and derivative terms to keep drones stable and robots balanced.</li>
</ul>
<p>Even if you never solve an integral by hand again, understanding <em>what</em> derivatives and integrals mean lets you use libraries and tools effectively instead of treating them as magic black boxes.</p>
</div>
</div>
<!-- Table of Contents -->
<div class="toc">
<h4>Table of Contents</h4>
<a href="#big-picture">1. What is Calculus? (The Big Picture)</a>
<a href="#limits">2. Limits</a>
<a href="#derivatives-basics">3. Derivatives -- The Basics</a>
<a href="#derivatives-advanced">4. Derivative Rules (Advanced)</a>
<a href="#applications-derivatives">5. Applications of Derivatives</a>
<a href="#integrals-basics">6. Integrals -- The Basics</a>
<a href="#definite-integrals">7. Definite Integrals</a>
<a href="#applications-integrals">8. Applications of Integrals</a>
<a href="#partial-derivatives">9. Partial Derivatives & Gradients (For ML)</a>
<a href="#quiz">10. Practice Quiz</a>
</div>
<!-- ============================== -->
<!-- SECTION 1: THE BIG PICTURE -->
<!-- ============================== -->
<section id="big-picture">
<h2>1. What is Calculus? (The Big Picture)</h2>
<p>
Calculus sounds intimidating, but at its core it answers two simple questions:
</p>
<ol>
<li><strong>How fast is something changing right now?</strong> (Differential calculus)</li>
<li><strong>How much has accumulated over time?</strong> (Integral calculus)</li>
</ol>
<p>
That is it. Every formula, every rule, every theorem in calculus is just a tool for answering one of those two questions more precisely.
</p>
<h3>The Speed vs. Distance Analogy</h3>
<p>
Imagine you are driving a car. Your speedometer tells you your <strong>speed at this exact instant</strong> -- that is a derivative. Your odometer tells you the <strong>total distance you have traveled</strong> -- that is an integral. Calculus is the math that connects these two ideas.
</p>
<ul>
<li>If you know your position at every moment, calculus can tell you your speed (derivative).</li>
<li>If you know your speed at every moment, calculus can tell you the total distance traveled (integral).</li>
</ul>
<p>
These two operations are inverses of each other -- and that beautiful connection is called the <strong>Fundamental Theorem of Calculus</strong>.
</p>
<h3>Why Programmers Need Calculus</h3>
<p>
You might wonder: "I write code, not physics papers. Why do I need this?" Here is why:
</p>
<ul>
<li><strong>Machine Learning / AI:</strong> Training a neural network uses gradient descent, which is literally "follow the derivative downhill to minimize error."</li>
<li><strong>Optimization:</strong> Finding the minimum cost, maximum efficiency, or best fit to data -- all use derivatives.</li>
<li><strong>Physics Simulations:</strong> Game engines, robotics, animations -- anything with motion uses calculus.</li>
<li><strong>Computer Graphics:</strong> Smooth curves, lighting calculations, and surface normals rely on calculus.</li>
<li><strong>Probability and Statistics:</strong> Continuous probability distributions are defined using integrals.</li>
</ul>
<div class="tip-box">
<div class="label">Tip</div>
<p>You do not need to be a calculus wizard to be a great programmer. But understanding the core ideas -- especially derivatives and what they mean -- will unlock entire areas of CS that would otherwise feel like black-box magic.</p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 2: LIMITS -->
<!-- ============================== -->
<section id="limits">
<h2>2. Limits</h2>
<p>
Before you can understand derivatives or integrals, you need limits. A <strong>limit</strong> answers the question: "What value does f(x) <em>approach</em> as x gets closer and closer to some number?"
</p>
<p>
Notice the word "approach." We do not care what f(x) actually equals at that point -- we care about what it is heading toward.
</p>
<h3>Notation</h3>
<div class="formula-box">
lim f(x) = L
x → a
"The limit of f(x) as x approaches a equals L"
</div>
<p>
This means: as x gets closer and closer to <strong>a</strong> (from both sides), f(x) gets closer and closer to <strong>L</strong>.
</p>
<h3>One-Sided Limits</h3>
<p>
Sometimes a function approaches different values depending on which direction you come from:
</p>
<div class="formula-box">
Left-hand limit: lim f(x) = L
x → a−
Right-hand limit: lim f(x) = L
x → a+
</div>
<p>
The full (two-sided) limit exists <strong>only if</strong> the left-hand and right-hand limits are equal. If they disagree, the limit does not exist.
</p>
<h3>Limits at Infinity</h3>
<p>
We can also ask what happens as x grows without bound:
</p>
<div class="formula-box">
lim f(x) = L
x → ∞
"As x grows infinitely large, f(x) approaches L"
</div>
<div class="example-box">
<div class="label">Example</div>
<p>Find: lim (1/x) as x → ∞</p>
<p>As x gets bigger and bigger (10, 100, 1000, ...), 1/x gets smaller and smaller (0.1, 0.01, 0.001, ...).</p>
<p><strong>Answer:</strong> The limit is <strong>0</strong>.</p>
</div>
<h3>When Limits Do Not Exist</h3>
<p>A limit does not exist (DNE) when:</p>
<ul>
<li>The left and right limits disagree (e.g., a step function at the jump).</li>
<li>The function oscillates wildly without settling (e.g., sin(1/x) as x → 0).</li>
<li>The function blows up to infinity (though we sometimes write "the limit is ∞" informally).</li>
</ul>
<h3>How to Evaluate Limits</h3>
<p><strong>Method 1: Direct Substitution</strong></p>
<p>Just plug in the value of a. If you get a real number, that is the limit. This works for most "nice" functions (polynomials, exponentials, trig).</p>
<div class="example-box">
<div class="label">Example -- Direct Substitution</div>
<p>Find: lim (x² + 3x) as x → 2</p>
<p>Plug in x = 2: (2)² + 3(2) = 4 + 6 = 10</p>
<p><strong>Answer:</strong> The limit is <strong>10</strong>.</p>
</div>
<p><strong>Method 2: Factoring</strong></p>
<p>If direct substitution gives 0/0 (an indeterminate form), try factoring and canceling.</p>
<div class="example-box">
<div class="label">Example -- Factoring</div>
<p>Find: lim (x² - 4) / (x - 2) as x → 2</p>
<p>Direct substitution gives: (4 - 4) / (2 - 2) = 0/0. Indeterminate!</p>
<p>Factor the numerator: (x - 2)(x + 2) / (x - 2)</p>
<p>Cancel (x - 2): x + 2</p>
<p>Now substitute: 2 + 2 = 4</p>
<p><strong>Answer:</strong> The limit is <strong>4</strong>.</p>
</div>
<p><strong>Method 3: Rationalizing</strong></p>
<p>When you have square roots and get 0/0, multiply by the conjugate.</p>
<div class="example-box">
<div class="label">Example -- Rationalizing</div>
<p>Find: lim (√(x+1) - 1) / x as x → 0</p>
<p>Direct substitution: (√1 - 1) / 0 = 0/0. Indeterminate!</p>
<p>Multiply by the conjugate: [(√(x+1) - 1)(√(x+1) + 1)] / [x(√(x+1) + 1)]</p>
<p>Numerator becomes: (x+1) - 1 = x</p>
<p>So we get: x / [x(√(x+1) + 1)] = 1 / (√(x+1) + 1)</p>
<p>Now substitute x = 0: 1 / (√1 + 1) = 1/2</p>
<p><strong>Answer:</strong> The limit is <strong>1/2</strong>.</p>
</div>
<div class="example-box">
<div class="label">Example -- Limit at Infinity</div>
<p>Find: lim (3x² + 2x) / (x² - 5) as x → ∞</p>
<p>Divide every term by x² (the highest power): (3 + 2/x) / (1 - 5/x²)</p>
<p>As x → ∞, 2/x → 0 and 5/x² → 0</p>
<p>So the limit becomes: 3 / 1 = 3</p>
<p><strong>Answer:</strong> The limit is <strong>3</strong>.</p>
</div>
<div class="tip-box">
<div class="label">Tip -- Limits at Infinity for Rational Functions</div>
<p>For a ratio of polynomials as x → ∞: compare the highest degree in the numerator and denominator. Same degree? Limit = ratio of leading coefficients. Numerator higher? Limit is ∞. Denominator higher? Limit is 0.</p>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>0/0 is NOT the answer -- it means the limit is "indeterminate" and you need to do more work (factor, rationalize, or use L'Hopital's rule). Do not confuse 0/0 with "the limit is 0" or "the limit does not exist."</p>
</div>
<div class="formula-box">
<strong>Limit Laws (Precise Rules):</strong><br><br>
If lim(x→a) f(x) = L and lim(x→a) g(x) = M, then:<br><br>
• Sum: lim(x→a) [f(x) + g(x)] = L + M<br>
• Difference: lim(x→a) [f(x) - g(x)] = L - M<br>
• Product: lim(x→a) [f(x) · g(x)] = L · M<br>
• Quotient: lim(x→a) [f(x)/g(x)] = L/M (provided M ≠ 0)<br>
• Constant: lim(x→a) [c · f(x)] = c · L<br>
• Power: lim(x→a) [f(x)]ⁿ = Lⁿ
</div>
</section>
<!-- ============================== -->
<!-- SECTION 3: DERIVATIVES BASICS -->
<!-- ============================== -->
<section id="derivatives-basics">
<h2>3. Derivatives -- The Basics</h2>
<p>
A <strong>derivative</strong> measures the <strong>instantaneous rate of change</strong> of a function. If f(x) describes your position at time x, then f'(x) tells you your velocity -- how fast you are moving right at that instant.
</p>
<h3>The Formal Definition</h3>
<div class="formula-box">
f'(x) = lim [f(x + h) - f(x)] / h
h → 0
</div>
<p>
This says: take two points on the curve that are very close together (distance h apart), compute the slope of the line between them, and see what happens as h shrinks to zero. The slope of that infinitely-close line is the derivative.
</p>
<h3>Geometric Meaning</h3>
<p>
The derivative at a point is the <strong>slope of the tangent line</strong> to the curve at that point. A positive derivative means the function is going up; a negative derivative means it is going down; a zero derivative means it is flat (possibly a peak or valley).
</p>
<h3>Notation</h3>
<p>You will see derivatives written in several ways. They all mean the same thing:</p>
<div class="formula-box">
f'(x) -- "f prime of x" (Lagrange notation)
dy/dx -- "dee y dee x" (Leibniz notation)
df/dx -- "dee f dee x"
Df(x) -- "D of f" (operator notation)
</div>
<div class="tip-box">
<div class="label">Tip</div>
<p>Leibniz notation (dy/dx) is not a fraction, but it behaves like one in many situations -- which is part of its power. It reminds you that the derivative is a ratio of tiny changes.</p>
</div>
<h3>Basic Differentiation Rules</h3>
<p>You do not need to use the formal limit definition every time. These rules let you find derivatives quickly:</p>
<table>
<tr>
<th>Rule</th>
<th>Formula</th>
<th>Example</th>
</tr>
<tr>
<td>Constant</td>
<td>d/dx(c) = 0</td>
<td>d/dx(7) = 0</td>
</tr>
<tr>
<td>Power Rule</td>
<td>d/dx(x<sup>n</sup>) = n · x<sup>n-1</sup></td>
<td>d/dx(x³) = 3x²</td>
</tr>
<tr>
<td>Constant Multiple</td>
<td>d/dx(c · f) = c · f'(x)</td>
<td>d/dx(5x²) = 10x</td>
</tr>
<tr>
<td>Sum Rule</td>
<td>d/dx(f + g) = f' + g'</td>
<td>d/dx(x² + 3x) = 2x + 3</td>
</tr>
<tr>
<td>Difference Rule</td>
<td>d/dx(f - g) = f' - g'</td>
<td>d/dx(x³ - x) = 3x² - 1</td>
</tr>
</table>
<div class="example-box">
<div class="label">Example -- Power Rule</div>
<p>Find the derivative of f(x) = x²</p>
<p>Using the limit definition (to show why the power rule works):</p>
<p>f'(x) = lim [(x+h)² - x²] / h as h → 0</p>
<p>= lim [x² + 2xh + h² - x²] / h</p>
<p>= lim [2xh + h²] / h</p>
<p>= lim (2x + h)</p>
<p>= <strong>2x</strong></p>
<p>This matches the power rule: d/dx(x²) = 2x<sup>1</sup> = 2x.</p>
</div>
<div class="example-box">
<div class="label">Example -- Combining Rules</div>
<p>Find f'(x) where f(x) = 4x³ - 2x² + 7x - 5</p>
<p>Apply the rules term by term:</p>
<ul>
<li>d/dx(4x³) = 12x² (power rule + constant multiple)</li>
<li>d/dx(-2x²) = -4x</li>
<li>d/dx(7x) = 7</li>
<li>d/dx(-5) = 0 (constant rule)</li>
</ul>
<p><strong>f'(x) = 12x² - 4x + 7</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Negative and Fractional Exponents</div>
<p>The power rule works for all exponents, not just positive integers:</p>
<ul>
<li>d/dx(1/x) = d/dx(x<sup>-1</sup>) = -1 · x<sup>-2</sup> = <strong>-1/x²</strong></li>
<li>d/dx(√x) = d/dx(x<sup>1/2</sup>) = (1/2) · x<sup>-1/2</sup> = <strong>1 / (2√x)</strong></li>
</ul>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>The power rule only works on x<sup>n</sup> where n is a constant. It does NOT work on functions like 2<sup>x</sup> or x<sup>x</sup> -- those require different rules.</p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 4: ADVANCED RULES -->
<!-- ============================== -->
<section id="derivatives-advanced">
<h2>4. Derivative Rules (Advanced)</h2>
<p>
The basic rules handle simple terms, but what about products, quotients, and compositions of functions? That is where these three powerful rules come in.
</p>
<h3>The Product Rule</h3>
<div class="formula-box">
(f · g)' = f' · g + f · g'
"Derivative of first times second, plus first times derivative of second"
</div>
<div class="example-box">
<div class="label">Example -- Product Rule</div>
<p>Find d/dx [x² · sin(x)]</p>
<p>Let f = x² and g = sin(x)</p>
<p>f' = 2x, g' = cos(x)</p>
<p>Product rule: f'g + fg' = 2x · sin(x) + x² · cos(x)</p>
<p><strong>Answer: 2x sin(x) + x² cos(x)</strong></p>
</div>
<h3>The Quotient Rule</h3>
<div class="formula-box">
(f / g)' = [f' · g - f · g'] / g²
"Low d-high minus high d-low, over the square of what's below"
</div>
<div class="example-box">
<div class="label">Example -- Quotient Rule</div>
<p>Find d/dx [(3x + 1) / (x² + 1)]</p>
<p>f = 3x + 1, g = x² + 1</p>
<p>f' = 3, g' = 2x</p>
<p>= [3(x² + 1) - (3x + 1)(2x)] / (x² + 1)²</p>
<p>= [3x² + 3 - 6x² - 2x] / (x² + 1)²</p>
<p><strong>= (-3x² - 2x + 3) / (x² + 1)²</strong></p>
</div>
<h3>The Chain Rule</h3>
<p>The chain rule is arguably the most important rule in all of calculus, especially for machine learning. It tells you how to differentiate <strong>compositions</strong> of functions -- a function inside another function.</p>
<div class="formula-box">
d/dx [f(g(x))] = f'(g(x)) · g'(x)
"Derivative of the outer (evaluated at inner) times derivative of the inner"
</div>
<div class="example-box">
<div class="label">Example -- Chain Rule</div>
<p>Find d/dx [(3x + 2)³]</p>
<p>Outer function: f(u) = u³, so f'(u) = 3u²</p>
<p>Inner function: g(x) = 3x + 2, so g'(x) = 3</p>
<p>Chain rule: f'(g(x)) · g'(x) = 3(3x + 2)² · 3</p>
<p><strong>= 9(3x + 2)²</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Chain Rule with Trig</div>
<p>Find d/dx [sin(x²)]</p>
<p>Outer: sin(u), derivative = cos(u)</p>
<p>Inner: x², derivative = 2x</p>
<p><strong>= cos(x²) · 2x = 2x cos(x²)</strong></p>
</div>
<div class="tip-box">
<div class="label">Tip -- Why the Chain Rule Matters for ML</div>
<p>Neural networks are just chains of functions: layer after layer of transformations. <strong>Backpropagation</strong> -- the algorithm that trains neural networks -- is literally the chain rule applied repeatedly. When people say "deep learning requires calculus," this is what they mean.</p>
</div>
<h3>Derivatives of Common Functions</h3>
<p>Memorize these. They come up constantly:</p>
<table>
<tr>
<th>f(x)</th>
<th>f'(x)</th>
<th>Notes</th>
</tr>
<tr>
<td>sin(x)</td>
<td>cos(x)</td>
<td></td>
</tr>
<tr>
<td>cos(x)</td>
<td>-sin(x)</td>
<td>Note the negative sign</td>
</tr>
<tr>
<td>tan(x)</td>
<td>sec²(x)</td>
<td></td>
</tr>
<tr>
<td>e<sup>x</sup></td>
<td>e<sup>x</sup></td>
<td>e<sup>x</sup> is its own derivative!</td>
</tr>
<tr>
<td>ln(x)</td>
<td>1/x</td>
<td>Only for x > 0</td>
</tr>
<tr>
<td>a<sup>x</sup></td>
<td>a<sup>x</sup> · ln(a)</td>
<td>General exponential</td>
</tr>
</table>
<div class="example-box">
<div class="label">Example -- Combining Multiple Rules</div>
<p>Find d/dx [e<sup>x</sup> · ln(x)]</p>
<p>Use product rule with f = e<sup>x</sup>, g = ln(x):</p>
<p>f' = e<sup>x</sup>, g' = 1/x</p>
<p>= e<sup>x</sup> · ln(x) + e<sup>x</sup> · (1/x)</p>
<p><strong>= e<sup>x</sup>(ln(x) + 1/x)</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Chain Rule with e</div>
<p>Find d/dx [e<sup>(3x²)</sup>]</p>
<p>Outer: e<sup>u</sup>, derivative = e<sup>u</sup></p>
<p>Inner: 3x², derivative = 6x</p>
<p><strong>= 6x · e<sup>(3x²)</sup></strong></p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 5: APPLICATIONS -->
<!-- ============================== -->
<section id="applications-derivatives">
<h2>5. Applications of Derivatives</h2>
<p>
Derivatives are not just abstract math -- they are tools for solving real problems. Here are the most important applications.
</p>
<h3>Finding Maxima and Minima</h3>
<p>
At the top of a hill or the bottom of a valley, the slope is <strong>zero</strong>. This means we can find peaks and valleys of any function by setting its derivative equal to zero and solving.
</p>
<div class="formula-box">
To find maxima/minima of f(x):
1. Find f'(x)
2. Set f'(x) = 0 and solve for x (these are "critical points")
3. Use the second derivative to classify:
- f''(x) > 0 at critical point → local minimum (concave up)
- f''(x) < 0 at critical point → local maximum (concave down)
- f''(x) = 0 → inconclusive (need further analysis)
</div>
<div class="example-box">
<div class="label">Example -- Finding Extrema</div>
<p>Find the maximum and minimum of f(x) = x³ - 3x + 2</p>
<p><strong>Step 1:</strong> f'(x) = 3x² - 3</p>
<p><strong>Step 2:</strong> Set f'(x) = 0: 3x² - 3 = 0 → x² = 1 → x = 1 or x = -1</p>
<p><strong>Step 3:</strong> f''(x) = 6x</p>
<ul>
<li>At x = 1: f''(1) = 6 > 0, so x = 1 is a <strong>local minimum</strong>. f(1) = 0</li>
<li>At x = -1: f''(-1) = -6 < 0, so x = -1 is a <strong>local maximum</strong>. f(-1) = 4</li>
</ul>
</div>
<h3>Optimization Problems</h3>
<p>
Real-world optimization follows the same pattern: write a function for the thing you want to maximize or minimize, take the derivative, set it to zero, solve.
</p>
<div class="example-box">
<div class="label">Example -- Optimization</div>
<p>A farmer has 100 meters of fencing. What dimensions maximize the area of a rectangular pen?</p>
<p>Let width = x, so length = (100 - 2x) / 2 = 50 - x</p>
<p>Area: A(x) = x(50 - x) = 50x - x²</p>
<p>A'(x) = 50 - 2x</p>
<p>Set A'(x) = 0: 50 - 2x = 0 → x = 25</p>
<p>So width = 25m, length = 25m. <strong>A square!</strong></p>
<p>Maximum area = 25 × 25 = <strong>625 m²</strong></p>
</div>
<h3>Related Rates (Brief Introduction)</h3>
<p>
Related rates problems ask: if one quantity is changing, how fast is a related quantity changing? You use the chain rule with respect to time.
</p>
<div class="example-box">
<div class="label">Example -- Related Rates</div>
<p>A circle's radius is increasing at 2 cm/s. How fast is the area increasing when r = 5 cm?</p>
<p>A = πr²</p>
<p>Differentiate both sides with respect to time t:</p>
<p>dA/dt = 2πr · (dr/dt)</p>
<p>Plug in r = 5 and dr/dt = 2:</p>
<p>dA/dt = 2π(5)(2) = 20π ≈ <strong>62.83 cm²/s</strong></p>
</div>
<h3>Gradient Descent -- Why ML Engineers Love Derivatives</h3>
<p>
In machine learning, you have a <strong>loss function</strong> that measures how wrong your model is. Training the model means finding the inputs (weights) that <strong>minimize</strong> this loss. How? Derivatives!
</p>
<div class="formula-box">
Gradient Descent Update Rule:
w_new = w_old - learning_rate × dL/dw
"Move the weight in the direction that reduces the loss"
</div>
<p>The idea is beautifully simple:</p>
<ol>
<li>Compute the derivative of the loss with respect to each weight (the gradient).</li>
<li>The derivative tells you which direction increases the loss.</li>
<li>Go in the <strong>opposite</strong> direction (hence the minus sign).</li>
<li>Repeat until you reach a minimum.</li>
</ol>
<div class="tip-box">
<div class="label">Tip</div>
<p>Think of gradient descent like walking downhill in fog. You cannot see the bottom, but you can feel the slope under your feet (the derivative). Take a step in the steepest downhill direction, check the slope again, repeat. You will eventually reach a valley.</p>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>Gradient descent can get stuck in local minima -- a valley that is not the lowest point overall. This is a real problem in ML and is why techniques like momentum, Adam optimizer, and random restarts exist.</p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 6: INTEGRALS BASICS -->
<!-- ============================== -->
<section id="integrals-basics">
<h2>6. Integrals -- The Basics</h2>
<p>
If derivatives answer "how fast is it changing?", integrals answer "how much has accumulated?" Integration is the reverse process of differentiation -- finding the <strong>antiderivative</strong>.
</p>
<h3>Antiderivatives</h3>
<p>
If F'(x) = f(x), then F(x) is the antiderivative of f(x). We write this as:
</p>
<div class="formula-box">
∫ f(x) dx = F(x) + C
where F'(x) = f(x) and C is the constant of integration
</div>
<h3>Why the +C?</h3>
<p>
Since the derivative of any constant is zero, there are infinitely many antiderivatives. For example, x² + 5 and x² - 100 and x² + π all have the same derivative: 2x. The "+C" captures all these possibilities.
</p>
<div class="warning-box">
<div class="label">Warning</div>
<p>Never forget the +C on indefinite integrals. It is a constant source of lost marks (pun intended) and it actually matters in applications -- the constant represents initial conditions (like starting position or initial amount).</p>
</div>
<h3>Basic Integration Rules</h3>
<p>These are the derivative rules, but reversed:</p>
<table>
<tr>
<th>Function</th>
<th>Integral</th>
<th>Example</th>
</tr>
<tr>
<td>x<sup>n</sup> (n ≠ -1)</td>
<td>x<sup>n+1</sup> / (n+1) + C</td>
<td>∫ x³ dx = x<sup>4</sup>/4 + C</td>
</tr>
<tr>
<td>1/x</td>
<td>ln|x| + C</td>
<td>∫ (1/x) dx = ln|x| + C</td>
</tr>
<tr>
<td>e<sup>x</sup></td>
<td>e<sup>x</sup> + C</td>
<td>∫ e<sup>x</sup> dx = e<sup>x</sup> + C</td>
</tr>
<tr>
<td>cos(x)</td>
<td>sin(x) + C</td>
<td>∫ cos(x) dx = sin(x) + C</td>
</tr>
<tr>
<td>sin(x)</td>
<td>-cos(x) + C</td>
<td>∫ sin(x) dx = -cos(x) + C</td>
</tr>
<tr>
<td>constant k</td>
<td>kx + C</td>
<td>∫ 5 dx = 5x + C</td>
</tr>
</table>
<div class="tip-box">
<div class="label">Tip -- Checking Your Work</div>
<p>You can always verify an integral by differentiating your answer. If you get back the original function, you did it right. This is the best self-check in calculus.</p>
</div>
<div class="example-box">
<div class="label">Example -- Power Rule for Integration</div>
<p>Find ∫ x<sup>4</sup> dx</p>
<p>Add 1 to the exponent: 4 + 1 = 5</p>
<p>Divide by the new exponent: x<sup>5</sup> / 5</p>
<p><strong>∫ x<sup>4</sup> dx = x<sup>5</sup>/5 + C</strong></p>
<p>Check: d/dx (x<sup>5</sup>/5 + C) = 5x<sup>4</sup>/5 = x<sup>4</sup>. Correct!</p>
</div>
<div class="example-box">
<div class="label">Example -- Integrating a Polynomial</div>
<p>Find ∫ (3x² + 2x - 7) dx</p>
<p>Integrate term by term:</p>
<ul>
<li>∫ 3x² dx = 3 · x³/3 = x³</li>
<li>∫ 2x dx = 2 · x²/2 = x²</li>
<li>∫ -7 dx = -7x</li>
</ul>
<p><strong>= x³ + x² - 7x + C</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Rewriting Before Integrating</div>
<p>Find ∫ (1/x³) dx</p>
<p>Rewrite: 1/x³ = x<sup>-3</sup></p>
<p>Apply power rule: x<sup>-3+1</sup> / (-3+1) = x<sup>-2</sup> / (-2) = -1/(2x²)</p>
<p><strong>∫ (1/x³) dx = -1/(2x²) + C</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Roots</div>
<p>Find ∫ √x dx</p>
<p>Rewrite: √x = x<sup>1/2</sup></p>
<p>Apply power rule: x<sup>1/2 + 1</sup> / (1/2 + 1) = x<sup>3/2</sup> / (3/2) = (2/3)x<sup>3/2</sup></p>
<p><strong>∫ √x dx = (2/3)x<sup>3/2</sup> + C</strong></p>
</div>
<div class="formula-box">
<strong>Integration Linearity (The One Rule That Governs All Integration):</strong><br><br>
∫[a·f(x) + b·g(x)]dx = a·∫f(x)dx + b·∫g(x)dx<br><br>
<span style="color:#555555;">Constants pull out. Sums split. This single rule, combined with the table of basic antiderivatives, lets you integrate any polynomial.</span>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 7: DEFINITE INTEGRALS -->
<!-- ============================== -->
<section id="definite-integrals">
<h2>7. Definite Integrals</h2>
<p>
An <strong>indefinite integral</strong> gives you a family of functions (+C). A <strong>definite integral</strong> gives you a specific number -- the total accumulation between two points.
</p>
<h3>Area Under a Curve</h3>
<p>
The definite integral of f(x) from a to b represents the <strong>signed area</strong> between the curve and the x-axis, from x = a to x = b.
</p>
<div class="formula-box">
b
∫ f(x) dx = "Area under f(x) from a to b"
a
Area above x-axis is positive; area below x-axis is negative.
</div>
<h3>The Fundamental Theorem of Calculus</h3>
<p>
This is the crown jewel of calculus. It says that differentiation and integration are inverses of each other, and it gives us a practical way to evaluate definite integrals:
</p>
<div class="formula-box">
b
∫ f(x) dx = F(b) - F(a)
a
where F(x) is any antiderivative of f(x)
"Find the antiderivative, plug in the upper limit,
subtract the antiderivative at the lower limit."
</div>
<p>
Notice that the +C cancels out (it appears in both F(b) and F(a)), which is why definite integrals do not have a +C.
</p>
<div class="tip-box">
<div class="label">Tip</div>
<p>The notation F(x) |<sub>a</sub><sup>b</sup> (with the evaluation bar) is a shorthand for F(b) - F(a). You will see this written as [F(x)]<sub>a</sub><sup>b</sup> in many textbooks.</p>
</div>
<div class="example-box">
<div class="label">Example -- Basic Definite Integral</div>
<p>Evaluate ∫ from 1 to 3 of x² dx</p>
<p><strong>Step 1:</strong> Find antiderivative: F(x) = x³/3</p>
<p><strong>Step 2:</strong> Evaluate at the bounds:</p>
<p>F(3) - F(1) = (3³/3) - (1³/3) = 27/3 - 1/3 = 26/3</p>
<p><strong>Answer: 26/3 ≈ 8.667</strong></p>
<p>This is the area under y = x² from x = 1 to x = 3.</p>
</div>
<div class="example-box">
<div class="label">Example -- Definite Integral with Multiple Terms</div>
<p>Evaluate ∫ from 0 to 2 of (3x² - 4x + 1) dx</p>
<p>Antiderivative: F(x) = x³ - 2x² + x</p>
<p>F(2) = 8 - 8 + 2 = 2</p>
<p>F(0) = 0 - 0 + 0 = 0</p>
<p><strong>Answer: 2 - 0 = 2</strong></p>
</div>
<div class="example-box">
<div class="label">Example -- Using e and ln</div>
<p>Evaluate ∫ from 1 to e of (1/x) dx</p>
<p>Antiderivative: F(x) = ln(x)</p>
<p>F(e) - F(1) = ln(e) - ln(1) = 1 - 0</p>
<p><strong>Answer: 1</strong></p>
<p>The area under 1/x from 1 to e is exactly 1. This is actually how the number e is defined!</p>
</div>
<div class="warning-box">
<div class="label">Warning</div>
<p>The definite integral gives <strong>signed</strong> area. If f(x) is below the x-axis, the integral is negative. To find the total (unsigned) area, you need to split the integral at the zeros and take absolute values.</p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 8: APPLICATIONS -->
<!-- ============================== -->
<section id="applications-integrals">
<h2>8. Applications of Integrals</h2>
<h3>Area Between Two Curves</h3>
<p>
To find the area between two curves f(x) and g(x) (where f(x) ≥ g(x)) from a to b:
</p>
<div class="formula-box">
b
∫ [f(x) - g(x)] dx
a
"Integral of (top curve minus bottom curve)"
</div>
<div class="example-box">
<div class="label">Example -- Area Between Curves</div>
<p>Find the area between y = x² and y = x from x = 0 to x = 1.</p>
<p>On [0, 1], x ≥ x² (the line is above the parabola).</p>
<p>∫ from 0 to 1 of (x - x²) dx</p>
<p>= [x²/2 - x³/3] from 0 to 1</p>
<p>= (1/2 - 1/3) - (0 - 0) = 1/6</p>
<p><strong>Area = 1/6</strong></p>
</div>
<h3>Why CS People Care About Integrals</h3>
<p><strong>Probability Distributions</strong></p>
<p>
In statistics and ML, continuous probability distributions are defined using integrals. The probability that a random variable X falls between a and b is:
</p>
<div class="formula-box">
b
P(a ≤ X ≤ b) = ∫ f(x) dx
a
where f(x) is the probability density function (PDF)
</div>
<p>
The total area under the entire PDF must equal 1 (there is a 100% chance of something happening). This is why integrals are unavoidable in probability.
</p>
<p><strong>Expected Value</strong></p>
<p>
The expected value (average outcome) of a continuous random variable is:
</p>
<div class="formula-box">
∞
E[X] = ∫ x · f(x) dx
-∞
</div>
<p>
This shows up everywhere in ML: loss functions, reward signals in reinforcement learning, and Bayesian statistics.
</p>
<p><strong>Computational Geometry</strong></p>
<p>
Integrals help compute areas and volumes of complex shapes, which matters for computer graphics, geographic information systems (GIS), physics simulations, and any application that works with continuous shapes.
</p>
<div class="tip-box">
<div class="label">Tip</div>
<p>In practice, computers almost never compute integrals symbolically. They use <strong>numerical integration</strong> -- approximating the area by dividing it into tiny rectangles or trapezoids and adding them up. But understanding the theory tells you what the computer is approximating and why.</p>
</div>
</section>
<!-- ============================== -->
<!-- SECTION 9: PARTIAL DERIVATIVES -->
<!-- ============================== -->
<section id="partial-derivatives">
<h2>9. Partial Derivatives & Gradients (For ML)</h2>
<p>Everything you learned about derivatives so far involved <strong>one variable</strong>: y = f(x). But in real-world CS, functions depend on <strong>many variables at once</strong>. A machine learning loss function might depend on millions of weights. How do you take the derivative when there are 10 inputs, not 1?</p>
<p>The answer: <strong>partial derivatives</strong>. You take the derivative with respect to <em>one variable at a time</em>, treating all the others as constants.</p>
<h3>What Is a Partial Derivative?</h3>
<p>Consider this function with two inputs:</p>
<div class="formula-box">
f(x, y) = x² + 3xy + y²
</div>
<p>The partial derivative with respect to x (written ∂f/∂x) asks: <strong>"If I nudge x slightly while keeping y fixed, how much does f change?"</strong></p>
<p>To compute it, just differentiate normally with respect to x, treating y as a constant number:</p>
<div class="example-box">
<div class="label">Example: Partial Derivatives of f(x, y) = x² + 3xy + y²</div>
<p><strong>∂f/∂x:</strong> Treat y as a constant, differentiate with respect to x:</p>
<p>x² → 2x 3xy → 3y (y is a constant times x) y² → 0 (constant)</p>
<p><strong>∂f/∂x = 2x + 3y</strong></p>
<br>
<p><strong>∂f/∂y:</strong> Treat x as a constant, differentiate with respect to y:</p>
<p>x² → 0 (constant) 3xy → 3x (x is a constant times y) y² → 2y</p>
<p><strong>∂f/∂y = 3x + 2y</strong></p>
</div>
<p>That's it. Each partial derivative tells you how sensitive the output is to one specific input. If ∂f/∂x = 10 and ∂f/∂y = 2, then tweaking x has 5 times more effect than tweaking y.</p>
<div class="example-box">
<div class="label">Example: f(x, y, z) = 2x²y + z³ - 5xz</div>
<p><strong>∂f/∂x</strong> (y and z are constants): 4xy + 0 - 5z = <strong>4xy - 5z</strong></p>
<p><strong>∂f/∂y</strong> (x and z are constants): 2x² + 0 - 0 = <strong>2x²</strong></p>
<p><strong>∂f/∂z</strong> (x and y are constants): 0 + 3z² - 5x = <strong>3z² - 5x</strong></p>
</div>
<h3>The Gradient Vector</h3>
<p>If you collect <em>all</em> the partial derivatives into a single vector, you get the <strong>gradient</strong> (written ∇f, pronounced "nabla f" or "grad f"):</p>
<div class="formula-box">
∇f = (∂f/∂x, ∂f/∂y, ∂f/∂z, ...)<br><br>
For f(x, y) = x² + 3xy + y²:<br>
∇f = (2x + 3y, 3x + 2y)
</div>