Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Experience overflow in a function. When an object (ball, car, airplane) moves through the air, there is a very, very thin layer of air close to the object's surface where the air velocity varies dramatically \(^{18}\), from the same value as the velocity of the object at the object's surface to zero a few centimeters away. The change in velocity is quite abrupt and can be modeled by the functiion $$ v(x)=\frac{1-e^{x / \mu}}{1-e^{1 / \mu}} $$ where \(x=1\) is the object's surface, and \(x=0\) is some distance away where one cannot notice any wind velocity \(v\) because of the passing object \((v=0)\). The vind velocity coincides with the velocity of the object at \(x=1\), here set to \(v=1\). The parameter \(\mu\) is very small and related to the viscosity of air. With a small value of \(\mu\), it becomes difficult to calculate \(v(x)\) on a computer. Make a function \(v(x, m u=1 E-6\), exp=math.exp) for calculating the formula for \(v(x)\) using exp as a possibly user-given exponentional function. Let the v function return the nominator and denominator in the formula as well as the fraction (result). Call the v function for various \(\mathrm{x}\) values between 0 and 1 in a for loop, let mu be \(1 \mathrm{E}-3\), and have an inner for loop over two different exp functions: math. exp and numpy. exp. The output will demonstrate how the denominator is subject to overflow and how difficult it is to calculate this function on a computer. Also plot \(v(x)\) for \(\mu=1,0.01,0.001\) on \([0,1]\) using 10,000 points to see what the function looks like. Name of program file: boundary_layer_func1.py.

Short Answer

Expert verified
The function 'v(x)' is defined for velocity and its implementation reveals computational difficulties at small values of \( \mu \) especially with different exp functions.

Step by step solution

01

Define the Function

Start by defining the function `v(x, mu=1E-6, exp=math.exp)` in Python. This function will calculate the velocity based on the given formula. The function should return three values: the numerator, denominator, and the velocity `v(x)`. Use `exp` to compute the exponential expressions.
02

Implement the Formula

Inside the function `v(x)`, calculate the numerator as `1 - exp(x / mu)` and the denominator as `1 - exp(1 / mu)`. Then compute the fraction (velocity) by dividing the numerator by the denominator, i.e., `fraction = numerator / denominator`.
03

Setup the Iteration for Different x Values

Create a loop to iterate over various `x` values between 0 and 1. You can use `numpy.linspace(0, 1, number_of_points)` to generate the values for `x`. In this problem, `number_of_points` can be something like 10000 for a smooth plot.
04

Test with Different exp Functions

Within the loop for `x`, create an inner loop for two different exponential functions: `math.exp` and `numpy.exp`. Call the function `v(x, mu=1E-3, exp=chosen_exp_function)` using each of these functions.
05

Plot the Function for Various mu Values

Generate plots for `v(x)` with different values of `mu`: `1`, `0.01`, and `0.001`. For each `mu`, create a separate run of the loop with `x` values, and store the results of `v(x)` for plotting. Use a plotting library like `matplotlib` to visualize `v(x)` over the range `[0, 1]`.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Numerical Stability
Numerical stability is a significant concept in computational mathematics, particularly when dealing with algorithms that involve very small or very large numbers. In the given exercise, the function for modeling air velocity changes shows how tricky numerical calculations can become. When the parameter \( \mu \) is very small, it can lead to large exponentials, causing an overflow problem in the expression \( 1 - e^{1/\mu} \). This phenomenon occurs because of the limits of floating-point representation in computers, which may not handle extremely large or small numbers well.
Understanding numerical stability helps in designing algorithms that minimize the effect of rounding errors and prevent overflow. Techniques such as scaling or using logarithmic transformations can sometimes be applied to improve stability. For instance, in this exercise, using the exponential functions wisely and testing them under different conditions allows revealing the instability issues with computation for very small values of \( \mu \). This approach enlightens students about the practical constraints in computational mathematics.
To summarize, numerical stability is about ensuring that small errors in computation do not lead to significant inaccuracies in results. In the world of algorithm design and computational mathematics, keeping a check on numerical stability is crucial.
Exponential Functions
Exponential functions play a pivotal role in this exercise as they are used to model rapid changes in air velocity near moving objects. The main feature of an exponential function, such as \( e^{x} \), is its rapid rate of increase or decrease, which provides a good model for phenomena with abrupt transitions, like those in fluid dynamics.
In the function \( v(x) = \frac{1 - e^{x/\mu}}{1 - e^{1/\mu}} \), both the numerator and the denominator incorporate exponential terms. These terms are sensitive to the parameter \( \mu \). A smaller \( \mu \) increases the rate of change, potentially leading to computational difficulties, such as overflow, when executed on a computer. By testing different exponential implementations (like `math.exp` and `numpy.exp`), one can observe how these implementations handle large or small exponentials, highlighting their strengths and weaknesses.
In computational mathematics, understanding how exponential functions operate can aid in choosing the right library or function for specific tasks, especially when precision and computational limits are a concern.
Plotting with Matplotlib
Plotting functions with `matplotlib` offers a visual insight into the behavior of the model. In this exercise, plotting \( v(x) \) for different \( \mu \) values allows us to visualize how the air velocity transitions from the object's surface outwards.
`Matplotlib` is a powerful Python library that lets you create a wide range of graphs and plots. To use it effectively, you typically start by importing the library and then creating a range of data points, often using `numpy` to produce evenly spaced values over a specified range. In our case, plotting 10,000 points between 0 and 1 provides a detailed curve reflecting the changes in velocity.
Visualizing these plots for multiple \( \mu \) values \([1, 0.01, 0.001]\) helps in understanding the influence of the parameter on the appearance of the boundary layer. Variations in the plot demonstrate how the transition zone in air velocity evolves with \( \mu \), thus making the theoretical understanding of the function more tangible. `Matplotlib` not only helps in representing the data but also in offering an excellent platform for comparing results visually.
Python Programming
Python is an incredibly versatile language, ideal for computational mathematics thanks to its readability and wide array of libraries. In this exercise, Python is employed to calculate, test, and visualize the function for air velocity changes across an object's surface.
Defining the function `v(x, mu=1E-6, exp=math.exp)` in Python allows for flexibility and reusability. It requires careful coding to manage issues like overflow and ensures the function returns the numerator, denominator, and velocity as expected. Python's `math` and `numpy` libraries provide the foundation for handling exponentials, where `numpy` can be more efficient for array operations.
Creating loops and testing different exponential functions in Python showcases the language's capability in iterative computations. It efficiently employs `numpy.linspace()` for generating values and can handle large computations, such as the 10,000 data points required for smooth plotting with `matplotlib`.
Overall, Python stands out for its simplicity and the abundance of libraries it offers, making it an excellent choice for students and professionals alike who engage in computational tasks and data visualization.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Explore a function graphically. The wave speed \(c\) of water surface waves depends on the length \(\lambda\) of the waves. The following formula relates \(c\) to \(\lambda\) : $$ c(\lambda)=\sqrt{\frac{g \lambda}{2 \pi}\left(1+s \frac{4 \pi^{2}}{\rho g \lambda^{2}}\right) \tanh \left(\frac{2 \pi h}{\lambda}\right)} $$ where \(g\) is the acceleration of gravity, \(s\) is the air-water surface tension \(\left(7.9 \cdot 10^{-4} \mathrm{~N} / \mathrm{cm}\right), \rho\) is the density of water (can be taken as \(\left.1 \mathrm{~kg} / \mathrm{cm}^{3}\right)\), and \(h\) is the water depth. Let us fix \(h\) at \(50 \mathrm{~m}\). First make a plot of \(c(\lambda)\) for small \(\lambda(1 \mathrm{~mm}\) to \(10 \mathrm{~cm})\). Then make a plot \(c(\lambda)\) for larger \(\lambda\) ) (1 m to \(2 \mathrm{~km}\) ). Name of program file: water_wave_velocity.py.

Plot a smoothed "hat" function. The "hat" function \(N(x)\) defined by (3.5) on page 109 has a discontinuity in the derivative at \(x=1\). Suppose we want to "round" this function such that it looks smooth around \(x=1\). To this end, replace the straight lines in the vicinity of \(x=1\) by a (small) cubic curve $$ y=a(x-1)^{3}+b(x-1)^{2}+c(x-1)+d $$ for \(x \in[1-\epsilon, 1+\epsilon]\), where \(a, b, c\), and \(d\) are parameters that must be adjusted in order for the cubic curve to match the value and the derivative of the function \(N(x)\). The new rounded functions has the specification $$ \tilde{N}(x)= \begin{cases}0, & x<0 \\ x, & 0 \leq x<1-\epsilon \\\ a_{1}(x-1)^{3}+b(x-1)+c(x-1)+d_{1}, & 1-\epsilon \leq x<1 \\\ a_{2}(x-1)^{3}+b(x-1)+c(x-1)+d_{2}, & 1 \leq x<1+\epsilon \\ 2-x, & 1+\epsilon \leq x<2 \\ 0, & x \geq 2\end{cases} $$ with \(a_{1}=\frac{1}{3} \epsilon^{-2}, a_{2}=-a_{1}, d_{1}=1-\epsilon+a_{1} \epsilon^{3}, d_{2}=1-\epsilon-a_{2} \epsilon^{3}\), and \(b=c=0 .\) Plot this function. (Hint: Be careful with the choice of \(x\) coordinates!) Name of program file: plot_hat.py.

Plot the velocity profile for pipeflow. A fluid that flows through a (very long) pipe has zero velocity on the pipe wall and a maximum velocity along the centerline of the pipe. The velocity \(v\) varies through the pipe cross section according to the following formula: $$ v(r)=\left(\frac{\beta}{2 \mu_{0}}\right)^{1 / n} \frac{n}{n+1}\left(R^{1+1 / n}-r^{1+1 / n}\right) $$ where \(R\) is the radius of the pipe, \(\beta\) is the pressure gradient (the force that drives the flow through the pipe), \(\mu_{0}\) is a viscosity coefficient (small for air, larger for water and even larger for toothpaste), \(n\) is a real number reflecting the viscous properties of the fluid ( \(n=1\) for water and air, \(n<1\) for many modern plastic materials), and \(r\) is a radial coordinate that measures the distance from the centerline \((r=0\) is the centerline, \(r=R\) is the pipe wall). Make a function that evaluates \(v(r) .\) Plot \(v(r)\) as a function of \(r \in[0, R]\), with \(R=1, \beta=0.02, \mu=0.02\), and \(n=0.1 .\) Thereafter, make an animation of how the \(v(r)\) curves varies as \(n\) goes from 1 and down to \(0.01\). Because the maximum value of \(v(r)\) decreases rapidly as \(n\) decreases, each curve can be normalized by its \(v(0)\) value such that the maximum value is always unity. Name of program file: plot_velocity_pipeflow.py.

Plot a formula for several parameters. Make a program that reads a set of \(v_{0}\) values from the command line and plots the corresponding curves \(y(t)=v_{0} t-0.5 g t^{2}\) in the same figure (set \(g=9.81\) ). Let \(t \in\left[0,2 v_{0} / g\right]\) for each curve, which implies that you need a different vector of \(t\) coordinates for each curve. Name of program file: plot_bal12.py.

Plot a formula. Make a plot of the function \(y(t)=v_{0} t-0.5 g t^{2}\) for \(v_{0}=10, g=9.81\) and \(t \in\left[0,2 v_{0} / g\right] .\) The label on the \(x\) axis should be 'time (s)' and the label on the \(y\) axis should be 'height \((\mathrm{m}) '\). Name of program file: plot_ball1.py.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free