Project Developing Data Products jdemoya

Jose De Moya
octubre 22 2014

Roots of General Second Degree Equation

Business Intelligence, Big Data and Data Science Consultant Bogotà, Colombia

General Equation Second Degree (Quadratic Equation)

The aplication in shinyapps extract the two roots of a general second degree equation

The general function is

y <- a*x2 + b*x + c;

where x represents and unknown and a, b and c represent numbers such that a is not equal 0

That equation in its general form has two roots real or complex

Those soluctions are known since the Babilonians

Solutions

The general solution are

x1 = ((-1*b) + sqrt((b2 - 4*a*c)))/2*a; x2 = ((-1*b) - sqrt((b2 - 4*a*c)))/2*a;

First Generate the data for plotting the curves

These roots can be real complex or a combination of then For example

3*x2 + 2*x + 1 = 0 In this case a = 3, b = 2 and c = 1 and the solutions are

a <- 3;
b <- 2;
c <- 1;
x1 <- ((-1*b) + sqrt((b^2 - 4*a*c)))/2*a;
x2 <- ((-1*b) - sqrt((b^2 - 4*a*c)))/2*a;
x1;
[1] NaN
x2;
[1] NaN

Performing of the program

The application takes three values: Value a, Value b, Value c and applies the formula and gets the two roots

That is the graph of the equation expressed in function of x

y = 3*x2 + 2*x + 1;

library(rCharts);
x <- seq(1,10,by=0.01);
y <- 3*x^2 + 2*x + 1;
ds <- as.data.frame(x);
ds <- cbind(ds,y)
par(mfrow=c(1,1));
r1 <- rPlot(y ~ x,data=ds,type="point",color="blue");
r1$print("chart1") 

<div id = 'chart1' class = 'rChart polycharts'></div>
<script type='text/javascript'>
    var chartParams = {
 "dom": "chart1",
"width":    800,
"height":    400,
"layers": [
 {
 "x": "x",
"y": "y",
"data": {
 "x": [      1,   1.01,   1.02,   1.03,   1.04,   1.05,   1.06,   1.07,   1.08,   1.09,    1.1,   1.11,   1.12,   1.13,   1.14,   1.15,   1.16,   1.17,   1.18,   1.19,    1.2,   1.21,   1.22,   1.23,   1.24,   1.25,   1.26,   1.27,   1.28,   1.29,    1.3,   1.31,   1.32,   1.33,   1.34,   1.35,   1.36,   1.37,   1.38,   1.39,    1.4,   1.41,   1.42,   1.43,   1.44,   1.45,   1.46,   1.47,   1.48,   1.49,    1.5,   1.51,   1.52,   1.53,   1.54,   1.55,   1.56,   1.57,   1.58,   1.59,    1.6,   1.61,   1.62,   1.63,   1.64,   1.65,   1.66,   1.67,   1.68,   1.69,    1.7,   1.71,   1.72,   1.73,   1.74,   1.75,   1.76,   1.77,   1.78,   1.79,    1.8,   1.81,   1.82,   1.83,   1.84,   1.85,   1.86,   1.87,   1.88,   1.89,    1.9,   1.91,   1.92,   1.93,   1.94,   1.95,   1.96,   1.97,   1.98,   1.99,      2,   2.01,   2.02,   2.03,   2.04,   2.05,   2.06,   2.07,   2.08,   2.09,    2.1,   2.11,   2.12,   2.13,   2.14,   2.15,   2.16,   2.17,   2.18,   2.19,    2.2,   2.21,   2.22,   2.23,   2.24,   2.25,   2.26,   2.27,   2.28,   2.29,    2.3,   2.31,   2.32,   2.33,   2.34,   2.35,   2.36,   2.37,   2.38,   2.39,    2.4,   2.41,   2.42,   2.43,   2.44,   2.45,   2.46,   2.47,   2.48,   2.49,    2.5,   2.51,   2.52,   2.53,   2.54,   2.55,   2.56,   2.57,   2.58,   2.59,    2.6,   2.61,   2.62,   2.63,   2.64,   2.65,   2.66,   2.67,   2.68,   2.69,    2.7,   2.71,   2.72,   2.73,   2.74,   2.75,   2.76,   2.77,   2.78,   2.79,    2.8,   2.81,   2.82,   2.83,   2.84,   2.85,   2.86,   2.87,   2.88,   2.89,    2.9,   2.91,   2.92,   2.93,   2.94,   2.95,   2.96,   2.97,   2.98,   2.99,      3,   3.01,   3.02,   3.03,   3.04,   3.05,   3.06,   3.07,   3.08,   3.09,    3.1,   3.11,   3.12,   3.13,   3.14,   3.15,   3.16,   3.17,   3.18,   3.19,    3.2,   3.21,   3.22,   3.23,   3.24,   3.25,   3.26,   3.27,   3.28,   3.29,    3.3,   3.31,   3.32,   3.33,   3.34,   3.35,   3.36,   3.37,   3.38,   3.39,    3.4,   3.41,   3.42,   3.43,   3.44,   3.45,   3.46,   3.47,   3.48,   3.49,    3.5,   3.51,   3.52,   3.53,   3.54,   3.55,   3.56,   3.57,   3.58,   3.59,    3.6,   3.61,   3.62,   3.63,   3.64,   3.65,   3.66,   3.67,   3.68,   3.69,    3.7,   3.71,   3.72,   3.73,   3.74,   3.75,   3.76,   3.77,   3.78,   3.79,    3.8,   3.81,   3.82,   3.83,   3.84,   3.85,   3.86,   3.87,   3.88,   3.89,    3.9,   3.91,   3.92,   3.93,   3.94,   3.95,   3.96,   3.97,   3.98,   3.99,      4,   4.01,   4.02,   4.03,   4.04,   4.05,   4.06,   4.07,   4.08,   4.09,    4.1,   4.11,   4.12,   4.13,   4.14,   4.15,   4.16,   4.17,   4.18,   4.19,    4.2,   4.21,   4.22,   4.23,   4.24,   4.25,   4.26,   4.27,   4.28,   4.29,    4.3,   4.31,   4.32,   4.33,   4.34,   4.35,   4.36,   4.37,   4.38,   4.39,    4.4,   4.41,   4.42,   4.43,   4.44,   4.45,   4.46,   4.47,   4.48,   4.49,    4.5,   4.51,   4.52,   4.53,   4.54,   4.55,   4.56,   4.57,   4.58,   4.59,    4.6,   4.61,   4.62,   4.63,   4.64,   4.65,   4.66,   4.67,   4.68,   4.69,    4.7,   4.71,   4.72,   4.73,   4.74,   4.75,   4.76,   4.77,   4.78,   4.79,    4.8,   4.81,   4.82,   4.83,   4.84,   4.85,   4.86,   4.87,   4.88,   4.89,    4.9,   4.91,   4.92,   4.93,   4.94,   4.95,   4.96,   4.97,   4.98,   4.99,      5,   5.01,   5.02,   5.03,   5.04,   5.05,   5.06,   5.07,   5.08,   5.09,    5.1,   5.11,   5.12,   5.13,   5.14,   5.15,   5.16,   5.17,   5.18,   5.19,    5.2,   5.21,   5.22,   5.23,   5.24,   5.25,   5.26,   5.27,   5.28,   5.29,    5.3,   5.31,   5.32,   5.33,   5.34,   5.35,   5.36,   5.37,   5.38,   5.39,    5.4,   5.41,   5.42,   5.43,   5.44,   5.45,   5.46,   5.47,   5.48,   5.49,    5.5,   5.51,   5.52,   5.53,   5.54,   5.55,   5.56,   5.57,   5.58,   5.59,    5.6,   5.61,   5.62,   5.63,   5.64,   5.65,   5.66,   5.67,   5.68,   5.69,    5.7,   5.71,   5.72,   5.73,   5.74,   5.75,   5.76,   5.77,   5.78,   5.79,    5.8,   5.81,   5.82,   5.83,   5.84,   5.85,   5.86,   5.87,   5.88,   5.89,    5.9,   5.91,   5.92,   5.93,   5.94,   5.95,   5.96,   5.97,   5.98,   5.99,      6,   6.01,   6.02,   6.03,   6.04,   6.05,   6.06,   6.07,   6.08,   6.09,    6.1,   6.11,   6.12,   6.13,   6.14,   6.15,   6.16,   6.17,   6.18,   6.19,    6.2,   6.21,   6.22,   6.23,   6.24,   6.25,   6.26,   6.27,   6.28,   6.29,    6.3,   6.31,   6.32,   6.33,   6.34,   6.35,   6.36,   6.37,   6.38,   6.39,    6.4,   6.41,   6.42,   6.43,   6.44,   6.45,   6.46,   6.47,   6.48,   6.49,    6.5,   6.51,   6.52,   6.53,   6.54,   6.55,   6.56,   6.57,   6.58,   6.59,    6.6,   6.61,   6.62,   6.63,   6.64,   6.65,   6.66,   6.67,   6.68,   6.69,    6.7,   6.71,   6.72,   6.73,   6.74,   6.75,   6.76,   6.77,   6.78,   6.79,    6.8,   6.81,   6.82,   6.83,   6.84,   6.85,   6.86,   6.87,   6.88,   6.89,    6.9,   6.91,   6.92,   6.93,   6.94,   6.95,   6.96,   6.97,   6.98,   6.99,      7,   7.01,   7.02,   7.03,   7.04,   7.05,   7.06,   7.07,   7.08,   7.09,    7.1,   7.11,   7.12,   7.13,   7.14,   7.15,   7.16,   7.17,   7.18,   7.19,    7.2,   7.21,   7.22,   7.23,   7.24,   7.25,   7.26,   7.27,   7.28,   7.29,    7.3,   7.31,   7.32,   7.33,   7.34,   7.35,   7.36,   7.37,   7.38,   7.39,    7.4,   7.41,   7.42,   7.43,   7.44,   7.45,   7.46,   7.47,   7.48,   7.49,    7.5,   7.51,   7.52,   7.53,   7.54,   7.55,   7.56,   7.57,   7.58,   7.59,    7.6,   7.61,   7.62,   7.63,   7.64,   7.65,   7.66,   7.67,   7.68,   7.69,    7.7,   7.71,   7.72,   7.73,   7.74,   7.75,   7.76,   7.77,   7.78,   7.79,    7.8,   7.81,   7.82,   7.83,   7.84,   7.85,   7.86,   7.87,   7.88,   7.89,    7.9,   7.91,   7.92,   7.93,   7.94,   7.95,   7.96,   7.97,   7.98,   7.99,      8,   8.01,   8.02,   8.03,   8.04,   8.05,   8.06,   8.07,   8.08,   8.09,    8.1,   8.11,   8.12,   8.13,   8.14,   8.15,   8.16,   8.17,   8.18,   8.19,    8.2,   8.21,   8.22,   8.23,   8.24,   8.25,   8.26,   8.27,   8.28,   8.29,    8.3,   8.31,   8.32,   8.33,   8.34,   8.35,   8.36,   8.37,   8.38,   8.39,    8.4,   8.41,   8.42,   8.43,   8.44,   8.45,   8.46,   8.47,   8.48,   8.49,    8.5,   8.51,   8.52,   8.53,   8.54,   8.55,   8.56,   8.57,   8.58,   8.59,    8.6,   8.61,   8.62,   8.63,   8.64,   8.65,   8.66,   8.67,   8.68,   8.69,    8.7,   8.71,   8.72,   8.73,   8.74,   8.75,   8.76,   8.77,   8.78,   8.79,    8.8,   8.81,   8.82,   8.83,   8.84,   8.85,   8.86,   8.87,   8.88,   8.89,    8.9,   8.91,   8.92,   8.93,   8.94,   8.95,   8.96,   8.97,   8.98,   8.99,      9,   9.01,   9.02,   9.03,   9.04,   9.05,   9.06,   9.07,   9.08,   9.09,    9.1,   9.11,   9.12,   9.13,   9.14,   9.15,   9.16,   9.17,   9.18,   9.19,    9.2,   9.21,   9.22,   9.23,   9.24,   9.25,   9.26,   9.27,   9.28,   9.29,    9.3,   9.31,   9.32,   9.33,   9.34,   9.35,   9.36,   9.37,   9.38,   9.39,    9.4,   9.41,   9.42,   9.43,   9.44,   9.45,   9.46,   9.47,   9.48,   9.49,    9.5,   9.51,   9.52,   9.53,   9.54,   9.55,   9.56,   9.57,   9.58,   9.59,    9.6,   9.61,   9.62,   9.63,   9.64,   9.65,   9.66,   9.67,   9.68,   9.69,    9.7,   9.71,   9.72,   9.73,   9.74,   9.75,   9.76,   9.77,   9.78,   9.79,    9.8,   9.81,   9.82,   9.83,   9.84,   9.85,   9.86,   9.87,   9.88,   9.89,    9.9,   9.91,   9.92,   9.93,   9.94,   9.95,   9.96,   9.97,   9.98,   9.99,     10 ],
"y": [      6, 6.0803, 6.1612, 6.2427, 6.3248, 6.4075, 6.4908, 6.5747, 6.6592, 6.7443,   6.83, 6.9163, 7.0032, 7.0907, 7.1788, 7.2675, 7.3568, 7.4467, 7.5372, 7.6283,   7.72, 7.8123, 7.9052, 7.9987, 8.0928, 8.1875, 8.2828, 8.3787, 8.4752, 8.5723,   8.67, 8.7683, 8.8672, 8.9667, 9.0668, 9.1675, 9.2688, 9.3707, 9.4732, 9.5763,   9.68, 9.7843, 9.8892, 9.9947, 10.101, 10.207, 10.315, 10.423, 10.531,  10.64,  10.75,  10.86, 10.971, 11.083, 11.195, 11.308, 11.421, 11.535, 11.649, 11.764,  11.88, 11.996, 12.113, 12.231, 12.349, 12.467, 12.587, 12.707, 12.827, 12.948,  13.07, 13.192, 13.315, 13.439, 13.563, 13.688, 13.813, 13.939, 14.065, 14.192,  14.32, 14.448, 14.577, 14.707, 14.837, 14.968, 15.099, 15.231, 15.363, 15.496,  15.63, 15.764, 15.899, 16.035, 16.171, 16.308, 16.445, 16.583, 16.721,  16.86,     17,  17.14, 17.281, 17.423, 17.565, 17.707, 17.851, 17.995, 18.139, 18.284,  18.43, 18.576, 18.723, 18.871, 19.019, 19.168, 19.317, 19.467, 19.617, 19.768,  19.92, 20.072, 20.225, 20.379, 20.533, 20.688, 20.843, 20.999, 21.155, 21.312,  21.47, 21.628, 21.787, 21.947, 22.107, 22.268, 22.429, 22.591, 22.753, 22.916,  23.08, 23.244, 23.409, 23.575, 23.741, 23.908, 24.075, 24.243, 24.411,  24.58,  24.75,  24.92, 25.091, 25.263, 25.435, 25.608, 25.781, 25.955, 26.129, 26.304,  26.48, 26.656, 26.833, 27.011, 27.189, 27.368, 27.547, 27.727, 27.907, 28.088,  28.27, 28.452, 28.635, 28.819, 29.003, 29.188, 29.373, 29.559, 29.745, 29.932,  30.12, 30.308, 30.497, 30.687, 30.877, 31.067, 31.259, 31.451, 31.643, 31.836,  32.03, 32.224, 32.419, 32.615, 32.811, 33.008, 33.205, 33.403, 33.601,   33.8,     34,   34.2, 34.401, 34.603, 34.805, 35.007, 35.211, 35.415, 35.619, 35.824,  36.03, 36.236, 36.443, 36.651, 36.859, 37.067, 37.277, 37.487, 37.697, 37.908,  38.12, 38.332, 38.545, 38.759, 38.973, 39.188, 39.403, 39.619, 39.835, 40.052,  40.27, 40.488, 40.707, 40.927, 41.147, 41.368, 41.589, 41.811, 42.033, 42.256,  42.48, 42.704, 42.929, 43.155, 43.381, 43.608, 43.835, 44.063, 44.291,  44.52,  44.75,  44.98, 45.211, 45.443, 45.675, 45.908, 46.141, 46.375, 46.609, 46.844,  47.08, 47.316, 47.553, 47.791, 48.029, 48.267, 48.507, 48.747, 48.987, 49.228,  49.47, 49.712, 49.955, 50.199, 50.443, 50.688, 50.933, 51.179, 51.425, 51.672,  51.92, 52.168, 52.417, 52.667, 52.917, 53.168, 53.419, 53.671, 53.923, 54.176,  54.43, 54.684, 54.939, 55.195, 55.451, 55.708, 55.965, 56.223, 56.481,  56.74,     57,  57.26, 57.521, 57.783, 58.045, 58.308, 58.571, 58.835, 59.099, 59.364,  59.63, 59.896, 60.163, 60.431, 60.699, 60.968, 61.237, 61.507, 61.777, 62.048,  62.32, 62.592, 62.865, 63.139, 63.413, 63.688, 63.963, 64.239, 64.515, 64.792,  65.07, 65.348, 65.627, 65.907, 66.187, 66.467, 66.749, 67.031, 67.313, 67.596,  67.88, 68.164, 68.449, 68.735, 69.021, 69.308, 69.595, 69.883, 70.171,  70.46,  70.75,  71.04, 71.331, 71.623, 71.915, 72.208, 72.501, 72.795, 73.089, 73.384,  73.68, 73.976, 74.273, 74.571, 74.869, 75.168, 75.467, 75.767, 76.067, 76.368,  76.67, 76.972, 77.275, 77.579, 77.883, 78.188, 78.493, 78.799, 79.105, 79.412,  79.72, 80.028, 80.337, 80.647, 80.957, 81.267, 81.579, 81.891, 82.203, 82.516,  82.83, 83.144, 83.459, 83.775, 84.091, 84.408, 84.725, 85.043, 85.361,  85.68,     86,  86.32, 86.641, 86.963, 87.285, 87.607, 87.931, 88.255, 88.579, 88.904,  89.23, 89.556, 89.883, 90.211, 90.539, 90.868, 91.197, 91.527, 91.857, 92.188,  92.52, 92.852, 93.185, 93.519, 93.853, 94.188, 94.523, 94.859, 95.195, 95.532,  95.87, 96.208, 96.547, 96.887, 97.227, 97.568, 97.909, 98.251, 98.593, 98.936,  99.28, 99.624, 99.969, 100.31, 100.66, 101.01, 101.35,  101.7, 102.05,  102.4, 102.75,  103.1, 103.45,  103.8, 104.15, 104.51, 104.86, 105.21, 105.57, 105.92, 106.28, 106.64, 106.99, 107.35, 107.71, 108.07, 108.43, 108.79, 109.15, 109.51, 109.87, 110.23,  110.6, 110.96, 111.32, 111.69, 112.05, 112.42, 112.79, 113.15, 113.52, 113.89, 114.26, 114.63,    115, 115.37, 115.74, 116.11, 116.48, 116.86, 117.23,  117.6, 117.98, 118.35, 118.73, 119.11, 119.48, 119.86, 120.24, 120.62,    121, 121.38, 121.76, 122.14, 122.52, 122.91, 123.29, 123.67, 124.06, 124.44, 124.83, 125.22,  125.6, 125.99, 126.38, 126.77, 127.16, 127.55, 127.94, 128.33, 128.72, 129.11, 129.51,  129.9, 130.29, 130.69, 131.08, 131.48, 131.88, 132.27, 132.67, 133.07, 133.47, 133.87, 134.27, 134.67, 135.07, 135.47, 135.87, 136.28, 136.68, 137.08, 137.49, 137.89,  138.3, 138.71, 139.11, 139.52, 139.93, 140.34, 140.75, 141.16, 141.57, 141.98, 142.39, 142.81, 143.22, 143.63, 144.05, 144.46, 144.88,  145.3, 145.71, 146.13, 146.55, 146.97, 147.39, 147.81, 148.23, 148.65, 149.07, 149.49, 149.92, 150.34, 150.76, 151.19, 151.61, 152.04, 152.47, 152.89, 153.32, 153.75, 154.18, 154.61, 155.04, 155.47,  155.9, 156.33, 156.76,  157.2, 157.63, 158.06,  158.5, 158.93, 159.37, 159.81, 160.24, 160.68, 161.12, 161.56,    162, 162.44, 162.88, 163.32, 163.76, 164.21, 164.65, 165.09, 165.54, 165.98, 166.43, 166.88, 167.32, 167.77, 168.22, 168.67, 169.12, 169.57, 170.02, 170.47, 170.92, 171.37, 171.83, 172.28, 172.73, 173.19, 173.64,  174.1, 174.56, 175.01, 175.47, 175.93, 176.39, 176.85, 177.31, 177.77, 178.23, 178.69, 179.15, 179.62, 180.08, 180.54, 181.01, 181.47, 181.94, 182.41, 182.87, 183.34, 183.81, 184.28, 184.75, 185.22, 185.69, 186.16, 186.63, 187.11, 187.58, 188.05, 188.53,    189, 189.48, 189.96, 190.43, 190.91, 191.39, 191.87, 192.35, 192.83, 193.31, 193.79, 194.27, 194.75, 195.24, 195.72,  196.2, 196.69, 197.17, 197.66, 198.15, 198.63, 199.12, 199.61,  200.1, 200.59, 201.08, 201.57, 202.06, 202.55, 203.04, 203.54, 204.03, 204.52, 205.02, 205.51, 206.01, 206.51,    207,  207.5,    208,  208.5,    209,  209.5,    210,  210.5,    211, 211.51, 212.01, 212.51, 213.02, 213.52, 214.03, 214.54, 215.04, 215.55, 216.06, 216.57, 217.08, 217.59,  218.1, 218.61, 219.12, 219.63, 220.15, 220.66, 221.17, 221.69,  222.2, 222.72, 223.24, 223.75, 224.27, 224.79, 225.31, 225.83, 226.35, 226.87, 227.39, 227.91, 228.43, 228.96, 229.48,    230, 230.53, 231.05, 231.58, 232.11, 232.63, 233.16, 233.69, 234.22, 234.75, 235.28, 235.81, 236.34, 236.87, 237.41, 237.94, 238.47, 239.01, 239.54, 240.08, 240.62, 241.15, 241.69, 242.23, 242.77, 243.31, 243.85, 244.39, 244.93, 245.47, 246.01, 246.56,  247.1, 247.64, 248.19, 248.73, 249.28, 249.83, 250.37, 250.92, 251.47, 252.02, 252.57, 253.12, 253.67, 254.22, 254.77, 255.32, 255.88, 256.43, 256.98, 257.54, 258.09, 258.65, 259.21, 259.76, 260.32, 260.88, 261.44,    262, 262.56, 263.12, 263.68, 264.24, 264.81, 265.37, 265.93,  266.5, 267.06, 267.63,  268.2, 268.76, 269.33,  269.9, 270.47, 271.04, 271.61, 272.18, 272.75, 273.32, 273.89, 274.47, 275.04, 275.61, 276.19, 276.76, 277.34, 277.92, 278.49, 279.07, 279.65, 280.23, 280.81, 281.39, 281.97, 282.55, 283.13, 283.71,  284.3, 284.88, 285.46, 286.05, 286.63, 287.22, 287.81, 288.39, 288.98, 289.57, 290.16, 290.75, 291.34, 291.93, 292.52, 293.11, 293.71,  294.3, 294.89, 295.49, 296.08, 296.68, 297.28, 297.87, 298.47, 299.07, 299.67, 300.27, 300.87, 301.47, 302.07, 302.67, 303.27, 303.88, 304.48, 305.08, 305.69, 306.29,  306.9, 307.51, 308.11, 308.72, 309.33, 309.94, 310.55, 311.16, 311.77, 312.38, 312.99,  313.6, 314.22, 314.83, 315.44, 316.06, 316.67, 317.29, 317.91, 318.52, 319.14, 319.76, 320.38,    321 ] 
},
"facet": null,
"type": "point",
"color": "blue" 
} 
],
"facet": [],
"guides": [],
"coord": [],
"id": "chart1" 
}
    _.each(chartParams.layers, function(el){
        el.data = polyjs.data(el.data)
    })
    var graph_chart1 = polyjs.chart(chartParams);
</script>
r1$save('myPlot.html');
#r1$publish('myPlot.html',host='rpubs');

Roots Properties

The sum of the roots is

x1 + x2 = (-1*b)/a;

s <- x1 + x2;

and the product of the roots is

x1*x2 = c/a;

p <- x1*x2;