diff --git a/7-calculas-tutorial.ipynb b/7-calculas-tutorial.ipynb
new file mode 100644
index 0000000..d9b3594
--- /dev/null
+++ b/7-calculas-tutorial.ipynb
@@ -0,0 +1,1044 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "0ad92c1a",
+ "metadata": {},
+ "source": [
+ "## The Derivative Function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3c73a9c6",
+ "metadata": {},
+ "source": [
+ "#### $y = x^3 - 2x^2 + 3x - 4$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "7a076a18",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " x.powi(3) - 2.0 * x.powi(2) + 3.0 * x - 4.0\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "43cee4c5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn derivative(f: fn(f64) -> f64, x: f64) -> f64 {\n",
+ " let delta_x = 1.0 / 1_000_000.0;\n",
+ " (f(x + delta_x) - f(x)) / delta_x\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "62eac785",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The derivative at x = 2 is: 7.000004001334048\n"
+ ]
+ }
+ ],
+ "source": [
+ "let x_value = 2.0;\n",
+ "let result = derivative(f, x_value);\n",
+ "println!(\"The derivative at x = {} is: {}\", x_value, result);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "f39cc3a1",
+ "metadata": {},
+ "source": [
+ "### Example 1: Exploring a Trigonometric Function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "29cd5843",
+ "metadata": {},
+ "source": [
+ "#### $y = \\sin(x)$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "4ffc4a08",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Derivatives of sin(x):\n",
+ "-3 -0.989992426175812\n",
+ "-2 -0.4161463817986544\n",
+ "-1 0.540302726670383\n",
+ "1 0.5403018851213304\n",
+ "2 -0.41614729129335615\n",
+ "0 0.9999999999998334\n",
+ "3 -0.9899925672851584\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "fn sin_function(x: f64) -> f64 {\n",
+ " x.sin()\n",
+ "}\n",
+ "\n",
+ "println!(\"Derivatives of sin(x):\");\n",
+ "for i in -3..=3 {\n",
+ " println!(\"{} {}\", i, derivative(sin_function, i as f64));\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "47f78b64",
+ "metadata": {},
+ "source": [
+ "### Example 2: Analyzing an Exponential Growth Function"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "60802361",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Derivatives of e^x:\n",
+ "-3 0.04978709326752817\n",
+ "-2 0.1353353508704025\n",
+ "-1 0.3678796250961014\n",
+ "0 1.0000004999621837\n",
+ "1 2.7182831874306146\n",
+ "2 7.38905979424942\n",
+ "3 20.08554696786291\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "fn exponential_function(x: f64) -> f64 {\n",
+ " x.exp()\n",
+ "}\n",
+ "\n",
+ "println!(\"Derivatives of e^x:\");\n",
+ "for i in -3..=3 {\n",
+ " println!(\"{} {}\", i, derivative(exponential_function, i as f64));\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "d225608d",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9c8745b4",
+ "metadata": {},
+ "source": [
+ "## Tangent Line Equation"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "35c17559",
+ "metadata": {},
+ "source": [
+ "### The Equation of Tangent Lines"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "502bbabb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " // Define the curve equation here, for example, a quadratic function\n",
+ " // Let's use f(x) = x^2 - 3x + 2 as an example\n",
+ " x.powi(2) - 3.0 * x + 2.0\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "170eccd7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn slope(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {\n",
+ " (y2 - y1) / (x2 - x1)\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "8be2678e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn derivative(f: fn(f64) -> f64, x: f64) -> f64 {\n",
+ " const EPSILON: f64 = 1e-8;\n",
+ " let x1 = x - EPSILON;\n",
+ " let x2 = x + EPSILON;\n",
+ " let y1 = f(x1);\n",
+ " let y2 = f(x2);\n",
+ " (y2 - y1) / (x2 - x1)\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "354d1ee0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn tangent_line(f: fn(f64) -> f64, x: f64) {\n",
+ " let m = derivative(f, x);\n",
+ " let y0 = f(x);\n",
+ " let b = y0 - m * x;\n",
+ "\n",
+ " println!(\"Equation of the tangent line at x = {:.2} is: y = {:.2}x + {:.2}\", x, m, b);\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "4af4fba5",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Equation of the tangent line at x = -0.48 is: y = -3.96x + 1.77\n",
+ "Equation of the tangent line at x = 0.67 is: y = -1.66x + 1.55\n"
+ ]
+ }
+ ],
+ "source": [
+ "let point_a_x = -0.48;\n",
+ "let point_b_x = 0.67;\n",
+ "\n",
+ "tangent_line(f, point_a_x);\n",
+ "tangent_line(f, point_b_x);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3bc36c3f",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2ef5ff91",
+ "metadata": {},
+ "source": [
+ "## Calculating Integrals"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "5ee1e4e8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " x.powi(3) - 2.0 * x.powi(2) + 1.0\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "id": "04591b23",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn integral_trapezoidal(f: fn(f64) -> f64, a: f64, b: f64, num_intervals: usize) -> f64 {\n",
+ " let h = (b - a) / num_intervals as f64;\n",
+ " let mut sum = 0.5 * (f(a) + f(b));\n",
+ "\n",
+ " for i in 1..num_intervals {\n",
+ " let x = a + i as f64 * h;\n",
+ " sum += f(x);\n",
+ " }\n",
+ "\n",
+ " sum * h\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "id": "5a0250bc",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The approximate area under the curve from x = 0 to x = 2 is: 0.66680\n"
+ ]
+ }
+ ],
+ "source": [
+ "let lower_limit = 0.0;\n",
+ "let upper_limit = 2.0;\n",
+ "let num_intervals = 100;\n",
+ "\n",
+ "let result = integral_trapezoidal(f, lower_limit, upper_limit, num_intervals);\n",
+ "println!(\"The approximate area under the curve from x = {} to x = {} is: {:.5}\", lower_limit, upper_limit, result);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e0fcf383",
+ "metadata": {},
+ "source": [
+ "### Using Trapezoids"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "id": "b14955d0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn trap_integral(f: fn(f64) -> f64, a: f64, b: f64, num: usize) -> f64 {\n",
+ " let width = (b - a) / num as f64;\n",
+ " let area = 0.5 * width * (f(a) + f(b) + 2.0 * (1..num).map(|n| f(a + width * n as f64)).sum::() as f64);\n",
+ " area\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "id": "4c10dbe8",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "With 5 trapezoids: 0.3400000000000001\n",
+ "With 10 trapezoids: 0.3350000000000001\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " x.powi(2)\n",
+ "}\n",
+ "\n",
+ "let result_with_5_trapezoids = trap_integral(f, 0.0, 1.0, 5);\n",
+ "let result_with_10_trapezoids = trap_integral(f, 0.0, 1.0, 10);\n",
+ "\n",
+ "println!(\"With 5 trapezoids: {}\", result_with_5_trapezoids);\n",
+ "println!(\"With 10 trapezoids: {}\", result_with_10_trapezoids);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "581805c4",
+ "metadata": {},
+ "source": [
+ "### Exercise: Finding the Area of a Circle Sector"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "id": "18116dd4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "use std::f64::consts::PI;\n",
+ "\n",
+ "fn circle_sector_area(radius: f64, angle: f64) -> f64 {\n",
+ " let angle_in_radians = angle * PI / 180.0;\n",
+ " let arc_length = radius * angle_in_radians;\n",
+ " let base_length = 2.0 * radius * angle_in_radians.sin();\n",
+ "\n",
+ " trap_integral(|x| radius * x.tan(), 0.0, angle_in_radians, 100) - 0.5 * base_length * arc_length\n",
+ "}\n",
+ "\n",
+ "fn trap_integral(f: F, a: f64, b: f64, num_trapezoids: usize) -> f64\n",
+ "where\n",
+ " F: Fn(f64) -> f64,\n",
+ "{\n",
+ " let width = (b - a) / num_trapezoids as f64;\n",
+ " let mut integral = 0.0;\n",
+ "\n",
+ " for i in 0..num_trapezoids {\n",
+ " let x0 = a + i as f64 * width;\n",
+ " let x1 = x0 + width;\n",
+ " let y0 = f(x0);\n",
+ " let y1 = f(x1);\n",
+ " integral += 0.5 * width * (y0 + y1);\n",
+ " }\n",
+ "\n",
+ " integral\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "id": "8cc68e55",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Area of a 30° sector: -5.83\n",
+ "Area of a 45° sector: -12.15\n",
+ "Area of a 60° sector: -19.21\n",
+ "Area of a 90° sector: 641326269700173.12\n",
+ "Area of a 120° sector: 1039938046564675.38\n",
+ "Area of a 150° sector: 661881748226897.38\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "let radius = 5.0;\n",
+ "let angles = [30.0, 45.0, 60.0, 90.0, 120.0, 150.0];\n",
+ "\n",
+ "for angle in angles.iter() {\n",
+ " let area = circle_sector_area(radius, *angle);\n",
+ " println!(\"Area of a {}° sector: {:.2}\", angle, area);\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0bbd0e9d",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3e6efbe7",
+ "metadata": {},
+ "source": [
+ "### Using Integrals to Solve Applied Problems"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "id": "87e70bae",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " x.sqrt()\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "id": "c23377c9",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "use std::f64::consts::PI;\n",
+ "\n",
+ "fn vol_solid(f: fn(f64) -> f64, a: f64, b: f64) -> f64 {\n",
+ " let mut volume = 0.0;\n",
+ " let num_slices = 1000;\n",
+ " let width = (b - a) / num_slices as f64;\n",
+ " for i in 0..num_slices {\n",
+ " let x = a + i as f64 * width;\n",
+ " let r = f(x);\n",
+ " let vol = PI * r.powi(2) * width;\n",
+ " volume += vol;\n",
+ " }\n",
+ " volume\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 29,
+ "id": "c36ae51a",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1.5692255304681022"
+ ]
+ },
+ "execution_count": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "let result = vol_solid(f, 0.0, 1.0);\n",
+ "result"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6fa27228",
+ "metadata": {},
+ "source": [
+ "### Exercise: Volume of a Cone"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "id": "99d5f34b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " x\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "id": "c46aa2c5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fn vol_cone(f: fn(f64) -> f64, a: f64, b: f64) -> f64 {\n",
+ " let mut volume = 0.0;\n",
+ " let num_cylinders = 10000;\n",
+ " let width = (b - a) / num_cylinders as f64;\n",
+ "\n",
+ " for i in 0..num_cylinders {\n",
+ " let x = a + i as f64 * width;\n",
+ " let r = f(x);\n",
+ " let vol = PI * r.powi(2) * width;\n",
+ " volume += vol;\n",
+ " }\n",
+ "\n",
+ " volume\n",
+ "}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "id": "c3157aa2",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Volume of the cone: 130.88\n"
+ ]
+ }
+ ],
+ "source": [
+ "let result = vol_cone(f, 0.0, 5.0);\n",
+ "println!(\"Volume of the cone: {:.2}\", result);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ca4687e0",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9ebace14",
+ "metadata": {},
+ "source": [
+ "## Using Derivatives to Solve Optimization Problems"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "cd5059fd",
+ "metadata": {},
+ "source": [
+ "### Exercise 1: Maximizing a Cubic Function"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "id": "e9b923e2",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Local maximum at x = 0.75\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn f(x: f64) -> f64 {\n",
+ " -x.powi(3) + 3.0 * x.powi(2) + 9.0 * x - 8.0\n",
+ "}\n",
+ "\n",
+ "fn f_prime(x: f64) -> f64 {\n",
+ " -3.0 * x.powi(2) + 6.0 * x + 9.0\n",
+ "}\n",
+ "\n",
+ "fn newton_method(f: fn(f64) -> f64, f_prime: fn(f64) -> f64, initial_guess: f64, max_iterations: usize) -> f64 {\n",
+ " let mut x = initial_guess;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta = f(x) / f_prime(x);\n",
+ " x -= delta;\n",
+ "\n",
+ " if delta.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " x\n",
+ "}\n",
+ "\n",
+ "let critical_point = newton_method(f, f_prime, 0.0, 100);\n",
+ "println!(\"Local maximum at x = {:.2}\", critical_point);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3180fa95",
+ "metadata": {},
+ "source": [
+ "### Exercise 2: Minimizing Total Cost"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "id": "6f7622aa",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Optimal price: $166.67\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn cost(x: f64) -> f64 {\n",
+ " 5000.0 + 20.0 * x\n",
+ "}\n",
+ "\n",
+ "fn revenue(x: f64) -> f64 {\n",
+ " 50.0 * x\n",
+ "}\n",
+ "\n",
+ "fn profit(x: f64) -> f64 {\n",
+ " revenue(x) - cost(x)\n",
+ "}\n",
+ "\n",
+ "fn profit_prime(x: f64) -> f64 {\n",
+ " 50.0 - 20.0\n",
+ "}\n",
+ "\n",
+ "fn newton_method(f: fn(f64) -> f64, f_prime: fn(f64) -> f64, initial_guess: f64, max_iterations: usize) -> f64 {\n",
+ " let mut x = initial_guess;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta = f(x) / f_prime(x);\n",
+ " x -= delta;\n",
+ "\n",
+ " if delta.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " x\n",
+ "}\n",
+ "\n",
+ "let optimal_price = newton_method(profit, profit_prime, 50.0, 100);\n",
+ "println!(\"Optimal price: ${:.2}\", optimal_price);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0b758ba9",
+ "metadata": {},
+ "source": [
+ "### Exercise 3: Maximizing Profit in a Market"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "id": "81a14b23",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Equilibrium quantity: 0.00 units\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn demand(x: f64) -> f64 {\n",
+ " 100.0 - x\n",
+ "}\n",
+ "\n",
+ "fn supply(x: f64) -> f64 {\n",
+ " 20.0 + 2.0 * x\n",
+ "}\n",
+ "\n",
+ "fn price(x: f64) -> f64 {\n",
+ " (demand(x) + supply(x)) / 2.0\n",
+ "}\n",
+ "\n",
+ "fn profit(x: f64) -> f64 {\n",
+ " price(x) * x - (20.0 + 2.0 * x) * x\n",
+ "}\n",
+ "\n",
+ "fn profit_prime(x: f64) -> f64 {\n",
+ " 100.0 - 4.0 * x\n",
+ "}\n",
+ "\n",
+ "fn newton_method(f: fn(f64) -> f64, f_prime: fn(f64) -> f64, initial_guess: f64, max_iterations: usize) -> f64 {\n",
+ " let mut x = initial_guess;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta = f(x) / f_prime(x);\n",
+ " x -= delta;\n",
+ "\n",
+ " if delta.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " x\n",
+ "}\n",
+ "\n",
+ "let equilibrium_quantity = newton_method(profit, profit_prime, 20.0, 100);\n",
+ "println!(\"Equilibrium quantity: {:.2} units\", equilibrium_quantity);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1fde5f6a",
+ "metadata": {},
+ "source": [
+ "### Exercise 4: Minimizing Loss"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "id": "9d3d1fd3",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Time of minimum loss: 6.67 months\n",
+ "Minimum loss: $-0.00\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn stock_value(t: f64) -> f64 {\n",
+ " 1000.0 - 20.0 * t + 3.0 * t.powi(2)\n",
+ "}\n",
+ "\n",
+ "fn stock_loss(t: f64) -> f64 {\n",
+ " 1000.0 - stock_value(t)\n",
+ "}\n",
+ "\n",
+ "fn stock_loss_prime(t: f64) -> f64 {\n",
+ " -(-20.0 + 6.0 * t)\n",
+ "}\n",
+ "\n",
+ "fn newton_method(f: fn(f64) -> f64, f_prime: fn(f64) -> f64, initial_guess: f64, max_iterations: usize) -> f64 {\n",
+ " let mut x = initial_guess;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta = f(x) / f_prime(x);\n",
+ " x -= delta;\n",
+ "\n",
+ " if delta.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " x\n",
+ "}\n",
+ "\n",
+ "let time_min_loss = newton_method(stock_loss, stock_loss_prime, 5.0, 100);\n",
+ "let min_loss = stock_loss(time_min_loss);\n",
+ "println!(\"Time of minimum loss: {:.2} months\", time_min_loss);\n",
+ "println!(\"Minimum loss: ${:.2}\", min_loss);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6a1e851a",
+ "metadata": {},
+ "source": [
+ "### Exercise 5: Maximizing Crop Yield"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "id": "ec9f12b5",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Maximized crop yield: NaN square meters\n",
+ "Dimensions of the rectangular field: NaN meters x NaN meters\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn crop_yield(length:\n",
+ "\n",
+ " f64, width: f64) -> f64 {\n",
+ " length * width\n",
+ "}\n",
+ "\n",
+ "fn crop_yield_prime_length(length: f64, width: f64) -> f64 {\n",
+ " width\n",
+ "}\n",
+ "\n",
+ "fn crop_yield_prime_width(length: f64, width: f64) -> f64 {\n",
+ " length\n",
+ "}\n",
+ "\n",
+ "fn newton_method_2d(f: fn(f64, f64) -> f64, f_prime_x: fn(f64, f64) -> f64, f_prime_y: fn(f64, f64) -> f64, initial_guess_x: f64, initial_guess_y: f64, max_iterations: usize) -> (f64, f64) {\n",
+ " let mut x = initial_guess_x;\n",
+ " let mut y = initial_guess_y;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta_x = f(x, y) / f_prime_x(x, y);\n",
+ " let delta_y = f(x, y) / f_prime_y(x, y);\n",
+ " x -= delta_x;\n",
+ " y -= delta_y;\n",
+ "\n",
+ " if delta_x.abs() < tolerance && delta_y.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " (x, y)\n",
+ "}\n",
+ "\n",
+ "let (length, width) = newton_method_2d(crop_yield, crop_yield_prime_length, crop_yield_prime_width, 20.0, 20.0, 100);\n",
+ "let yield_max = crop_yield(length, width);\n",
+ "println!(\"Maximized crop yield: {:.2} square meters\", yield_max);\n",
+ "println!(\"Dimensions of the rectangular field: {:.2} meters x {:.2} meters\", length, width);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e39f9937",
+ "metadata": {},
+ "source": [
+ "### Exercise 6: Minimizing Surface Area"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "id": "78413a5b",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Minimized surface area: NaN square centimeters\n",
+ "Dimensions of the rectangular box: NaN cm x NaN cm x NaN cm\n"
+ ]
+ }
+ ],
+ "source": [
+ "fn box_surface_area(length: f64, width: f64, height: f64) -> f64 {\n",
+ " 2.0 * (length * width + length * height + width * height)\n",
+ "}\n",
+ "\n",
+ "fn box_volume(length: f64, width: f64, height: f64) -> f64 {\n",
+ " length * width * height\n",
+ "}\n",
+ "\n",
+ "fn box_volume_prime_length(length: f64, width: f64, height: f64) -> f64 {\n",
+ " width * height\n",
+ "}\n",
+ "\n",
+ "fn box_volume_prime_width(length: f64, width: f64, height: f64) -> f64 {\n",
+ " length * height\n",
+ "}\n",
+ "\n",
+ "fn box_volume_prime_height(length: f64, width: f64, height: f64) -> f64 {\n",
+ " length * width\n",
+ "}\n",
+ "\n",
+ "fn newton_method_3d(f: fn(f64, f64, f64) -> f64, f_prime_x: fn(f64, f64, f64) -> f64, f_prime_y: fn(f64, f64, f64) -> f64, f_prime_z: fn(f64, f64, f64) -> f64, initial_guess_x: f64, initial_guess_y: f64, initial_guess_z: f64, max_iterations: usize) -> (f64, f64, f64) {\n",
+ " let mut x = initial_guess_x;\n",
+ " let mut y = initial_guess_y;\n",
+ " let mut z = initial_guess_z;\n",
+ " let mut iterations = 0;\n",
+ " let tolerance = 1e-6;\n",
+ "\n",
+ " while iterations < max_iterations {\n",
+ " let delta_x = f(x, y, z) / f_prime_x(x, y, z);\n",
+ " let delta_y = f(x, y, z) / f_prime_y(x, y, z);\n",
+ " let delta_z = f(x, y, z) / f_prime_z(x, y, z);\n",
+ " x -= delta_x;\n",
+ " y -= delta_y;\n",
+ " z -= delta_z;\n",
+ "\n",
+ " if delta_x.abs() < tolerance && delta_y.abs() < tolerance && delta_z.abs() < tolerance {\n",
+ " break;\n",
+ " }\n",
+ "\n",
+ " iterations += 1;\n",
+ " }\n",
+ "\n",
+ " (x, y, z)\n",
+ "}\n",
+ "\n",
+ "let (length, width, height) = newton_method_3d(box_volume, box_volume_prime_length, box_volume_prime_width, box_volume_prime_height, 10.0, 10.0, 20.0, 100);\n",
+ "let min_surface_area = box_surface_area(length, width, height);\n",
+ "println!(\"Minimized surface area: {:.2} square centimeters\", min_surface_area);\n",
+ "println!(\"Dimensions of the rectangular box: {:.2} cm x {:.2} cm x {:.2} cm\", length, width, height);"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "90817bb2",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Rust",
+ "language": "rust",
+ "name": "rust"
+ },
+ "language_info": {
+ "codemirror_mode": "rust",
+ "file_extension": ".rs",
+ "mimetype": "text/rust",
+ "name": "Rust",
+ "pygment_lexer": "rust",
+ "version": ""
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}