实验五:py求函数驻点极大小值凹凸区间拐点

本文通过Python的sympy库演示了如何求解多项式函数的驻点、极值以及凹凸区间。以两个具体例子展示了利用一阶导数找到驻点,再通过二阶导数判断极值类型,以及识别凹凸区间和拐点的过程。最后,通过matplotlib绘制函数图像以直观验证计算结果。
摘要由CSDN通过智能技术生成

本博文源于高等数学基础,旨在用python对函数进行驻点和极大值与极小值求取,最后采用画图进行展示。最后一个例子就是求凹凸区间与拐点。

例题1:求函数 y = 2 x 3 − 6 x 2 − 18 x + 7 y=2x^3-6x^2-18x+7 y=2x36x218x+7的极值,并作图对照

1.1思路解析

先观察其定义域,然后发现定义域属于R,然后进行求导,驻点就是导数为0点,然后根据二阶导数判断它是极大值还是极小值就行了。

1.2 实验效果

在这里插入图片描述

在这里插入图片描述

1.3 实验代码

from sympy import *
x = symbols('x')
y = 2*x**3-6*x**2-18*x+7
ds_1 = diff(y,x)
ans = solve(ds_1,x)
print('函数的导数为',ds_1)
print('驻点为',ans)
ds_2 = diff(y,x,2)
ans_1 = ds_2.evalf(subs={x:-1})
ans_2 = ds_2.evalf(subs={x:3})
print('二阶导数在x=-1的值为',ans_1)
print('二阶导数在x=3的值为',ans_2)


# 求极大值与极小值
ans_3 = y.evalf(subs={x:-1})
ans_4 = y.evalf(subs={x:3})
print('函数的极大值为',ans_3)
print('函数的极小值为',ans_4)

# 对照图像
import matplotlib.pyplot as plt
from numpy import *
x = arange(-4,4,0.01)
y = 2*x**3-6*x**2-18*x+7
plt.figure()
plt.plot(x,y)
plt.grid(True)
plt.show()

例题2:求 y = x 4 − 2 x 3 + 1 y=x^4-2x^3+1 y=x42x3+1的凹凸区间与拐点

2.1 思路解析

判断凹凸区间的时候,一定要加上二阶导数,二阶导数diff函数,就行了,拐点是二阶导数为0的点。

2.2 实验效果

在这里插入图片描述

在这里插入图片描述

2.3 实验代码

from sympy import *
x = symbols('x')
y = x**4-2*x**3+1
ds_1 = diff(y,x)
ds_2 = diff(y,x,2)
ans = solve(ds_2,x)
print('函数的导数为',ds_1)
print('函数的二阶导数为',ds_2)
print('二阶导数为0的点是',ans)

# 确定二阶导数在两个根左右附近的符号
ans_1 = ds_2.evalf(subs={x:-1})
ans_2 = ds_2.evalf(subs={x:1/2})
ans_3 = ds_2.evalf(subs={x:2})
print('二阶导数在x=-1的值为',ans_1)
print('二阶导数在x=1/2的值为',ans_2)
print('二阶导数在x=2的值为',ans_3)

# 结合图像确定凹凸区间
import matplotlib.pyplot as plt
from numpy import *
x = arange(-1,2,0.01)
y = x**4-2*x**3+1
plt.figure()
plt.plot(x,y)
plt.grid(True)
plt.show()


总结

通过本次学习,简单运用python对函数进行极值,与凹凸区间与拐点进行求值。结构清晰。

这是一段使用遗传算法求解函数最大值的 Python 代码,代码中使用了 Python 标准库中的 random 模块。 ``` import random # 目标函数 def target_function(x): return x**2 - 3*x + 4 # 适应度函数 def fitness_function(x): return target_function(x) # 个体编码 def encode_individual(): return random.uniform(-10, 10) # 初始化种群 def init_population(population_size): population = [] for i in range(population_size): population.append(encode_individual()) return population # 选择操作 def selection(population): sorted_population = sorted(population, key=lambda x: fitness_function(x), reverse=True) selected_population = sorted_population[:len(population)//2] return selected_population # 交叉操作 def crossover(parent1, parent2): child1 = (parent1 + parent2) / 2 child2 = (parent1 - parent2) / 2 return child1, child2 # 变异操作 def mutation(child): mutated_child = child + random.uniform(-0.5, 0.5) return mutated_child # 遗传算法主函数 def genetic_algorithm(population_size, max_generations): population = init_population(population_size) for i in range(max_generations): selected_population = selection(population) new_population = [] while len(new_population) < population_size: parent1 = random.choice(selected_population) parent2 = random.choice(selected_population) child1, child2 = crossover(parent1, parent2) mutated_child1 = mutation(child1) mutated_child2 = mutation(child2) new_population.append(mutated_child1) new_population.append(mutated_child2) population = new_population best_individual = max(population, key=lambda x: fitness_function(x)) return best_individual # 测试遗传算法 if __name__ == '__main__': best_individual = genetic_algorithm(population_size=100, max_generations=1000) best_fitness = fitness_function(best_individual) print('最优个体:', best_individual) print('最优适应度:', best_fitness) ``` 在这个实验中,我们使用了简单的遗传算法来求解目标函数 $f(x) = x^2 - 3x + 4$ 的最大值。首先,我们定义了目标函数和适应度函数,其中目标函数就是要求解的函数,适应度函数就是根据目标函数计算个体适应度的函数。然后,我们定义了个体编码函数,这里采用了实数编码,即每个个体是一个实数。接着,我们初始化了种群,并定义了选择、交叉和变异等遗传算法的操作。最后,我们使用遗传算法进行求解,并输出了最优个体和最优适应度。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值