【发布时间】:2019-08-19 11:33:09
【问题描述】:
我需要找到与给出的曲线的 x 轴的上下交点
y=f(x)=10⋅exp(sin(x))−(x^2)/2
为了求曲线的弧长,在Python中
我已经尝试了两种方法,我根本无法使用的割线法。以及找到一个交点的牛顿法。
from math import exp
from math import sin
from math import cos
def func( x ):
return 10*exp(sin(x))-(x**2)/2
def derivFunc( x ):
return 10*exp(sin(x))*cos(x)-x
def newtonRaphson( x ):
h = func(x) / derivFunc(x)
while abs(h) >= 0.0001:
h = func(x)/derivFunc(x)
x = x - h
print("The value of the root is : ",
"%.4f"% x)
x0 = -20
newtonRaphson(x0)
这给了
The value of the root is : -5.7546
然后是第二种方法
import math
from math import exp
from math import sin
def f(x):
f = 10*exp(sin(x))-(x**2)/2
return f;
def secant(x1, x2, E):
n = 0; xm = 0; x0 = 0; c = 0;
if (f(x1) * f(x2) < 0):
while True:
x0 = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n += 1;
if (c == 0):
xm = ((x1 * f(x2) - x2 * f(x1)) /(f(x2) - f(x1)));
if(abs(xm - x0) < E):
print("Root of the given equation =",round(x0, 6));
print("No. of iterations = ", n);
print("Can not find a root in ","the given inteval");
x1 = 0; x2 = 1;
E = 0.0001;
secant(x1, x2, E);
只有结果
NameError: name 'x2' is not defined
然而,每当我尝试定义字符时,它都不会运行
我希望能够得到与 x 轴的上下交点,这样我就可以找到弧长。有没有办法让它也绘制图表
【问题讨论】:
-
为什么不尝试从不同的初始值调用牛顿方法?请修复割线法的压痕,这是您观察到的错误吗?如果
c!=0,xm是什么?
标签: python python-3.x numerical-methods