简体   繁体   English

我的函数不使用浮点数:-“ TypeError('只有size-1数组可以转换为Python标量')”(我认为它可以快速解决!)

[英]My function does not take floats: - “TypeError('only size-1 arrays can be converted to Python scalars')” (I think it has a quick fix!)

I wrote a code to use Riemann to calculate the area under certain function. 我编写了一个代码,使用黎曼来计算某些功能下的面积。 It works well but when I set the boundaries to decimal number I get an error. 它工作正常,但是当我将边界设置为十进制数时,会出现错误。 I think I should tell the function I want to be able to enter floats but I don't see how. 我想我应该告诉该函数我希望能够输入浮点数,但我不知道如何。 I think it is an easy fix, hope someone can help me: 我认为这很容易解决,希望有人可以帮助我:

import numpy as np
import math

def func(x):
    return x ** 2

def func1(x):
    return x ** (x +0.5)

def func2(x):
    return np.sin(x)

def func3(x):
    return np.sin(x ** 2)

def riemann(f,a,b,N):
    dx = (b-a)/N
    x_midpoint = np.linspace(dx/2,b - dx/2,N)
    riemann_sum = np.sum(f(x_midpoint) * dx)
    return riemann_sum

res = riemann(func,0,1,10000)
res1 = riemann(func1,0,1,10000)
res2 = riemann(func2,0,1,10000)
res3 = riemann(func3,0,1,10000)


print("Midpoint Riemann Sum:",res)
print("Midpoint Riemann Sum:",res1)
print("Midpoint Riemann Sum:",res2)
print("Midpoint Riemann Sum:",res3)

Got the followin output and no error found: 得到了Followin输出,没有发现错误:

Midpoint Riemann Sum: 0.3333333325
Midpoint Riemann Sum: 0.527737838955
Midpoint Riemann Sum: 0.459697694323
Midpoint Riemann Sum: 0.310268301273

What can we do to reproduce the error ? 我们该怎么做才能重现错误?

EDIT: 编辑:

using a and b as decimals: 使用a和b作为小数:

res = riemann(func,0.2,3.14,10000)
res1 = riemann(func1,0.2,3.14,10000)
res2 = riemann(func2,0.2,3.14,10000)
res3 = riemann(func3,0.2,3.14,10000)

OUTPUT: 输出:

Midpoint Riemann Sum: 9.66243875102
Midpoint Riemann Sum: 27.9239376537
Midpoint Riemann Sum: 1.87259837142
Midpoint Riemann Sum: 0.724064617658

and still got no error :/ 仍然没有错误:/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:只有大小为 1 的数组可以转换为 Python 标量 (matplotlib) - TypeError: only size-1 arrays can be converted to Python scalars (matplotlib) 类型错误:只有大小为 1 的数组可以转换为 Python 标量 - TypeError: only size-1 arrays can be converted to Python scalars TypeError:只有大小为1的数组可以转换为Python标量,此错误 - TypeError: only size-1 arrays can be converted to Python scalars this error TypeError: 只有 size-1 arrays 可以转换为 Python 标量 Numpy - TypeError: only size-1 arrays can be converted to Python scalars Numpy 类型错误:只有大小为 1 的数组可以转换为 Python 标量 + 解决方案 - TypeError: only size-1 arrays can be converted to Python scalars + Solution TypeError: only size-1 arrays 可以转换为 Python 标量, - TypeError: only size-1 arrays can be converted to Python scalars, 如何修复“TypeError:只有 size-1 arrays 可以转换为 Python 标量” - How to fix "TypeError : only size-1 arrays can be converted to Python scalars" 发生异常:类型错误:只有大小为 1 的数组可以转换为 Python 标量 - Exception has occurred: TypeError:only size-1 arrays can be converted to Python scalars 我的代码抛出一个类型错误:只有大小为 1 的数组可以转换为 Python 标量 - My code is throwing an TypeError: only size-1 arrays can be converted to Python scalars 尝试集成 function 此错误显示:TypeError: only size-1 arrays can be converted to Python scalars - Trying to integrate a function this error shows: TypeError: only size-1 arrays can be converted to Python scalars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM