Kotlin-如何使用GraphView以图形方式绘制数学函数? [英] Kotlin - How can a math function be graphically plotted using GraphView?

查看:294
本文介绍了Kotlin-如何使用GraphView以图形方式绘制数学函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能,它是正态分布的公式,我的目标是使用GraphView库对其进行图形化. 好吧,我有以下内容:

I have the following function, well it is the formula of the normal distribution and my goal is to graph it using the GraphView library. Well, I have the following:

  • Id-情节

科特林码

class ExampleActivity : AppCompatActivity() {
    lateinit var series1: LineGraphSeries<DataPoint>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)

        series1 = LineGraphSeries<DataPoint>()

        val media = 8.0  
        val sd = 2.0  

        fun linspace(start: Double, stop: Double, num: Int) = Array(num) { start + it * ((stop - start) / (num - 1)) }

        val x1 = linspace(media - 3*sd, media + 3*sd, 10)
        val normal = NormalDistribution(media,sd)

        for (i in x1){
            val y = normal.cumulativeProbability(i)
            series1.appendData(DataPoint(i,y),false,x1.size)
        }
        plot.viewport.isXAxisBoundsManual = true
        plot.viewport.setMaxX(20.0)
        plot.viewport.isYAxisBoundsManual = true
        plot.viewport.setMaxY(2.0)
        //plot.viewport.isScalable = true
        plot.addSeries(series1)
    }
}

结果

这很奇怪,在python中,我使用了相同的方法,结果更加完整,向您展示.

this is very strange, in python I used the same and the result is more complete, I show you.

代码python

code python

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import math

mu = 8
sigma = 2
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 10)
print(np.linspace(mu - 3*sigma, mu + 3*sigma, 10))
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.show()

结果python

信息

科特林码 val x1 = linspace(media - 3*sd, media + 3*sd, 10)

==

编码Python x = np.linspace(mu - 3*sigma, mu + 3*sigma, 10)

您能帮我吗,我需要补充什么?

Can you help me please, what do I need to add?

推荐答案

似乎python示例正在绘制.pdf概率密度函数",而kotlin示例正在绘制.cumulativeProbability,该本质上是PDF.您很可能希望使用

It looks like the python example is plotting the .pdf "probability density function" while the kotlin example is plotting the .cumulativeProbability which is essentially the integral of the PDF. Most likely you'll want to use the .density method instead.

这篇关于Kotlin-如何使用GraphView以图形方式绘制数学函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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