You are on page 1of 14

MC LC

S 1...................................................................................................................... 2
1. Xy dng hm truyn v phng trnh sai phn ca h. ..................................... 2
2. Code chng trnh vi ngn ng C# ................................................................... 3
3. In kt qu ng cong qu ............................................................................ 10
4. So snh kt qu vi phn mm Matlab. ............................................................. 12
5. In kt qu y[k] .................................................................................................... 13
6. Cc ch tiu cht lng ca h thng ................................................................. 13
7. Nhn xt ............................................................................................................. 14
8. Ti liu tham kho ............................................................................................. 14

S 1

1. Xy dng hm truyn v phng trnh sai phn ca h.


Ta c hm truyn kn ca h thng l:
1
2
1 2
1 + 1 2 + 1
() =
=
1 . 2 . 3
(1 + 1)(2 + 1) + 1 2 3
1+
(1 + 1)(2 + 1)
() =

1 2
1 2 2 + (1 + 2 ) + 1 2 3 + 1

2 1

Thay = .

+1

vo hm W(s) ta c:

2 1
1 2 . ( .
+ 1)
() =
2 1 2
2 1
1 2 . ( . + 1) + (1 + 2 ) ( . + 1) + 1 2 3 + 1
1 2 2 ( + 1)2
() =
41 2 ( 1)2 + (1 2 3 + 1) 2 ( + 1)2 + 2(1 + 2 )( + 1)( 1)
Bin i v dng:
() 1 2 2 ( 2 + 2 + 1)
=
()
2 + +
Trong :

= 41 2 + 2(1 + 2 ) + (1 2 3 + 1) 2
= 81 2 + 2 2 (1 2 3 + 1)
= 41 2 2(1 + 2 ) + (1 2 3 + 1) 2
Az2Y(z) + BzY(z) + CY(z) = K1K2T2 [z2U(z) + 2zU(z) + U(z)]
Dng tnh cht dch hm gc c bin i Z ta tm c phng trnh sai phn tng
ng:
AY[k+2] + BY[k+1] + CY[k] = K1K2T2 (U[k+2] + 2 U[k+1] + U[k])
V tn hiu vo l tn hiu nhy cp U(t) = 1(t) nn ta c:
U[k+2] = U[k+1] = U[k] = 1
AY[k+2] + BY[k+1] + CY[k] = 4K1K2T2
Y[k+2] = (4K1K2T2 BY[k+1] CY[k]) /A
T ta c cng thc tnh cc gi tr p ng y(t) nh sau:
y[0] = y[1] = 0;
y[k+2] = (4K1K2T2 By[k+1] Cy[k]) /A;
k=0, 1, 2999; A, B, C l cc hng s tnh trn.

2. Code chng trnh vi ngn ng C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form

{
private Graphics graph;
private int x0, y0, xMax, yMax;
// ta gc O, xMax, yMax
private int min, max;
private int delta_x=30;
private int delta_y=30;
// lch trc ta khi v
private double dx, x, y;
private float x1, y1, x2, y2;
private bool OnDinh = true;
private float K1, K2, K3, T1, T2, T;
//thng s u vo
private float Tmin;
private float ymax, yod, x_ymax, Tod, Tmax, xicma;
//Thng s u ra cn tnh ton
private int x_Tod;
// Xc nh ta ca Tod;

private float unit_x, unit_y;


private float A, B, C;

// chia v th

public Form1()
{
InitializeComponent();
graph = DrawArea.CreateGraphics();
xMax = DrawArea.Width;
yMax = DrawArea.Height;
x0 = 0;
y0 = DrawArea.Height;
}
private void button1_Click(object sender, EventArgs e)
{
int sopt = 1000;
graph.Clear(Color.White);
float[] y = new float[sopt];
PointF[] pnt = new PointF[sopt];
Pen blackPen = new Pen(Color.Black, 1);
Pen redPen = new Pen(Color.Red, 1);
Pen greenPen = new Pen(Color.Green, 3);
int index = 0;
for (int i = 0; i < 100; i++)
{
// 100 ch s im y[k] cn in ra.
comboBox_item.Items.Add(index);

index += 10;
}
listBox1.Items.Clear(); // phi xa rng listbox mi ln cp nht li th
// Kim tra nhp thng s hp l hay khng
if (float.TryParse(textBoxK1.Text, out K1) && float.TryParse(textBoxK2.Text, out K2)
&& float.TryParse(textBoxT2.Text, out T2) && float.TryParse(textBoxT1.Text, out T1) &&
float.TryParse(textBoxT.Text, out T)
&& float.TryParse(textBoxT2.Text, out K3))
{
if (T1 < T2) { Tmin = T1/20; }
else { Tmin = T2 / 20; }
if (T > Tmin)
{ MessageBox.Show("Xin nhp li chu k trich mu T, T<= (1/20) Min(T1,T2) !"); } // chu k
trch mu
else
{
A = 4 * T1 * T2 + 2*T*(T1+T2) + (1 + K1 * K2 * K3) *T*T;
B = (-8*T1*T2 +(2 +2*K1*K2*K3) *T*T);
C = 4*T1*T2 - 2*T*(T1+T2) + T*T*(1 + K1 *K2 *K3);
float a0 = (1 + K1 * K2 * K3);
float a1 = (T1 + T2);
float a2 = (T1 * T2);
//Kiem tra tinh on dinh he thong theo tieu chuan Hurwits
if ((a1 > 0) && (a2 > 0)) { OnDinh = true; }
else OnDinh = false;
y[0] = 0; y[1] = 0;
ymax = y[1];
float ymin = 0;
for (int i = 0; i < (sopt - 2); i++)
{
y[i + 2] = (4*K1*K2*T*T - B *y[i+1] - C* y[i]) / A;
if (ymax < y[i]) { ymax = y[i]; x_ymax = i; } // Tm v tr v gi tr ca ymax
if (ymin > y[i]) { ymin = y[i]; }
}
if (ymin < 0) { y0 = DrawArea.Height / 2; }
// ymin dng v trc ta khi c gi tr y m!!! => c th h thng ko n nh
else { y0 = DrawArea.Height; }
//=== Tnh ton li chia th nm chn trong "gn" trong picturebox
unit_x = (DrawArea.Width - 3 * delta_x) / (T * 1000);
unit_y = (y0 - 3 * delta_y) / ymax;

for (int j = 0; j <= (sopt - 1); j++)


{
// Cp nht cc im th ->pnt
float toado_x = +j * T;
float toado_y = y[j];
pnt[j].X = (float)(delta_x + toado_x * unit_x);
//nhn vi chia full mn hnh
pnt[j].Y = (float)( y0 - toado_y * unit_y - delta_y);
if (j % 10 == 0)
{ //Ly 4 ch s sau du phy
int tmp = (int)(y[j] * Math.Pow(10, 4));
float _item = (float)(tmp / Math.Pow(10, 4));
listBox1.Items.Add(_item); // C 10 phn t th thm y[] vo listbox
}
// listBox1.DataSource = y; add c mng y
// listBox1.Items.Add(y[0]); add tng phn t
}
VeTrucToaDo();
// V trc ta
graph.DrawLines(greenPen, pnt);
// V th
// V vch chia honh
Font f = new Font("Tahoma", 12);
Brush b = new SolidBrush(Color.Red);
float chi_so = 0;
for (int i = 0; i < 10; i++)
{ chi_so += 100;
graph.DrawLine(blackPen, delta_x + chi_so * T * unit_x, y0 - delta_y - 3, delta_x + chi_so
* T * unit_x, y0 - delta_y + 3);
graph.DrawString(chi_so.ToString(), f, b, delta_x + (chi_so - 15) * T * unit_x, y0 - 20);
}
if (OnDinh)
{
//==============Tnh thi gian h thng bt u n nh
int k = 999;
do
{
Tod = k * T; // Thi gian n nh
--k;
x_Tod = k;
}
while ((Math.Abs((y[k] - y[999]) / y[999]) <= 0.05) && (k >= x_ymax));
k = 999;
do
{ --k; }
while (Math.Abs((y[k] - (1 / K2)) / (1 / K2)) <= 0.01);

yod = 0;
for (int i = k; i < 1000; i++)
{
yod += y[i];
}
yod = yod / (999 - k + 1);
xicma = Math.Abs(ymax - yod) * 100 / yod;
Tmax = x_ymax * T;
// a ra cc thng s u ra
textBoxHT.Text = "n nh";
textBoxTod.Text = Tod.ToString();
textBoxYod.Text = yod.ToString();
textBoxTmax.Text = Tmax.ToString();
textBoxYmax.Text = ymax.ToString();
textBoxxicma.Text = xicma.ToString();
// V ging ta ca ymax
float toa_do_x = delta_x;
float toa_do_y = y0 - ymax * unit_y - delta_y;
do
{
graph.DrawLine(blackPen, toa_do_x, y0 - ymax * unit_y - delta_y, toa_do_x + 3, y0 ymax * unit_y - delta_y); //giong ngang
toa_do_x += 6;
} while (toa_do_x <= delta_x + x_ymax * T * unit_x);
do{
graph.DrawLine(blackPen, delta_x + x_ymax * T * unit_x, toa_do_y, delta_x + x_ymax
* T * unit_x, toa_do_y +3); //doc
toa_do_y +=6;
} while (toa_do_y <= y0 - ymax * unit_y - delta_y);
graph.DrawString("Ymax=" + ymax.ToString(), f, b, delta_x + x_ymax * T * unit_x, y0 ymax * unit_y - 2 * delta_y);
// V ging yod
toa_do_x = delta_x;
do
{
graph.DrawLine(blackPen, toa_do_x, y0 - delta_y - yod * unit_y, toa_do_x +3, y0 delta_y - yod * unit_y);
graph.DrawLine(blackPen, toa_do_x, y0 - delta_y - yod * unit_y * 1.05F, toa_do_x + 3,
y0 - delta_y - yod * unit_y * 1.05F);
graph.DrawLine(blackPen, toa_do_x, y0 - delta_y - yod * unit_y * 0.95F, toa_do_x + 3,
y0 - delta_y - yod * unit_y * 0.95F);

toa_do_x += 6;
} while (toa_do_x <= (xMax - 2 * delta_x));
graph.DrawString("Yod=" + yod.ToString(), f, b, xMax - 7 * delta_x, y0 - delta_y - yod *
unit_y * 1.1F);
//V ging Tod
toa_do_y = y0 - delta_y - y[x_Tod] * unit_y;
do
{
graph.DrawLine(blackPen, delta_x + x_Tod * T * unit_x, toa_do_y, delta_x + x_Tod *
T * unit_x, toa_do_y + 3);
toa_do_y += 6;
} while (toa_do_y <= (y0-delta_y));
graph.DrawString("Tod=" + x_Tod.ToString(), f, b, delta_x + x_Tod * T * unit_x + 10, y0
- 2*delta_y);
chi_so = ymax / 5;
int _tmp = (int)(chi_so * Math.Pow(10, 1));
chi_so = (float)(_tmp / Math.Pow(10, 1));
float tmp2 = chi_so;
for (int i = 0; i <= 15; i++)
if ((chi_so>0) && (chi_so<ymax))
{
graph.DrawLine(blackPen, delta_x - 3, yMax - chi_so * unit_y - delta_y, delta_x + 3,
yMax - chi_so * unit_y - delta_y);
_tmp = (int)(chi_so * Math.Pow(10, 1));
chi_so = (float)(_tmp / Math.Pow(10, 1));
graph.DrawString(chi_so.ToString(), f, b, delta_x - 30, yMax - chi_so * unit_y - delta_y10);
chi_so += tmp2;
}
} else
{ textBoxTod.Text = "N/A";
textBoxHT.Text = "Khng n nh";
textBoxYod.Text = "N/A";
textBoxTmax.Text = "N/A";
textBoxYmax.Text = "N/A";
textBoxxicma.Text = "N/A";
}
}
}
else
MessageBox.Show("Xin ch nhp d liu dng s!");
}
//============================================//
private void VeTrucToaDo()
{ Pen p = new Pen(Color.Black, 2);

graph.DrawLine(p, delta_x, y0 - delta_y, xMax - 2, y0 - delta_y);//truc hoanh


graph.DrawLine(p, delta_x, 4, delta_x, yMax - delta_y);//truc tung
MuiTen(graph, new Pen(Color.Black), new PointF((float)(xMax - 2.5), (float)(y0 - delta_y)), new
PointF((float)(xMax - 2), (float)(y0 - delta_x))); //mui ten Ox
MuiTen(graph, new Pen(Color.Black), new PointF((float)delta_y, (float)0), new
PointF((float)delta_y, (float)(-2.5))); //mui ten Oy
VeOXY();
}
//============================================//
private void VeOXY()
{ Font f = new Font("Tahoma", 12);
Brush b = new SolidBrush(Color.Black);
graph.DrawString("O", f, b, x0, y0 - delta_y);
graph.DrawString("T", f, b, xMax - 20, y0 - 18);
graph.DrawString("Y", f, b, x0 + 3, 5);
}
//============================================//
public static void MuiTen(Graphics graphics, Pen p, PointF diemDau, PointF diemCuoi)
{ Pen myPen = new Pen(Color.Black);
myPen.Width = 5;
myPen.EndCap = LineCap.ArrowAnchor;
PointF endPoint = new PointF((diemDau.X + diemCuoi.X) / 2, (diemDau.Y + diemCuoi.Y) / 2);
graphics.DrawLine(myPen, diemDau, endPoint);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{ // a ch s ca y ra comboBox
comboBox_item.Text = ((listBox1.SelectedIndex) * 10).ToString();
}
private void comboBox_item_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.SelectedIndex =int.Parse(comboBox_item.Text ) /10;
}
}
}// END OF PROGRAM

Giao din chnh ca chng trnh:

3. In kt qu ng cong qu
ng vi b thng s:

Ta c ng cong qu nh sau:

10

Trc x th hin gi tr T (1000 ln chu k trich mu).


Trc y th hin gi tr ca y(t) theo T.

11

4. So snh kt qu vi phn mm Matlab.


S simulink:

ng cong qu thu c qua Matlab

Nhn xt:
th thu c bng chng trnh m phng c dng ging vi th m
phng bi Matlab. Nh vy, nu b qua sai s khng th trnh khi trong qu trnh
m phng, chng ta c th kim chng c phng php tm phng trnh sai phn
ca h thng, v tnh ton ca chng trnh m phng l ng.

12

5. In kt qu y[k]
In ra mn hnh kt qu y[k], cch 10 s in 1 ln
S dng listbox a ra 100 gi tr, c 10 gi tr th add phn t vo listbox.
comboBox cha ch s k ca y[k] trong listbox, listbox v comboBox tham chiu
n nhau.
V d: y y[160] = 0.667

6. Cc ch tiu cht lng ca h thng


Cc ch tiu cht lng ca h thng c tnh ton t ng trong giap din
chnh ca chng trnh, ph thuc vo b tham s u vo.

13

Nu h thng n nh, textbox H thng s a ra thng bo n nh, ng


thi cc ch tiu cht lng c a ra cc tng ng.
Nu h thng khng n nh, textbox H thng s a ra thng bo khng
n nh, cc ch tiu a ra thng bo N/A
(Tnh n nh ca h thng c nh gi theo tiu chun Hurwitz)
V d: Vi b thng s u vo nh phn trn, h thng l n nh vi cc ch tiu

nh sau:

7. Nhn xt
Vi b thng s T1, T2, K1, K2, K3, T ph hp, h thng s n nh. Cn chn thi
gian trch mu T nh hn hoc bng 1/10 min(T1, T2). Cc thng s trn u phi
khc 0.
ng cong xut pht t gc ta , vt ln ti gi tr Ymax ti thi im Tmax,
dao ng xung quanh gi tr Yod. Gi tr vt qu ti a so vi Yod l h (%).
n thi gian Tod, h n nh, ch dao ng trong phm vi 5% xung quanh gi
tr Yod.

8. Ti liu tham kho


[1] Nguyn Cng Hin, Nguyn Phm Thc Anh, M hnh ha h thng v m
phng, NXB Khoa hc v K thut, 2006
[2] http://msdn.microsoft.com/ , C# Tutorials, truy nhp ln cui 10/04/2014
[3] http://congdongcviet.com/ , din n C#, truy nhp ln cui 10/04/2014

14

You might also like