Homework 9¶
Problem 1¶
Study carefully all of the material in the lecture notes on loops (the key version) and on the Newton examples. I recommend that you type what you see. It is important to read, and understand, and internalize. Ask yourself questions: why is it like this? what if I try that? Try coming up with your own test problems.
In a markdown cell below, just state if you did this.
Problem 2¶
Use for loops that will output the multiplication table for numbers from 1 to 10 inclusive. The output should look like a table with rows and columns, but they don't have to be perfectly aligned (though that is easy with a formatted string like print(f"{x:4d})" where x is the variable or expression holding the number you are printing). Note, print can be called with the optional argument end, like so:
print(f"{x:4d})", end="")
This will print x without making a new line.
You don't need column and row headings, just the product. For 3x2 = 6, you just need the "6" in the correct position of the table.
Problem 3¶
As we go up in the atmosphere, the pressure and temperature decrease. We can calculate how they change by using a force balance to obtain
$$\frac{dP}{dz} = -\rho g,$$
where $P$ is pressure, $z$ is height, $\rho$ is air density, and $g$ is gravitational acceleration. We can use the ideal gas law:
$$\rho = \frac{M P}{R T},$$
where $M$ is the mean molecular weight of air, $T$ is temperature (K), and $R$ is the gas constant. If we insert this equation for $\rho$ in the first equation, separate variables, and integrate, we get
$$P_{iso} = P_{sea}\exp(-Mgz/(RT)),$$
where $P_{sea}$ = 1 atm. This equation assumes constant temperature (isothermal).
Take M=29 kg/kmol, g=9.81 m/$s^2$, T=300 K.
Write a function that will take z as a function argument and return the pressure (blue equation). Evaluate this function in a loop and print the value of z and P at uniform values of z between z=0 and z= 30,000 ft (Mt. Everest). Output around 30 values. Your output on each line should look something like
z (m), P (atm) = 100, 0.9
Problem 4¶
Write a function called EE to solve the following rate equation using the Explicit Euler method.
$$\frac{dT}{dt} = \frac{hA}{mc_p}(T_f-T),$$ $$T_0 = 500\,\,K.$$
This equation represents the convective heating of an object of mass $m$, area $A$, and heat capacity $c_p$.
- Recall, for explicit Euler, for the rate equation $dy/dt = f(y,t)$, we have
$$y_{n+1} = y_n + \Delta t f(y_n,t_n).$$
Use the following
- $h$ = 1 W/$m^2$*K
- $A$ = 1 $m^2$
- $m$ = 1 kg
- $c_p$ = 1 J/kg*K
- $T_f$ = 1000 K
Use a stepsize $\Delta t=0.2$, and take 30 steps.
Output the time, and temperature at each step.
(We'll plot the resulting $T(t)$ when we learn to plot in Python.)