这是我正在做的here任务。
我被要求使用Python和Sympy包对其进行编码。我能够得到A部分的正确输出。
import numpy as np
from numpy import linspace, math, arange, linspace
from sympy import *
import sympy as sp
import math
x = Symbol('x')
f = (x**(4/5)) * ((x-4)**2)
fd = diff(f) #first derivative
fdd = diff(fd) #second derivative (for later)
print("f' =", fd)
print("Critical Vales are:")
dRoots = solve(Eq(fd, 0)) #finds critical values
print(dRoots)
我不知道如何编写B部分和C部分,它要求得到局部最大值和最小值,以及区间x ∈ [−1, 6]
上的f
的曲线图。到目前为止,我得到了一个"'>‘在'list’和‘int’的实例之间不受支持“错误
fdd_val = solve(Eq(fdd, 0))
if fdd_val > 0:
print("Local Maxima = ",fdd_val)
if fdd_val < 0:
print("Local Minima = ",fdd_val)
发布于 2020-10-28 20:45:58
一般来说,我们尽量避免在S.O.上发布家庭作业问题,但既然您确实尝试做了这个问题,我将为您的代码提供一些提示和更正。
from sympy import *
# Optimize imports -> if you don't need it, don't include it
x = Symbol('x')
f = (x**(4/5)) * ((x-4)**2)
fd = diff(f) #first derivative
# pick more descriptive variable names: you'll thank yourself later. Like 'second_derivative' or even 'sd'
fdd = diff(fd) #second derivative (for later)
print("f' =", fd)
print("Critical Vales are:")
# call solve on fd like the documentation suggests.
dRoots = solve(fd) #finds critical values, returns a list
print(dRoots, '\n')
for root in dRoots:
# Use function.subs(variable, value) to evaluate d2/dx^2 [f(x)] at each critical point
fdd_val = fdd.subs(x, root)
print(f"At root {root:.5g}, second derivative at this point is {fdd_val:.5g} which is "
f"{'less' if fdd_val < 0 else 'greater'} than zero, so this is a local _____")
这段修改后的代码应该指出根据documentation计算表达式的正确方法,并让您对如何继续进行有一些了解。你应该可以从这里完成。干杯
https://stackoverflow.com/questions/64580370
复制