跳至主要內容

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×nm \times n matrix M filled with numbers 1,2,3,...,mn in order, by rows. For example,if m=2m=2 and n=3n=3 ,then

M=[123456] M = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \\

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

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

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

[123456789101112131415] \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×nm \times n matrix MM 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 MM.

(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 MM.

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×nm \times n numpy array MM 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 jj is the sum of all elements in the column jj of MM.

(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 ii is the sum of all elements in the row ii of MM.

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_columns(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=0),'\n') 

M=np_create_matrix(1,7) 
print(np_sum_columns(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=0),'\n') 

M=np_create_matrix(3,5) 
print(np_sum_columns(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=0)) 



#1(f)

def np_sum_rows(M):  

    
    
    
M=np_create_matrix(2,3)
print(np_sum_rows(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=1),'\n') 

M=np_create_matrix(1,7)
print(np_sum_rows(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=1),'\n') 

M=np_create_matrix(3,5)
print(np_sum_rows(M))  # to check your answer, compare with the next line
print(np.sum(M,axis=1)) 

Solution 1

Answer 1

这个题目要求我们使用Python完成几个与矩阵操作相关的函数。我将按照你给出的框架,逐步填充每个函数的实现。

  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)

以上是每个函数的具体实现。这些函数分别实现了使用普通列表和 numpy 数组的方式来创建矩阵,并计算它们的行和列之和。在运行这些函数之前,请确保已经导入了numpy 库。

Question 2

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

f(x)=1σ2πe12(xμ(σ)2)2 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][a, b] = [1, 3]. In your calculations, use μ=1\mu = 1 and σ=2\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=100n = 100.

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

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

(d)[1] Write a function simpson_rule(f,a,b,n) to approximate the integral of f(x) over [a,b][a,b] using the Simpson’s rule. To approximate the given integral, use n=100n = 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

Answer1
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悦创·编程一对一

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

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度