如何处理Python函数的混合输入,即:5x? [英] How do I handle mixed input to a Python function, ie: 5x?

查看:110
本文介绍了如何处理Python函数的混合输入,即:5x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过二分法找到函数的根。我要求耐心(又一次),因为我暂时没有使用这种方法。我在维基百科上找到了公式。


Pseudocode如下:


start loop

Do While(xR - xL)> epsilon


''计算域的中点

xM =(xR + xL)/ 2


''找到f(xM)

如果((f(xL)* f(xM))> 0那么

''扔掉左半边

xL = xM

其他

''扔掉右半边

xR = xM

结束如果

循环


我正在努力解决这个问题,我对Python如何处理像5x ^ 8 - 3x ^ 4这样的等式有一些疑问+ x - 2?如果我从shell中尝试它,我会收到来自x的错误消息,因为它与5有关,所以我认为它对我要问的内容感到困惑。


一位朋友告诉我使用eval(功能),但我不知道如何在我的代码中使用它,或者它是否是最好的方式。

提醒你,这是一个起点。在完成这项任务之前还会有更多问题。


功能看起来像这样:


def root_find(f,xLo,xHi,eps)


所以,如果我测试上面所以f = 5x ** 8 - 3x ** 4 + x - 2.通过说y0 = f(xLo)我会得到一个错误,对吗?如何格式化输入以便函数使用它?我甚至问过正确的问题吗? (我好丢失了!)


谢谢你帮助

解决方案


我正在通过二分法找到函数的根。我要求耐心(又一次),因为我暂时没有使用这种方法。我在维基百科上找到了公式。


Pseudocode如下:


start loop

Do While(xR - xL)> epsilon


''计算域的中点

xM =(xR + xL)/ 2


''找到f(xM)

如果((f(xL)* f(xM))> 0那么

''扔掉左半边

xL = xM

其他

''扔掉右半边

xR = xM

结束如果

循环


我正在努力解决这个问题,我对Python如何处理像5x ^ 8 - 3x ^ 4这样的等式有一些疑问+ x - 2?如果我从shell中尝试它,我会收到来自x的错误消息,因为它与5有关,所以我认为它对我要问的内容感到困惑。


一位朋友告诉我使用eval(功能),但我不知道如何在我的代码中使用它,或者它是否是最好的方式。

提醒你,这是一个起点。在完成这项任务之前还会有更多问题。


功能看起来像这样:


def root_find(f,xLo,xHi,eps)


所以,如果我测试上面所以f = 5x ** 8 - 3x ** 4 + x - 2.通过说y0 = f(xLo)我会得到一个错误,对吗?如何格式化输入以便函数使用它?我甚至问过正确的问题吗? (我好迷失了!)



谢谢你帮助



我总是从假设x == 1以便在开始时简化事情。这使得5x == 5等等。然后,在函数工作之后,您可以将x添加到参数列表中并执行类似(5 * x)** 8的操作。当然,如果你试图解决x,那么事情要复杂得多,但是如果你能在纸上解决这个问题,你应该能够推算出适当的算法。

你是否使用eval ()或不(eval()只是让interpeter文本工作的另一种方式)你基本上有两个东西进入公式:变量有名称和值(即a = 1)和文字(即 hello)所有必须在评估之前分配。


谢谢......一如既往,你是一个救生员!并且你让它看起来如此简单(更别提它了)。


TMS



谢谢你...一如既往,你是一个救生员!而你让它看起来如此简单(更别提它了)。


TMS



任何时候......真的!


I''m working through the bisection method to find the root of a function. I''m asking patience (yet again) because I haven''t used this method for a while. I found the formula on wikipedia.

Pseudocode is as follows:

start loop
Do While (xR - xL) > epsilon

''calculate midpoint of domain
xM = (xR + xL) / 2

''Find f(xM)
If ((f(xL) * f(xM)) > 0 Then
''throw away left half
xL = xM
else
''throw away right half
xR = xM
End If
Loop

I''m trying to make my way through this and I have some questions about how Python handles an equation like 5x^8 - 3x^4 + x - 2? If I try it from the shell I get an error message from the x since its with the 5, so I assume it confused about what I''m asking.

A friend told me to use eval(function) but I''m not sure how to use it in my code, or if it is even the best way to go.

Mind you, this is a starting point. There will be MANY more questions before I''m done with this assignment.

The function is to look like this:

def root_find(f, xLo, xHi, eps)

So, if I test the above so that f = 5x**8 - 3x**4 + x - 2. by saying y0 = f(xLo) I will get an error, right? How do I format the input so the function will use it? Am I even asking the right question? (I''m so lost!)


thank you for helping

解决方案

I''m working through the bisection method to find the root of a function. I''m asking patience (yet again) because I haven''t used this method for a while. I found the formula on wikipedia.

Pseudocode is as follows:

start loop
Do While (xR - xL) > epsilon

''calculate midpoint of domain
xM = (xR + xL) / 2

''Find f(xM)
If ((f(xL) * f(xM)) > 0 Then
''throw away left half
xL = xM
else
''throw away right half
xR = xM
End If
Loop

I''m trying to make my way through this and I have some questions about how Python handles an equation like 5x^8 - 3x^4 + x - 2? If I try it from the shell I get an error message from the x since its with the 5, so I assume it confused about what I''m asking.

A friend told me to use eval(function) but I''m not sure how to use it in my code, or if it is even the best way to go.

Mind you, this is a starting point. There will be MANY more questions before I''m done with this assignment.

The function is to look like this:

def root_find(f, xLo, xHi, eps)

So, if I test the above so that f = 5x**8 - 3x**4 + x - 2. by saying y0 = f(xLo) I will get an error, right? How do I format the input so the function will use it? Am I even asking the right question? (I''m so lost!)


thank you for helping

I always start off by assuming the x == 1 in order to simplify things at the outset. This makes 5x == 5, etc. Then, after the function is working, you can add the x to the parameter list and do things like (5 * x)**8. Of course, if you are trying to solve for x then things are a lot more complicated, but if you can work it out on paper you should be able to forulate the appropriate algorithm.
Whether you use eval() or not (eval() is just another way of giving the interpeter text to work on) you basically have two things that go into the formula: variables which have names and values (ie a = 1) and literals (ie "hello") all of which must be assigned before they can be evaluated.


thank you... as always you are a life saver!!! And you make it look so easy (never mind that it is).

TMS


thank you... as always you are a life saver!!! And you make it look so easy (never mind that it is).

TMS

Any time... really!


这篇关于如何处理Python函数的混合输入,即:5x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆