0% found this document useful (0 votes)
73 views11 pages

Source Codes Machine Problem 4

The document contains the source code for a C# Windows Forms application. It defines classes and methods for a form that allows users to input x and f(x) data points, stores them in lists, and calculates linear regression to find the regression coefficients a0 and a1. Buttons on the form call methods to add data, perform selection sort, compute regression, and display results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views11 pages

Source Codes Machine Problem 4

The document contains the source code for a C# Windows Forms application. It defines classes and methods for a form that allows users to input x and f(x) data points, stores them in lists, and calculates linear regression to find the regression coefficients a0 and a1. Buttons on the form call methods to add data, perform selection sort, compute regression, and display results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SOURCE CODES

Machine Problem 4
//Canlas, Melson Ziolo P.
//COE60/B1
//Prof. Carlos Hortinela

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usinginfo.lundin.math;

namespace Canlas_MP4
{
public partial class Form1 : Form
{
List<string> _names = new List<string>();
List<double[]> _dataArray = new List<double[]>();
List<string> _names2 = new List<string>();
List<double[]> _dataArray2 = new List<double[]>();
double[,] deltax;
List<double>xlist = new List<double>();
List<double>fxlist = new List<double>();
List<double>nlist = new List<double>();
List<double>templist = new List<double>();

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 about = new Form2();
about.Show();
}

private void NIaddvariable_Click(object sender, EventArgs e)


{
double x, fx;
x = double.Parse(NIxtbox.Text);
fx = double.Parse(NIfxtbox.Text);

xlist.Add(x);
fxlist.Add(fx);

counter.Text = "No of Variables :" + xlist.Count;


_names.Clear();
_names.Add("X");
_dataArray.Clear();
_names.Add("F(x)");

_dataArray.Add(xlist.ToArray());
_dataArray.Add(fxlist.ToArray());
dataGridView1.DataSource = GetResultsTable();
NIxtbox.Text = NIfxtbox.Text = "";
}
publicDataTableGetResultsTable()
{

DataTable d = new DataTable();

for (inti = 0; i< this._dataArray.Count; i++)


{
string name = this._names[i];
d.Columns.Add(name);
List<object>objectNumbers = new List<object>();

foreach (double number in this._dataArray[i])


objectNumbers.Add((object)number);

while (d.Rows.Count<objectNumbers.Count)
d.Rows.Add();

for (int a = 0; a <objectNumbers.Count; a++)


d.Rows[a][i] = objectNumbers[a];
}
return d;
}
publicDataTable GetResultsTable3()
{
DataTable d = new DataTable();
for (inti = 0; i< this._dataArray.Count; i++)
{
string name = this._names[i];
d.Columns.Add(name);
List<object>objectNumbers = new List<object>();
foreach (double number in this._dataArray[i])
objectNumbers.Add((object)number);
while (d.Rows.Count<objectNumbers.Count)
d.Rows.Add();
for (int a = 0; a <objectNumbers.Count; a++)
d.Rows[a][i] = objectNumbers[a];
}
return d;
}
publicDataTable GetResultsTable2()
{
DataTable d = new DataTable();
for (inti = 0; i< this._dataArray2.Count; i++)
{
string name = this._names2[i];
d.Columns.Add(name);
List<object>objectNumbers = new List<object>();
foreach (double number in this._dataArray2[i])
objectNumbers.Add((object)number);
while (d.Rows.Count<objectNumbers.Count)
d.Rows.Add();
for (int a = 0; a <objectNumbers.Count; a++)
d.Rows[a][i] = objectNumbers[a];
}
return d;
}
private void createfunction()
{
stringfnc = "";
for (intctr = 0; ctr<= xlist.Count - 1; ctr++)
{
fnc += "(" + fxlist[ctr].ToString() + ")";
for (intctrr = 0; ctrr<ctr; ctrr++)
{
fnc += "(x-" + xlist[ctrr] + ")";
}
if (ctr<xlist.Count - 1)
fnc += " +";
}
functiontext.Text = "Function: "+fnc;
}
private void NIcomputebutton_Click(object sender, EventArgs e)
{
selectionSort();
dataGridView1.DataSource = GetResultsTable();
int row = xlist.Count - 1, col = xlist.Count - 1, xctr = 1;
int n = 1, m = 1;
deltax = new double[row, col];
for (intcolctr = 0; colctr<= xlist.Count - 2; colctr++)
{
if (colctr == 0)
{
for (introwctr = 0; rowctr<= xlist.Count - n - 1; rowctr++)
{
deltax[rowctr, colctr] = ((fxlist[rowctr + 1] - fxlist[rowctr]) / (xlist[rowctr + 1] -
xlist[rowctr]));
}
}
else
{
for (introwctr = 0; rowctr<xlist.Count - m - 1; rowctr++)
{
deltax[rowctr, colctr] = ((deltax[rowctr + 1, colctr - 1] - deltax[rowctr, colctr - 1]) /
(xlist[xctr + m] - xlist[xctr - 1]));
xctr += 1;
}
xctr = 0;
m += 1;
xctr += 1;
}
}
_names2.Clear();
_names2.Add("X");
_dataArray2.Clear();
_names2.Add("F(x)");
_dataArray2.Add(xlist.ToArray());
_dataArray2.Add(fxlist.ToArray());
int columns = xlist.Count - 2;
string k;
for (int x = 0; x <xlist.Count - 1; x++)
{
for (int y = 0; y <= columns; y++)
{
templist.Add(deltax[y, x]);
}
_dataArray2.Add(templist.ToArray());
columns -= 1;
templist.Clear();
k = "Delta X" + x;
_names2.Add(k);
}

dataGridView2.DataSource = GetResultsTable2();
createfunction();

}
voidselectionSort()
{
int index;
intsmallestIndex;
int location;
double temp, temp2;
for (index = 0; index <xlist.Count - 1; index++)
{
smallestIndex = index;
for (location = index + 1; location <xlist.Count; location++)
if (xlist[location] <xlist[smallestIndex])
smallestIndex = location;
temp = xlist[smallestIndex];
temp2 = fxlist[smallestIndex];
xlist[smallestIndex] = xlist[index];
fxlist[smallestIndex] = fxlist[index];
xlist[index] = temp;
fxlist[index] = temp2;
}
}
private void button4_Click(object sender, EventArgs e)
{
doubleey = 0, ex = 0, exy = 0, ex2 = 0, eym = 0, ee2 = 0, a0 = 0, a1 = 0, xbar = 0, ybar = 0;
double x1, x2, x3, x4, x5, x6, x7;
double y1, y2, y3, y4, y5, y6, y7;
double ym1, ym2, ym3, ym4, ym5, ym6, ym7;
double e1, e2, e3, e4, e5, e6, e7;
button5.Show();
label7.Show();
label8.Show();
label9.Show();
label10.Show();
label11.Show();
label12.Show();
label13.Show();
label14.Show();
label15.Show();
label16.Show();
label17.Show();
textBox15.Show();
textBox16.Show();
textBox17.Show();
textBox18.Show();
textBox19.Show();
textBox20.Show();
textBox21.Show();
textBox22.Show();
textBox23.Show();
textBox24.Show();
listBox1.Show();
listBox2.Show();
listBox3.Show();
listBox4.Show();
listBox5.Show();
listBox6.Show();
x1 = double.Parse(textBox1.Text);
x2 = double.Parse(textBox3.Text);
x3 = double.Parse(textBox5.Text);
x4 = double.Parse(textBox7.Text);
x5 = double.Parse(textBox9.Text);
x6 = double.Parse(textBox11.Text);
x7 = double.Parse(textBox13.Text);
listBox1.Items.Add(x1);
listBox1.Items.Add(x2);
listBox1.Items.Add(x3);
listBox1.Items.Add(x4);
listBox1.Items.Add(x5);
listBox1.Items.Add(x6);
listBox1.Items.Add(x7);
ex = x1 + x2 + x3 + x4 + x5 + x6 + x7;
textBox15.Text = ex.ToString();
listBox3.Items.Add(x1 * x1);
listBox3.Items.Add(x2 * x2);
listBox3.Items.Add(x3 * x3);
listBox3.Items.Add(x4 * x4);
listBox3.Items.Add(x5 * x5);
listBox3.Items.Add(x6 * x6);
listBox3.Items.Add(x7 * x7);
ex2 = x1 * x1 + x2 * x2 + x3 * x3 + x4 * x4 + x5 * x5 + x6 * x6 + x7 * x7;
textBox17.Text = ex2.ToString();
y1 = double.Parse(textBox2.Text);
y2 = double.Parse(textBox4.Text);
y3 = double.Parse(textBox6.Text);
y4 = double.Parse(textBox8.Text);
y5 = double.Parse(textBox10.Text);
y6 = double.Parse(textBox12.Text);
y7 = double.Parse(textBox14.Text);
listBox2.Items.Add(y1);
listBox2.Items.Add(y2);
listBox2.Items.Add(y3);
listBox2.Items.Add(y4);
listBox2.Items.Add(y5);
listBox2.Items.Add(y6);
listBox2.Items.Add(y7);
ey = y1 + y2 + y3 + y4 + y5 + y6 + y7;
textBox16.Text = ex.ToString();
listBox4.Items.Add(x1 * y1);
listBox4.Items.Add(x2 * y2);
listBox4.Items.Add(x3 * y3);
listBox4.Items.Add(x4 * y4);
listBox4.Items.Add(x5 * y5);
listBox4.Items.Add(x6 * y6);
listBox4.Items.Add(x7 * y7);
exy = x1 * y1 + x2 * y2 + x3 * y3 + x4 * y4 + x5 * y5 + x6 * y6 + x7 * y7;
textBox18.Text = exy.ToString();
xbar = ex / 7;
textBox21.Text = xbar.ToString("F4");
ybar = ey / 7;
textBox22.Text = ybar.ToString("F4");
a1 = (7 * exy - ex * ey) / (7 * ex2 - (ex * ex));
textBox24.Text = a1.ToString("F4");
a0 = (ey / 7) - a1 * (ex / 7);
textBox23.Text = a0.ToString("F4");
ym1 = a0 + (a1 * x1);
listBox5.Items.Add(Math.Round(ym1, 4));
ym2 = a0 + (a1 * x2);
listBox5.Items.Add(Math.Round(ym2, 4));
ym3 = a0 + (a1 * x3);
listBox5.Items.Add(Math.Round(ym3, 4));
ym4 = a0 + (a1 * x4);
listBox5.Items.Add(Math.Round(ym4, 4));
ym5 = a0 + (a1 * x5);
listBox5.Items.Add(Math.Round(ym5, 4));
ym6 = a0 + (a1 * x6);
listBox5.Items.Add(Math.Round(ym6, 4));
ym7 = a0 + (a1 * x7);
listBox5.Items.Add(Math.Round(ym7, 4));
eym = ym1 + ym2 + ym3 + ym4 + ym5 + ym6 + ym7;
textBox19.Text = eym.ToString("F4");
e1 = (y1 - a0 - a1 * x1) * (y1 - a0 - a1 * x1);
listBox6.Items.Add(Math.Round(e1, 4));
e2 = (y2 - a0 - a1 * x2) * (y2 - a0 - a1 * x2);
listBox6.Items.Add(Math.Round(e2, 4));
e3 = (y3 - a0 - a1 * x3) * (y3 - a0 - a1 * x3);
listBox6.Items.Add(Math.Round(e3, 4));
e4 = (y4 - a0 - a1 * x4) * (y4 - a0 - a1 * x4);
listBox6.Items.Add(Math.Round(e4, 4));
e5 = (y5 - a0 - a1 * x5) * (y5 - a0 - a1 * x5);
listBox6.Items.Add(Math.Round(e5, 4));
e6 = (y6 - a0 - a1 * x6) * (y6 - a0 - a1 * x6);
listBox6.Items.Add(Math.Round(e6, 4));
e7 = (y7 - a0 - a1 * x7) * (y7 - a0 - a1 * x7);
listBox6.Items.Add(Math.Round(e7, 4));
ee2 = e1 + e2 + e3 + e4 + e5 + e6 + e7;
textBox20.Text = ee2.ToString("F4");
}
private void button8_Click(object sender, EventArgs e)
{
double r1;
doublesimpn, simpxa, simpxb, simph, simpf=0, simpfi;
double xb1, xb2, xb3, xb4, xb5, xb6;
double[] sg = new double[15];
intss = 0;
doublesimpsum;
double simptrue1;
label47.Show();
label48.Show();
label49.Show();
label51.Show();
label52.Show();
textBox45.Show();
textBox46.Show();
listBox10.Show();
listBox11.Show();
listBox12.Show();
button9.Show();
xb1 = Convert.ToDouble(textBox41.Text);
xb2 = Convert.ToDouble(textBox40.Text);
xb3 = Convert.ToDouble(textBox39.Text);
xb4 = Convert.ToDouble(textBox38.Text);
xb5 = Convert.ToDouble(textBox37.Text);
xb6 = Convert.ToDouble(textBox36.Text);
simpn = Convert.ToDouble(textBox42.Text);
simpxa = Convert.ToDouble(textBox43.Text);
simpxb = Convert.ToDouble(textBox44.Text);
simph = (simpxb - simpxa) / simpn;
for (inti = 0; i< (simpn + 1); i++)
{
if (i == 0)
simpf = (simpxa);
else
simpf = simpf + simph;
listBox10.Items.Add(Math.Round(simpf, 4));
}
for (int j = 0; j < (simpn + 1); j++)
{
if (j == 0)
simpf = (simpxa);
else
simpf = simpf + simph;
simpfi = xb1 + (xb2 * simpf) + (xb3 * simpf * simpf) + (xb4 * simpf * simpf * simpf) + (xb5 *
simpf * simpf * simpf * simpf) + (xb6 * simpf * simpf * simpf * simpf * simpf);
listBox11.Items.Add(Math.Round(simpfi, 4));
}
for (int k = 0; k < (simpn + 1); k++)
{
if (k == 0)
simpf = (simpxa);
else
simpf = simpf + simph;
simpfi = xb1 + (xb2 * simpf) + (xb3 * simpf * simpf) + (xb4 * simpf * simpf * simpf) + (xb5 *
simpf * simpf * simpf * simpf) + (xb6 * simpf * simpf * simpf * simpf * simpf);
if (k == 0 || k == simpn)
r1 = (1 * simpfi);
else if ((k % 2) == 0)
{
r1 = (2 * simpfi);
}

else
{
r1 = (4 * simpfi);
}
listBox12.Items.Add(Math.Round(r1, 4));
sg[ss] = r1;
ss++;

}
simpsum = sg[0] + sg[1] + sg[2] + sg[3] + sg[4] + sg[5] + sg[6] + sg[7] + sg[8];
textBox45.Text = (Convert.ToString(Math.Round(simpsum, 4)));
simptrue1 = ((simph / 3) * simpsum);
textBox46.Text = (Convert.ToString(Math.Round(simptrue1, 4)));
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
textBox13.Clear();
textBox14.Clear();
textBox15.Clear();
textBox16.Clear();
textBox17.Clear();
textBox18.Clear();
textBox19.Clear();
textBox20.Clear();
textBox21.Clear();
textBox22.Clear();
textBox23.Clear();
textBox24.Clear();
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
listBox5.Items.Clear();
listBox6.Items.Clear();
label7.Hide();
label8.Hide();
label9.Hide();
label10.Hide();
label11.Hide();
label12.Hide();
label13.Hide();
label14.Hide();
label15.Hide();
label16.Hide();
label17.Hide();
textBox15.Hide();
textBox16.Hide();
textBox17.Hide();
textBox18.Hide();
textBox19.Hide();
textBox20.Hide();
textBox21.Hide();
textBox22.Hide();
textBox23.Hide();
textBox24.Hide();
listBox1.Hide();
listBox2.Hide();
listBox3.Hide();
listBox4.Hide();
listBox5.Hide();
listBox6.Hide();
button5.Hide();
}
private void button9_Click(object sender, EventArgs e)
{
textBox36.Clear();
textBox37.Clear();
textBox38.Clear();

textBox39.Clear();
textBox40.Clear();
textBox41.Clear();
textBox42.Clear();
textBox43.Clear();
textBox44.Clear();
textBox45.Clear();
textBox46.Clear();
listBox10.Items.Clear();
listBox11.Items.Clear();
listBox12.Items.Clear();

label47.Hide();
label48.Hide();
label49.Hide();
label51.Hide();
label52.Hide();
textBox45.Hide();
textBox46.Hide();
listBox10.Hide();
listBox11.Hide();
listBox12.Hide();
button9.Hide();
}

private void NIcomputebutton_Click_1(object sender, EventArgs e)


{
selectionSort();
dataGridView1.DataSource = GetResultsTable();
int row = xlist.Count - 1, col = xlist.Count - 1, xctr = 1;
int n = 1, m = 1;
deltax = new double[row, col];
for (intcolctr = 0; colctr<= xlist.Count - 2; colctr++)
{
if (colctr == 0)
{
for (introwctr = 0; rowctr<= xlist.Count - n - 1; rowctr++)
{
deltax[rowctr, colctr] = ((fxlist[rowctr + 1] - fxlist[rowctr]) / (xlist[rowctr + 1] -
xlist[rowctr]));
}
}
else
{
for (introwctr = 0; rowctr<xlist.Count - m - 1; rowctr++)
{
deltax[rowctr, colctr] = ((deltax[rowctr + 1, colctr - 1] - deltax[rowctr, colctr - 1]) /
(xlist[xctr + m] - xlist[xctr - 1]));
xctr += 1;
}
xctr = 0;
m += 1;
xctr +=0;
}
_names2.Clear();
_names2.Add("X");
_dataArray2.Clear();
_names2.Add("F(x)");
_dataArray2.Add(xlist.ToArray());
_dataArray2.Add(fxlist.ToArray());
int columns = xlist.Count - 2;
string k;

for (int x = 0; x <xlist.Count - 1; x++)


{
for (int y = 0; y <= columns; y++)
{
templist.Add(deltax[y, x]);
}

_dataArray2.Add(templist.ToArray());
columns -= 1;
templist.Clear();
k = "Delta X" + x;
_names2.Add(k);
}

dataGridView2.DataSource = GetResultsTable2();
createfunction();
}
private void button3_Click(object sender, EventArgs e)
{
double h, n=1,a=double.Parse(alimit.Text),b=double.Parse(blimit.Text),efex=0,index;
ExpressionParser parser = new ExpressionParser();
DoubleValuevariaball = new DoubleValue();
parser.Values.Add("x", variaball);

do
{
h = (b - a) / n;

doublectr = 0;
ctr = a;
while(ctr<=b)
{
variaball.Value = ctr;
if ((ctr == a) || (ctr == b))
{
efex += index=parser.Parse(REfunctionbox.Text);
}
else
{
efex +=index= (2 * parser.Parse(REfunctionbox.Text));
}
variaball.Value += Math.Round(h,1);
ctr += h;
ctr = Math.Round(ctr, 1);
}

index = Math.Round((h*efex) / (2),4);


nlist.Add(n);
xlist.Add(h);
fxlist.Add(index);
efex = 0;
n = 2*n;

} while (h > .1);

int row = fxlist.Count - 1, col = fxlist.Count - 1, xctr = 2;


int mm = 1, nn = 1;
deltax = new double[row, col];
for (intcolctr = 0; colctr<= xlist.Count - 2; colctr++)
{
if (colctr == 0)
{
for (introwctr = 0; rowctr<= xlist.Count - mm - 1; rowctr++)
{
deltax[rowctr, colctr] = ((fxlist[rowctr + 1])+ ((fxlist[rowctr+1]-fxlist[rowctr])/
((Math.Pow(2,xctr))-1)) );
deltax[rowctr, colctr] = Math.Round(deltax[rowctr, colctr], 4);
}
xctr += 1;
}

else
{

for (introwctr = 0; rowctr<xlist.Count - mm - 1; rowctr++)


{
deltax[rowctr, colctr] = (deltax[rowctr + 1, colctr - 1] + (deltax[rowctr+1,colctr-1]-
deltax[rowctr, colctr - 1])/(Math.Pow(2, xctr) -1));
deltax[rowctr, colctr] = Math.Round(deltax[rowctr, colctr], 4)
}
xctr += 1;
mm += 1;
xctr += 1;
}
}
_names.Clear();
_names.Add("n");
_dataArray.Clear();
_names.Add("h");
_names.Add("I(h)");
_dataArray.Add(nlist.ToArray());
_dataArray.Add(xlist.ToArray());
_dataArray.Add(fxlist.ToArray());
int columns = xlist.Count - 2;
string k;
for (int x = 0; x <xlist.Count - 1; x++)
{
for (int y = 0; y <= columns; y++)
{
templist.Add(deltax[y, x]);
}
_dataArray.Add(templist.ToArray());
columns -= 1;
templist.Clear();
k = "K=" + (x + 2).ToString(); ;
_names.Add(k);
}
dataGridView3.DataSource = GetResultsTable3();
createfunction();
}
private void button12_Click(object sender, EventArgs e)
{
_dataArray.Clear();
_names.Clear();
fxlist.Clear();
nlist.Clear();
xlist.Clear();
templist.Clear();
dataGridView3.DataSource = GetResultsTable3();
alimit.Text = blimit.Text = REfunctionbox.Text = "";
}
}

You might also like