首页 > > 详细

代做Numbers and Arrays作业、代写Python语言作业、Python程序设计作业调试 代做数据库SQL|代写Python程序

Exercise 1: Numbers and Arrays
Consider the following function, which depends on the integer and the real variable :
a. Evaluate this function for the six values corresponding to N=1,2,3 and x=1,2 using python as a
calculator. Create a separate jupyter cell for each result.
# HINT: exponentiation is accomplished with **
b. Now let's create a python function that takes two arguments, N and x, and returns .
def eN(N, x):
"""Compute e_N(x) = (1+x/N)**N."""

val = # FILL HERE
return val
c. Use your function to evaluate for the 6 values of part a.
# FILL HERE
d. Many of you will know that
If you don't, you should remember it -- it's easy to derive:
where we used the leading Taylor expansion of for small in the
exponent.
Let's check this numerically.
Make an array of the values of for .
In [ ]:
In [ ]:
In [ ]:
In [ ]:
# create an array of N values from 1 to 100
Ns = r_[1:101]
# for loop method
# create an empty (zero) matrix of the right shape
eNs = zeros((len(Ns),), dtype=float)
# iterate over the N values using a for loop
for (i,N) in enumerate(Ns):
eNs[i] = # FILL HERE
Display the array values.
# FILL HERE
Plot the values versus . Make sure to label the axes on your plot and set the
marker for the points to be 'x' with no line drawn between the points.
# FILL HERE
Exercise 2: Discretizing Real Derivatives
In this exercise, we will learn how to numerically represent, manipulate and plot functions of 1
variable in numpy.
Suppose that somebody has heated a 2m bar which reaches from to using a blow torch
such that the temperature in Kelvin,
has a Gaussian profile.
The first representation of such a function that we may want to use it is programmatic:
a. Define a pythonic function called temperature which accepts the position x as an argument and
computes the temperature at that point. Make sure to include a document string explaining what
it does in english.
# FILL HERE
b. Evaluate the temperature at the center and the ends of the bar.
eN(1)
eN(1) N
−1m 1m
T(x) = 300 + 100e
− x 2
2(0.5)2
https://physics.bu.edu/jupyter-py355/user/wendiyu/nbconve…/HW1%20Real%20Functions%202020.ipynb?download=false 2020/1/23 下午10:02
⻚码:2/4
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
# FILL HERE
c. In order to plot this temperature profile, we discretize our representation of it. That is, we will
create two one-dimensional arrays, the first of which represents the positions at which we
sample the temperature and the second the values of the temperature at those
positions.
Create an array called x with a regularly spaced grid of 100 points between -1 and 1 (inclusive).
(Hint: the functions arange, linspace and/or r_ might help.)
# FILL HERE
In order to evaluate the temperature at all the positions in x, we can exploit numpy's element-wise
mathematics. Simply pass x to your temperature function to get an array of temperature values at
each of the points x. Assign this to T.
# FILL HERE
d. Now let's take a look at what we've done. Use the plot function to plot the temperature along
the bar. Make sure to label your axes including the units of and .
# FILL HERE
e. Repeat the above but reduce the number of points in the grid to 5. One of the essential parts of
numerical computing is ensuring that discretization error is small enough for whatever purposes
is at hand. Is it small enough with a grid of 5 points for correct visualization?
# FILL HERE
f. Now let's consider the derivative of . Define a function diffTempAnalytic(x) which returns the
derivative by working out the functional form of analytically.
# FILL HERE
xi
T = T( ) i xi
x T
T
T (x) ′ T (x) ′
https://physics.bu.edu/jupyter-py355/user/wendiyu/nbconve…/HW1%20Real%20Functions%202020.ipynb?download=false 2020/1/23 下午10:02
⻚码:3/4
In [ ]:
In [ ]:
g. Let's compute a numerical representation of the derivative. Suppose we only have the values of
the function on a regularly spaced mesh of points , with running from 0 to the number of
points in the grid minus 1. There are many approaches to estimating the derivative. The simplest
is to approximate the derivative by the finite difference quotient
If is sufficently small, then this should be a good approximation to the value of
at any point in the range between and . If we associate the approximate derivative to the
point this is called a 'forward difference'; if we associate it to it is called a 'backward
difference'.
Use the forward difference method to estimate the derivative on a grid of 100 points and assign it
to the variable diffT. (Hint: you may find the diff function useful.)
# FILL HERE
h Now make a plot with both the gradient computed using the function defined in part f and the
numerically estimated gradient from part g. Do they agree? Don't forget to label your axes
including units.
# FILL HERE
i. What are the sources of error in the previous comparison? How could you improve this?
FILL HERE

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codehelp
热点文章
程序代写更多图片

联系我们 - QQ: 99515681 微信:codinghelp
© 2014 www.7daixie.com
程序代写网!