C sharp - ecuaciones en C#

 
Vista:

ecuaciones en C#

Publicado por Mariett (1 intervención) el 15/12/2013 16:07:55
Buenos días, mi pregunta es la siguiente.

Tengo el programa que esta a continuación, y cuando entro en el primer thread llamado (controlobservador) tengo un problema ante las mismas condiciones la ecuación Xac[2] calcula diferentes valores, es decir tengo la ecuación

Xac[2] = (X) - (To * 0.007230 * (X)) + (To * 0.812 * Ref) - (To * 0.1267 * (Xo[0] - 14.6474));

para los mismos valores de X y Xo[0] en diferentes corridas calcula distintos valores, porque? hay alguna librería que no estoy usando? parece que realiza mal el calculo matemático, otra cosa es que al final del hilo yo redondeo el valor medido Xo[0], sera que no lo esta leyendo redondeado?..

Que algoritmo esta utilizando visual para ejecutar esta ecuación?

por favor agradecería su ayuda porque estoy teniendo discrepancia en mis resultados y no entiendo porque el visual esta calculando mal...

using System;

using System.IO;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using LabJack.LabJackUD;
using System.Windows.Forms.DataVisualization.Charting;



namespace controlobservador
{
public partial class Form1 : Form
{
private U3 u3;
double X, suma, promedio, AIN0 = 0, AIN1 = 0, tiempo = 0, tiempo1=0, Uant=0, Ref=0, R1, R2, To = 1, Udelta = 0, U, H2 = 4.8410, det = 0.9912, H1 = 14.6474;
Thread h1, h2;
double[] Bd = new double[2] { 0.0105, 0.000025021 };
double[] Xo = new double[5] { 0, 0, 0, 0, 0 };
double[] Xac = new double[5];
double[] nivel1;
bool pv = true;
String aux;
String aux1;
delegate void SetTextCallback(string text);
delegate void SetChartCallback(Chart chart);

public Form1()
{
InitializeComponent();
u3 = new U3(LJUD.CONNECTION.USB, "0", true);
LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);

}

public void enviar(double entrada)
{
double dblValue0;
if (entrada == 0)
dblValue0 = 0;
else
dblValue0 = (0.44 * entrada) + 0.9;

int binary = 0;
LJUD.eDAC(u3.ljhandle, 0, dblValue0, binary, 0, 0);


}
public void Medicion()
{

double dblValue = 99999;
int binary = 0;
LJUD.eAIN(u3.ljhandle, 0, 31, ref dblValue, 0, 0, 0, binary);
AIN0 = (6.7 * (dblValue));


}
public void Medicion1()
{
double dblValue1 = 99999;
int binary = 0;
LJUD.eAIN(u3.ljhandle, 1, 31, ref dblValue1, 0, 0, 0, binary);
AIN1 = (2.5 * dblValue1) - 2.2;

}
private void SetText1(string text1)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText1);
this.Invoke(d, new object[] { text1 });
}
else
{
this.textBox1.Text = text1;
}
}
private void SetText2(string text2)
{
if (this.textBox2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText2);
this.Invoke(d, new object[] { text2 });
}
else
{
this.textBox2.Text = text2;
}
}
private void SetText3(string text3)
{
if (this.textBox3.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText3);
this.Invoke(d, new object[] { text3 });
}
else
{
this.textBox3.Text = text3;
}
}
private void SetText4(string text4)
{
if (this.textBox4.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText4);
this.Invoke(d, new object[] { text4 });
}
else
{
this.textBox4.Text = text4;
}
}
private void SetChart1(Chart chart1)
{
if (this.chart1.InvokeRequired)
{
SetChartCallback g = new SetChartCallback(SetChart1);
this.Invoke(g, new object[] { chart1 });
}
else
{
chart1.Series["Residuo 1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["Residuo 1"].Points.AddXY(tiempo, R1);
chart1.Series["Residuo 1"].ChartArea = "ChartArea1";

}

}
private void SetChart2(Chart chart2)
{
if (this.chart1.InvokeRequired)
{
SetChartCallback g = new SetChartCallback(SetChart2);
this.Invoke(g, new object[] { chart2 });
}
else
{
chart2.Series["Residuo 2"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart2.Series["Residuo 2"].Points.AddXY(tiempo, R2);
chart2.Series["Residuo 2"].ChartArea = "ChartArea1";

}

}

private void SetChart3(Chart chart3)
{
if (this.chart1.InvokeRequired)
{
SetChartCallback g = new SetChartCallback(SetChart3);
this.Invoke(g, new object[] { chart3 });
}
else
{
chart3.Series["Nivel tanque 1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart3.Series["Nivel tanque 1"].Points.AddXY(tiempo, Xo[0]);
chart3.Series["Nivel tanque 1"].ChartArea = "ChartArea1";

}

}

private void SetChart4(Chart chart4)
{
if (this.chart1.InvokeRequired)
{
SetChartCallback g = new SetChartCallback(SetChart4);
this.Invoke(g, new object[] { chart4 });
}
else
{
chart4.Series["Señal de control"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart4.Series["Señal de control"].Points.AddXY(tiempo, Uant);
chart4.Series["Señal de control"].ChartArea = "ChartArea1";

}

}
public void cargar( double nivel)
{

nivel1 = new double[5];
for (int f = 0; f < 5; f++)
{
nivel1[f] = nivel;
}
}

public void Calcularpromedio()//aqui es donde calculo el promedio
{
suma = 0;
for (int f = 0; f < 5; f++)
{
suma = suma + nivel1[f];
}
promedio = suma / 5;

}

public void condicion()
{
X=Uant-3+94.6673*(Xo[0]-H1);
//pv = false;
}
private void controlobservador()
{
{

while (true)
{
condicion();
this.SetText3(Convert.ToString(Math.Round(Xo[0], 2)));
this.SetText4(Convert.ToString(Uant));
this.SetChart3(chart3);
this.SetChart4(chart4);

aux = Convert.ToString(Math.Round(Xo[0], 2));
aux = aux + " " + Convert.ToString(tiempo) + " " + Convert.ToString(Math.Round(Xo[1], 2)) + " " + Convert.ToString(Udelta + 3) + " " + Convert.ToString(Uant) + " " + Convert.ToString(Math.Round(X, 2));
try
{
string fileName = "data.txt";
// esto inserta texto en un archivo existente, si el archivo no existe lo crea
StreamWriter writer = File.AppendText(fileName);
writer.WriteLine(aux);
writer.Close();
}
catch
{
Console.WriteLine("Error");
}

Xac[0] = 0;
Xac[1] = 0;
Medicion();
Medicion1();
Xac[0] = AIN0;
Xac[1] = AIN1;

cargar(Xac[0]);//aqui le mando a cargar el valor de Xac[0]
// Calcularpromedio();


if (Xac[0] >= -1 && Xac[0] <= 2)
U = 2;
if (Xac[0] > 2 && Xac[0] <= 8)
U = 9;
if (Xac[0] > 8 && Xac[0] <= 13.5)
U = 4;
if (Xac[0] > 13.5)
{

Math.Round(X, 2);
pv = false;
Udelta = 0;
Xac[2] = 0;
Xac[2] = (X) - (To * 0.007230 * (X)) + (To * 0.812 * Ref) - (To * 0.1267 * (Xo[0] - 14.6474));//AQUI ESTA EL ERROR
Udelta = (Xac[2]) - (94.6673 * (Xac[0] - 14.6474));
U = 3 + Udelta;
X = Math.Round(Xac[2], 2);

if(U >= 9)
U = 9;
if (U <= 0)
U = 0;

}
enviar(U);
Xo[0] = Math.Round(Xac[0], 2);
Xo[1] = Math.Round(Xac[1], 2);
Uant = Math.Round(U, 2);
tiempo = tiempo + To;
Thread.Sleep(1000);
}
}
}
private void observador()
{
{

while (true)
{


this.SetText1(Convert.ToString(R1));
this.SetText2(Convert.ToString(R2));
this.SetChart1(chart1);
this.SetChart2(chart2);
aux1 = Convert.ToString(R1);
aux1 = aux1 + " " + Convert.ToString(tiempo1) + " " + Convert.ToString(R2);
try
{
string fileName = "data1.txt";
// esto inserta texto en un archivo existente, si el archivo no existe lo crea
StreamWriter writer = File.AppendText(fileName);
writer.WriteLine(aux1);
writer.Close();
}
catch
{
Console.WriteLine("Error");
}

Medicion();
Medicion1();
Xac[0] = AIN0;
Xac[1] = AIN1;
//observador
Xac[3] = (det * 1.0072 * Xo[3]) + (det * 0.0016 * Xo[4]) + (det * 0.0105 * Udelta) + (det * 1.0056 * ((Xo[0] - H1) - Xo[3])) + (det * 0.0032 * ((Xo[1] - H2) - Xo[4]));
Xac[4] = (det * 0.0024 * Xo[3]) + (det * 1.0016 * Xo[4]) + (det * 0 * Udelta) + (det * 0.0048 * ((Xo[0] - H1) - Xo[3])) + (det * 0.9944 * ((Xo[1] - H2) - Xo[4]));
R1 = (Xac[0] - H1) - Xac[3];
R2 = (Xac[1] - H2) - Xac[4];

Xo[0] = Xac[0];
Xo[1] = Xac[1];

Xo[3] = Xac[3];
Xo[4] = Xac[4];
Uant = U;

tiempo1 = tiempo1 + To;
Thread.Sleep(1000);
}
}
}


private void comenzar_Click_1(object sender, EventArgs e)
{

h1 = new Thread(new ThreadStart(controlobservador));
h2 = new Thread(new ThreadStart(observador));
h1.Start();
h2.Start();

}

private void button1_Click(object sender, EventArgs e)
{

h1.Abort();
h2.Abort();
Xo = new double[5] { 0, 0, 0, 0, 0 };
U = 0;
Uant = 0;
enviar(U);
}
}

}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

ecuaciones en C#

Publicado por Pico (114 intervenciones) el 28/01/2014 21:15:47
Es un poco complicado ver algo en el código que has puesto, pero una causa que provoca un resultado aleatorio es el acceso a una variable por dos hilos a la vez, sin controlar que cuando acceda uno no intente accedre el otro. Lo haces en el acceso a controles textbox, pero a variables de esa fórmula que te da el error no sé si lo haces.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar