Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//g++ standard_deviation.cpp -I/usr/include/python3.8 -lpython3.8
#include <iostream>
#include <tuple>
#include <vector>
#include <math.h>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
//---------------------------------------------------------------------------------------
void plotData(std::tuple<std::vector<double>, std::vector<double>> data)
{
std::vector<double> xx = std::get<0>(data);
std::vector<double> valueND = std::get<1>(data);
plt::figure_size(1200, 780);
plt::title("Normal Distribution, 66-95-99.7 rule ");
plt::plot(xx, valueND);
plt::xlabel("standard deviation");
plt::ylabel("density");
plt::show();
}
//---------------------------------------------------------------------------------------
std::tuple<std::vector<double>, std::vector<double>> computedNormalDistribution (double variance, double mean){
std::vector<double> xx;
std::vector<double> valuesND;
double vNDi;
double dx = 0.1;
double startX = -4.0 * variance * variance ;
double stopX = 4.0 * variance * variance;
for (double xi = startX; xi <= stopX+dx; xi+=dx){
//std::cout<< xi << std::endl;
vNDi = (1/(std::sqrt(variance) * std::sqrt(2*M_PI)))*std::exp(-0.5 * std::pow((xi - mean)/std::sqrt(variance),2));
xx.push_back(xi);
valuesND.push_back(vNDi);
}
return std::make_tuple(xx,valuesND);
}
int main(){
std::tuple<std::vector<double>, std::vector<double>> NormalDistribution = computedNormalDistribution(1.0, 0.0);
plotData(NormalDistribution);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment