{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating Functions\n",
    "Functions are isolated code blocks that can be executed at any point of your script. Functions are defined by their **name**, their **parameters** and their **return value**. The  function can then be called by using it’s name and providing the necessary parameters.  \n",
    "Use the following syntaxe to define and call your function :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def function_name(param1, param2, param3=9):\n",
    "    # ---- Your function code\n",
    "    value = (param1 + param2 + param3) / 3\n",
    "    # ---\n",
    "    return value\n",
    "\n",
    "mean = function_name(2, 4, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Parameters can be of any type if not implied otherwise. They can also be given a default value by writing (_param = default_) . If not given a value when calling the function, this parameter will take its default value.  \n",
    "Use functions either to do repetitive actions, or make your code more readable by separating and naming relevant code blocks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task 1 : Learning Functions\n",
    "\n",
    "Create a function that takes into account mass and velocity, checks if the mass is positive, and returns the kinetic energy. Recall : $E_{kin}=\\frac{1}{2}mv^2$.\n",
    "Call this function for different values.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Consider a satellite of mass m  orbiting the earth at a distance r. Create a function that takes the distance from earth and returns their orbit speed (considering circular orbit). Recall $v=\\sqrt{\\frac{GM}{r}}$, where  M is the mass of the earth and G the gravitational constant. Set G and M as parmeters of your function with default values.  \n",
    "What is the orbit speed of the ISS ? How would that speed change if the gravitationnal constant was doubled ? And if the ISS was 1 km higher ?    \n",
    "  \n",
    "$G=\\ 6.67430\\times{10}^{-11}\\frac{Nm^2}{kg^2}$,  \n",
    "$M_{earth}=5.97\\times{10}^{24}\\ kg$,  \n",
    "$r_{ISS}=6.78\\times{10}^6\\ m$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Task 2 : "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using special observations techniques, you were able to observe the velocity of a sample of space debris in the Earth’s close orbit.  \n",
    "Generate a sample of N=100 velocities following a gaussian distribution  $\\mathcal{N}(7500\\ km,\\ \\ (250\\ km)²)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a function to relate a single velocity to the altitude (take into account the Earth’s radius and beware dimensions). Call this function on the first element of the velocity list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Call this function, but pass the entire array of velocities instead of a single one. What do you see ? What is the difference between giving the function a velocity or an array of velocities ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You are now leading a small EPFL startup that plans on cleaning the Earth’s orbit. With your current ressources, you can only go to 500km altitude.  \n",
    "Create a functions that takes an altitude from the previous step and returns either the unchanged value if you can reach this debris, or a NaN if not ( $np.nan$ ).  \n",
    "Call this function with your array of altitudes. What do you see ? Is this expected ?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The $np.vectorize(myfunc)$ function return your function transformed into an element wise function, that is to say calling your function with an array now applies the function on every point of your array.  \n",
    "Vectorize your last function and try again. What do you see ?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To take the mean and std of an array while ignoring the NaNs, use np.nanmean() and np.nanstd().  \n",
    "What is the mean and standard deviation of the reachable debris ?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
