-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (90 loc) · 3.62 KB
/
Program.cs
File metadata and controls
93 lines (90 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Globalization;
namespace ExponentialRegression
{
class Program
{
static void Main(string[] args)
{
float x = 0;
float x2sum = 0;
float y = 0;
float sumxlny = 0;
float sumlny = 0;
CultureInfo culture = new CultureInfo("id-ID");
Console.WriteLine("This program will determine exponential regression for given data.");
Console.WriteLine("How many data samples?");
int n = Convert.ToInt16(Console.ReadLine());
List<float> columnx = new List<float>();
List<float> columny = new List<float>();
for (int i = 1; i <= n; i++)
{
Console.WriteLine("x" + i + ":");
columnx.Add(Convert.ToSingle(Console.ReadLine()));
Console.WriteLine("y" + i + ":");
columny.Add(Convert.ToSingle(Console.ReadLine()));
}
List<float> columnlny = new List<float>();
List<float> columnx2 = new List<float>();
// print column1
Console.WriteLine("Column 1:");
foreach (float element in columnx)
{
Console.WriteLine(Convert.ToDecimal(element, culture));
x += element;
columnx2.Add((float)Math.Pow(element, 2));
}
foreach (float element in columnx2)
{
x2sum += element;
}
// print column2
Console.WriteLine("Column 2:");
foreach (float element in columny)
{
Console.WriteLine(Convert.ToDecimal(element, culture));
y += element;
float lny = (float)Math.Log(Convert.ToDouble(element), Math.E);
columnlny.Add(lny);
}
Console.WriteLine("Processing file...");
List<float> colxlny = new List<float>();
string sigma = "\u03A3";
Console.WriteLine("Total data entries (n) = " + Convert.ToString(n));
Console.WriteLine(sigma + "x = " + Convert.ToString(x));
Console.WriteLine(sigma + "y = " + Convert.ToString(y));
Console.WriteLine(sigma + "x\u00B2 = " + Convert.ToString(x2sum));
for (int i = 0; i < n; i++)
{
colxlny.Add(columnx[i] * columnlny[i]);
sumxlny += colxlny[i];
sumlny += columnlny[i];
}
Console.WriteLine(sigma + "ln(y) = " + Convert.ToString(sumlny));
Console.WriteLine(sigma + "(x ln(y)) = " + Convert.ToString(sumxlny));
Console.WriteLine("Calculating a and b...");
float a = (float)(((n * sumxlny) - (x * sumlny)) / ((n * x2sum) - Math.Pow(x, 2)));
float b = (float)((sumlny / n) - (a * x / n));
Console.WriteLine("Final result:");
Console.WriteLine("y=e^(" + a + "x + (" + b + "))");
Console.WriteLine("Find y value? (y/n) (leave blank to exit)");
switch (Console.ReadLine().ToLower())
{
case "y":
Console.WriteLine("Writing for x = 0 to x = 1,0");
for (int i = 0; i <= 10; i++)
{
float v = (float)i / 10;
float z = (float)Math.Exp((a * v) + b);
Console.WriteLine(v + " = " + z);
}
break;
case "n":
return;
default:
return;
}
}
}
}