Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
"Guiding Future STEM Leaders through Innovative Research Training" ~ thinkingbeyond.education
Image: ubuntu2204
#Summary of code
The purpose of this code is to create a piecewise constant function that the user can interact with, to aid understanding of how a piecewise constant function can approximate a continuous function. The user can choose the continuous function being approximated out of 3 choices. When the functions are plotted, the user can change the number of sections in the piecewise function to see that when the number of sections increases, the approximation improves.
#Installing packages
Importing packages
#Defining a function which applies the chosen function
This function allows us to use the same code to calculate the outputs of the continuous function regardless of the function chosen.
#Defining a function which creates a list of outputs for the piecewise function
For i from 0 to n_sections (the number of sections that the piecewise function has), if each in the list of inputs ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 17: …frac{i}{\text{n_̲sections}}\leq …, then the output of the piecewise function for this input x is ParseError: KaTeX parse error: Expected 'EOF', got '_' at position 19: …frac{i}{\text{n_̲sections}}), where is the continuous function chosen.
ensures that the last value is not missed, otherwise this will result in a value of 0 for the final output value. All the outputs are stored in an array y_1 which the function returns.
#Defining a function which plots both functions
This functions defines the input space, which is 1000 points equally spaced from 0 to 1. Then, it uses the piecewise_function function to define the outputs for the piecwise function. Then, it plots both functions. The outputs for the continuous function is defined simply but applying the continuous function chosen to all values in x.
#Defining functions for when a function is chosen (by clicking one of three buttons)
Each of these functions are called when the corresponding button is pressed. They set the value of chosen_func to represent the correct function, which stores the choice of function. Then they call the function to plot the piecewise and continuous function, with a slider allowing the user to control the number of sections in the function.
#Creating buttons for choosing a continuous function
This code creates buttons for each function. When each button is pressed, it calls its corresponding function, which in turn calls a function which plots the correct graphs.
#Displaying the buttons
Finally, the buttons are displayed.