<jupyter_start><jupyter_text>(c) <NAME> 2019. Thanks to Universidad EAFIT for support. This material is part of the course Introduction to Finite Element Analysis  Introduction to notebooks A **Notebook (NB)** is a digital document that combines computer code with enriched text elements such as text, equations, figures, videos, etc. Tipically a NB can be easily read allowing to deliver theoretical information together with executable code snippets that can be tested interactively.In this introductory Finite Element Method (FEM) course we will use notebooks as support material within the flipped class methodology. In that context NBs will allow the instructor to maintain the flow of the discussion treating the theoretical aspects in a condensed form and at the same time vary problem parameters, data and analysis techniques in an interactive fashion while establishing a direct connection between the mathematical respresentation in terms of equations and computer codes. **After completing this notebook you should be able to:*** Create independent notebooks and modify existing ones.* Represent simple numerical tasks in terms of computer algorithms.* Understand basic programming structures and its Python representation.  Text and code In a NB it is possible to combine text and code to describe a subject and conduct verifications, for instance, through parametric analyses.Equations in a NB can be written using [$\LaTeX$](https://katex.org/) like:$$f(x) = x^3+4x^2-10.$$It is also possible to insert images which are available in memory **(double click in the image to see the used command)**   Control structures In order to show how code snippets are introduced in NBs and used in Python we will describe the basic data flow structures used in computer programming. We will use combinations of text describing the structure and its corresponding Python implementation.The first block of code in every Python program imports (or activates) modules or **libraries** which are sets of subroutines to perform a family of specific taks. For instance, in the following case we import the modulus **numpy** and **matplotlib**.**Questions:***** Find out what type of computing tasks are performed by the subroutines in the module numpy.*****Find out what type of computing tasks are performed by the subroutines in the module matplotlib.**<jupyter_code>%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Image<jupyter_output><empty_output><jupyter_text>Sequential structure The most simple algorithmic form corresponds to a sequential structure where a series of consecutive commandsexecute one after the other (see figure).The sequential structure is illustrated by the following code where we compute the average of two numbers.<jupyter_code>A = 45
B = 20
sum = A + B
prom = sum/2
print(prom)<jupyter_output>32.5<jupyter_text>Simple IfAs shown in the figure in this structure a group of sequntial commands is executed only if the condition specified by **condition** is true.In the following code snippet we ask the user to input the radious of a circle. The code computes the resulting area provided that the value given by the user is in fact greater than 0.0.<jupyter_code>R = 1.0
if R > 0.0:
    Area = np.pi*R**2
print(Area)<jupyter_output>3.141592653589793<jupyter_text>Double IfThe double if includes an alternative path for the flow in case **condition** is false.This structure is explained in the following code where we check if a number is odd or even using the **%** operator.**What are the arithmetic operators in Python?**<jupyter_code>num = 4
if (num % 2) == 0:
    print('The number is even')
else:
    print('The number is odd')<jupyter_output>The number is even<jupyter_text>While loop.In this structure a sequence of commands is repeteadly executed as long as the condition specified by **condition** is true. The execution stops once the condition changes to false. For the structure to execute at least once the condition specified by **condition** must be true right before the beginning and to avoid the structure executing indefinetly the possibility for the condition changing to false must exist within the structure.In the following block the integer variable *icount* is initially set to zero and the loop executes as long as *icount* is smaller than 5. After each iterattion *icount* is incremented by 1 until it reaches 5 which sets **condition** to false.<jupyter_code>icount = 0
while icount < 5:
    icount = icount + 1
print(icount)<jupyter_output>5<jupyter_text>Until loop.This structure is similar to the **While** stucture but now **condition** is evaluated at the end of the loop.**Find out what is the form (if any) of the *Until* structure in Python**  For loop.This structure executes a sequential list of commands a finite number of times according to a counter which is controlled by a prescribed limit.**Find out the specific use of the *For* structure in Python**<jupyter_code>for i in range(5):
    print('Tha value of the counter is', i)<jupyter_output>Tha value of the counter is 0
Tha value of the counter is 1
Tha value of the counter is 2
Tha value of the counter is 3
Tha value of the counter is 4<jupyter_text>Defining and plotting a functionIn the following block of code we show how to define and plot a function. To declare the function we will use the intrinsic Python option **lambda** which allows us to declare the function and leave it ready to perform a mapping between numbers pretty much like in standard calculus. Once the function is defined we will use the **matplotlib** function **plot()**.<jupyter_code>def fx(x):
    return x**3+4.0*x**2-10.0


npts = 200
yy = np.zeros((npts))
xx = np.linspace(-1, 1, npts)
yy[:] = fx(xx[:])
plt.figure(0)
plt.plot(xx, yy, 'r--')<jupyter_output><empty_output><jupyter_text>Inserting a Video The Python function **YouTubeVideo** allows insertig youtube videos in Notebooks as shown in the following code snippet.<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo('ibeIKaSYG2U')<jupyter_output><empty_output><jupyter_text>Glossary of terms**Flow structure:** In programming a flow structure is a logical statement that chooses a code path based on values of prescribed parameters.**Loop:** A flow structure that executes a sequence of commands a prescribed or variable number of times.**Counter:** This is the typical name given to an integer variable that is used to count events in a program.**Flag:** An integer variable that usually takes the value of $0$ or $1$ and is used to evaluate a condition in a block of code.  Class activity In the flipped classroom format of this course most of the work is developed during the class where the students perform activities under the orientation of the instructor. Many of these activities involve either completing specific parts of a notebook or creating a new notebook from scratch.In this activity the students are required to find and plot the first derivative of the function:$$f(x) = x^3+4x^2-10$$in the interval $[-1 , 1]$ using Python explicit diffrentiation. **Add comments to explain each one of the used commands.**<jupyter_code>from sympy import *
zz = np.zeros((npts))
x = symbols('x')
N = x**3+4.0*x**2-10.0
fdx = diff(N, x)
for i in range(npts):
    zz[i] = fdx.subs([(x, xx[i])])
print(fdx)
plt.figure(1)
plt.plot(xx, zz, 'r--')
from IPython.core.display import HTML
def css_styling():
    styles = open('./nb_style.css', 'r').read()
    return HTML(styles)
css_styling()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0674350821409645, 'fraction_numerical': 0.011526232114467409, 'mean_word_length': 5.147394136807818, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '604802', 'license': 'MIT', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1917, 'n_words': 1065, 'path': 'notebooks/00a_introductory_notebook.ipynb', 'repo_name': 'AppliedMechanics-EAFIT/Introductory-Finite-Elements', 'size': '7552'}