# Assignment 4

AI悦创原创大约 9 分钟...约 2806 字

Questions

This assignment is worth 10 points; the number of points a question is worth is in the square brackets next to the problem number

## Question 1

1. In this question we explore the differences between lists and numpy arrays.First, you will create an $m \times n$ matrix M filled with numbers 1,2,3,...,mn in order, by rows. For example,if $m=2$ and $n=3$ ,then

$M = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \\$

if $m = 1$ and $n = 7$, then

$M = [1,2,3,4,5,6,7]$

and if $m = 3$ and $n = 5$

$\begin{bmatrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \end{bmatrix}$

Parts (a)-(c): Use commands for lists only (no numpy arrays)

(a)[1] Write a function create matrix(m,n) to create the $m \times n$ matrix $M$ as described above.

(b)[1] Write a function sum_columns(M) to create a list whose entry under index j is the sum of all elements in the column j of $M$.

(c)[1] Write a function sum_rows(M) to create a list whose entry under index i is the sum of all elements in the row i of $M$.

Parts (d)-(f): Use commands for numpy arrays only, such as np.arange, np.reshape,.sum, etc., as well as slicing. Do not use commands for lists, such as .append.

(d)[1] Write a function np create matrix(m,n) to create the $m \times n$ numpy array $M$ as de-scribed above.

(e)[1] Write a function np_sum_columns(M) to create a numpy vector (i.e., a one-dimensional array) whose entry under index $j$ is the sum of all elements in the column $j$ of $M$.

(f)[1] Write a function np_sum_rows(M) to create a a numpy vector (i.e., a one-dimensional array) whose entry under index $i$ is the sum of all elements in the row $i$ of $M$.

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

#1(a)

def create_matrix(m,n):

print(create_matrix(2,3))  # should return [[1, 2, 3], [4, 5, 6]]
print(create_matrix(1,7))  # should return [[1, 2, 3, 4, 5, 6, 7]]
print(create_matrix(3,5))  # should return [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
print(create_matrix(5,2))  # should return [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
print(create_matrix(7,1))  # should return [[1], [2], [3], [4], [5], [6], [7]]

#1(b)

def sum_columns(M):

M=create_matrix(2,3)
print(sum_columns(M))  # should print [5, 7, 9]
print(sum_columns(create_matrix(1,7)))  # should print [1, 2, 3, 4, 5, 6, 7]
print(sum_columns(create_matrix(3,5)))  # should print [18, 21, 24, 27, 30]

#1(c)

def sum_rows(M):

print(sum_rows(create_matrix(2,3))) # should print [6, 15]
print(sum_rows(create_matrix(1,7)))  # should print [28]
print(sum_rows(create_matrix(3,5)))  # should print [15, 40, 65]

#1(d)

def np_create_matrix(m,n):

print(np_create_matrix(2,3),'\n')  # compare all with 1(a)
print(np_create_matrix(1,7),'\n')
print(np_create_matrix(3,5),'\n')
print(np_create_matrix(5,2),'\n')
print(np_create_matrix(7,1))

#1(e)

def np_sum_columns(M):

M=np_create_matrix(2,3)
print(np.sum(M,axis=0),'\n')

M=np_create_matrix(1,7)
print(np.sum(M,axis=0),'\n')

M=np_create_matrix(3,5)
print(np.sum(M,axis=0))

#1(f)

def np_sum_rows(M):

M=np_create_matrix(2,3)
print(np.sum(M,axis=1),'\n')

M=np_create_matrix(1,7)
print(np.sum(M,axis=1),'\n')

M=np_create_matrix(3,5)
print(np.sum(M,axis=1))


## Solution 1

1. (a) 创建矩阵 create_matrix(m, n)
def create_matrix(m, n):
return [[i * n + j + 1 for j in range(n)] for i in range(m)]

1. (b) 计算每列之和 sum_columns(M)
def sum_columns(M):
return [sum(M[i][j] for i in range(len(M))) for j in range(len(M[0]))]

1. (c) 计算每行之和 sum_rows(M)
def sum_rows(M):
return [sum(row) for row in M]

1. (d) 使用 numpy 创建矩阵 np_create_matrix(m, n)
def np_create_matrix(m, n):
return np.arange(1, m * n + 1).reshape(m, n)

1. (e) 使用 numpy 计算每列之和 np_sum_columns(M)
def np_sum_columns(M):
return np.sum(M, axis=0)

1. (f) 使用 numpy 计算每行之和 np_sum_rows(M)
def np_sum_rows(M):
return np.sum(M, axis=1)


## Question 2

1. In this question we explore numeric approximations of the integral of the function

$f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{1}{2} \left(\frac{x-\mu}{(\sigma)^2}\right)^2}$

over the interval $[a, b] = [1, 3]$. In your calculations, use $\mu = 1$ and $\sigma = 2$.

Complete the code in the template to define f(x) and see its graph.

(a)[1] Write a function left_sum(f,a,b,n) to approximate the integral of f(x) over [a, b] using the left Riemann sum (i.e., using the left endpoint approximation) with n rectangles. To approximate the given integral, use $n = 100$.

(b)[1] Write a function midpoint_sum(f,a,b,n) to approximate the integral of f(x) over $[a, b]$ using the Midpoint sum (Midpoint rule) with n rectangles. To approximate the given integral, use $n = 100$.

(c)[1] Write a function trapezoidal_rule(f,a,b,n) to approximate the integral of f(x) over $[a, b]$ using the Trapezoidal rule. To approximate the given integral, use $n = 100$.

(d)[1] Write a function simpson_rule(f,a,b,n) to approximate the integral of f(x) over $[a,b]$ using the Simpson’s rule. To approximate the given integral, use $n = 100$.

def f(x):
return       # enter the function here then run the cell

a=-6
b=8
n=500
x=np.linspace(a,b,n+1)

plt.title("Graph of f(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x,f(x),color='blue')
plt.grid()
plt.show()

#2(a) left sum

def left_sum(f,a,b,n):

print(left_sum(f,1,3,100))  # should be 0.34212758741852883
# possibly different in the last few decimals

#2(b) midpoint sum

def midpoint_sum(f,a,b,n):

print(midpoint_sum(f,1,3,100))  # should be 0.34134272963911727
# possibly different in the last few decimals

#2(c) trapezoidal rule

def trapezoidal_rule(f,a,b,n):

print(trapezoidal_rule(f,1,3,100))  # should be 0.3413427296391172
# possibly different in the last few decimals

#2(d) Simpson's rule

def simpson_rule(f,a,b,n):

print(simpson_rule(f,1,3,100))  # should be 0.34134474609542953
# possibly different in the last few decimals


## Solution 2

import numpy as np
import matplotlib.pyplot as plt

# 定义函数 f(x)
def f(x):
mu = 1
sigma = 2
return (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mu) / sigma) ** 2)

a = 1
b = 3
n = 500
x = np.linspace(a, b, n+1)

# 绘制 f(x) 的图像
plt.title("Graph of f(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, f(x), color='blue')
plt.grid()
plt.show()

# 2(a) 左矩形法
def left_sum(f, a, b, n):
h = (b - a) / n
total = sum(f(a + i * h) for i in range(n))
return h * total

print(left_sum(f, 1, 3, 100))

# 2(b) 中点法
def midpoint_sum(f, a, b, n):
h = (b - a) / n
total = sum(f(a + (i + 0.5) * h) for i in range(n))
return h * total

print(midpoint_sum(f, 1, 3, 100))

# 2(c) 梯形法
def trapezoidal_rule(f, a, b, n):
h = (b - a) / n
total = 0.5 * (f(a) + f(b)) + sum(f(a + i * h) for i in range(1, n))
return h * total

print(trapezoidal_rule(f, 1, 3, 100))

# 2(d) 辛普森法
def simpson_rule(f, a, b, n):
h = (b - a) / n
total = f(a) + f(b) + 2 * sum(f(a + i * h) for i in range(2, n, 2))
total += 4 * sum(f(a + i * h) for i in range(1, n, 2))
return h / 3 * total

print(simpson_rule(f, 1, 3, 100))



AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度