0% found this document useful (0 votes)
116 views

Visual Basic 6.0: Codes of Optimization Laboratory

The document discusses several numerical methods for solving non-linear equations and simultaneous linear equations, including: 1) Methods for solving non-linear equations such as bisection, false position, Newton-Raphson, and secant. 2) Methods for solving simultaneous linear equations such as Gaussian elimination, Gauss-Jordan, and Grammer's rule. 3) Iterative methods for simultaneous linear equations including Jacobi, Gauss-Seidel, and SOR. Visual Basic code examples are provided for each method.

Uploaded by

Ahmed Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Visual Basic 6.0: Codes of Optimization Laboratory

The document discusses several numerical methods for solving non-linear equations and simultaneous linear equations, including: 1) Methods for solving non-linear equations such as bisection, false position, Newton-Raphson, and secant. 2) Methods for solving simultaneous linear equations such as Gaussian elimination, Gauss-Jordan, and Grammer's rule. 3) Iterative methods for simultaneous linear equations including Jacobi, Gauss-Seidel, and SOR. Visual Basic code examples are provided for each method.

Uploaded by

Ahmed Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Visual Basic

6.0
Codes of Optimization
Laboratory
‫االعداد‬
‫علي هحود هاشن‬.‫م‬

‫ العل محع هاش‬: ‫داالعدا‬

Non-linear
equations

2
‫ العل محع هاش‬: ‫داالعدا‬
1- Bi-section Method :
xl = Val(Text1.Text)

xu = Val(Text2.Text)

f1 = (xl ^ 3) - 20

f2 = (xu ^ 3) - 20

xm = (xl + xu) / 2

10 f3 = (xm ^ 3) - 20

If (f3 * f1) > 0 Then

xl = xm

Else

xu = xm

End If

xm1 = (xl + xu) / 2

If Abs(xm1 - xm) < 0.00001 Then

Print "xm="; xm

Else

xm = xm1

GoTo 10

End If

f4 = (xm ^ 3) - 20

Ea = Abs((xm - xm1) / xm)

Print "f4="; f4

Print "Ea="; Ea

3
‫ العل محع هاش‬: ‫داالعدا‬
2- False position Method :
Private Sub Command1_Click()

xl = Val(Text1.Text)

xu = Val(Text2.Text)

f1 = (xl ^ 3) - 20

f2 = (xu ^ 3) - 20

xr = ((xu*f1)-(xl*f2))/(f1-f2)

10 f3 = (xr ^ 3) - 20

If (f3 * f1) > 0 Then

xl = xr

Else

xu = xr

End If

xr1 =((xu*f1)-(xl*f2))/(f1-f2)

If Abs(xr1 - xr) < 0.00001 Then

Print "xr="; xr

Else

xr = xr1

GoTo 10

End If

f4 = (xr ^ 3) - 20

Ea = Abs((xr- xr1) / xr)

Print "f4="; f4

Print "Ea="; Ea
4
‫ العل محع هاش‬: ‫داالعدا‬
3- Newton Raphson Method :
Private Sub Command1_Click()

x = Val(Text1.Text)

10 f1 = (x ^ 3) - 20

f2 = 3 * (x ^ 2)

X1 = x - (f1 / f2)

If Abs(X1 - x) < 0.001 Then

Print " x1="; X1

Else

x = X1

GoTo 10

End If

f2 = (X1 ^ 3) - 20

Print "f2="; f2

End sub

5
‫ العل محع هاش‬: ‫داالعدا‬
4- Secant method Method :
Private Sub Command1_Click()

x0 = Val(Text1.Text)

x1 = Val(Text2.Text)

10 f1 = (x0 ^ 3) - 20

f2 = (x1 ^ 3) - 20

X2 = x1 - ((f2 * (x1 - x0)) / (f2 - f1))

If Abs(X2 - x1) < 0.000000001 Then

Print " x2="; X2

Else

x1 = X2

GoTo 10

End If

f3 = (X2 ^ 3) - 20

Print "f3="; f3

End Sub

6
‫ العل محع هاش‬: ‫داالعدا‬

Simultanous
linear
equations

7
‫ العل محع هاش‬: ‫داالعدا‬
A-Derict Methods :
1- Naẗve Guass elimination Method :
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

k = -a21 / a11

a21 = a21 + (k * a11)

a22 = a22 + (k * a12)

a23 = a23 + (k * a13)

b2 = b2 + (k * b1)

k1 = -a31 / a11

a31 = a31 + (k1 * a11)

a32 = a32 + (k1 * a12)

8
‫ العل محع هاش‬: ‫داالعدا‬
a33 = a33 + (k1 * a13)

b3 = b3 + (k1 * b1)

k2 = -a32 / a22

a31 = a31 + (k2 * a21)

a32 = a32 + (k2 * a22)

a33 = a33 + (k2 * a23)

b3 = b3 + (k2 * b2)

x3 = b3 / a33

X2 = (b2 - (a23 * x3)) / a22

X1 = (b1 - (a12 * X2) - (a13 * x3)) / a11

Print "x3="; x3

Print "x2="; X2

Print "x1="; X1

End Sub

9
‫ العل محع هاش‬: ‫داالعدا‬
2- Guass - Jordon Method :
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

k = -a21 / a11

a21 = a21 + (k * a11)

a22 = a22 + (k * a12)

a23 = a23 + (k * a13)

b2 = b2 + (k * b1)

k1 = -a31 / a11

a31 = a31 + (k1 * a11)

a32 = a32 + (k1 * a12)

a33 = a33 + (k1 * a13)

b3 = b3 + (k1 * b1)
10
‫ العل محع هاش‬: ‫داالعدا‬
k2 = -a32 / a22

a31 = a31 + (k2 * a21)

a32 = a32 + (k2 * a22)

a33 = a33 + (k2 * a23)

b3 = b3 + (k2 * b2)

k3 = -a12 / a22

a11 = a11 + (k3 * a21)

a12 = a12 + (k3 * a22)

a13 = a13 + (k3 * a23)

b1 = b1 + (k3 * b2)

k4 = -a13 / a33

a11 = a11 + (k4 * a31)

a12 = a12 + (k4 * a32)

a13 = a13 + (k4 * a33)

b1 = b1 + (k4 * b3)

k5 = -a23 / a33

a21 = a21 + (k5 * a31)

a22 = a22 + (k5 * a32)

a23 = a23 + (k5 * a33)

b2 = b2 + (k5 * b3)

x3 = b3 / a33

X2 = (b2 - (a23 * x3)) / a22

X1 = (b1 - (a12 * X2) - (a13 * x3)) / a11

Print "x3="; x3

11
‫ العل محع هاش‬: ‫داالعدا‬
Print "x2="; X2

Print "x1="; X1

End Sub

12
‫ العل محع هاش‬: ‫داالعدا‬
3- Grammer’s rule
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

D = (a11 *((a22 * a33) - (a23 * a32))) -(a12*((a21 * a33) - (a23 * a31))) +(a13 *((a21 * a32) -(a22 * a31)))

a1 = (b1 * ((a22 * a33) - (a23 * a32))) - (a12 * ((b2 * a33) - (a23 * b3))) + (a13 * ((b2 * a32) - (a22 * b3)))

a2 = (a11 * ((b2 * a33) - (a23 * b3))) - (b1 * ((a21 * a33) - (a23 * a31))) + (a13 * ((a21 * b3) - (b2 * a31)))

a3 = (a11 * ((a22 * b3) - (b2 * a32))) - (a12 * ((a21 * b3) - (b2 * a31))) + (b1 * ((a21 * a32) - (a22 * a31)))

X1 = a1 / D

X2 = a2 / D

x3 = a3 / D

Print "x3="; x3

Print "x2="; X2

Print "x1="; X1

13
‫ العل محع هاش‬: ‫داالعدا‬
B- Iterative Methods
1- Jacobi method :
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

X1 = Val(Text12.Text)

X2 = Val(Text12.Text)

x3 = Val(Text12.Text)

X1 = 1

X2 = 2

x3 = 5

10 x4 = (b1 - (a12 * X2) - (a13 * x3)) / a11

x5 = (b2 - (a21 * X1) - (a23 * x3)) / a22

14
‫ العل محع هاش‬: ‫داالعدا‬
x6 = (b3 - (a31 * X1) - (a32 * X2)) / a33

If (x4 - X1) > 0.00001 And (x5 - X2) > 0.00001 And (x6 - x3) > 0.00001 Then

X1 = x4

X2 = x5

x3 = x6

GoTo 10

Else

Print "x4="; x4

Print "x5="; x5

Print "x6="; x6

End If

End Sub

15
‫ العل محع هاش‬: ‫داالعدا‬
2- Gauss- seidel Method :
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

X1 = Val(Text12.Text)

X2 = Val(Text12.Text)

x3 = Val(Text12.Text)

X1 = 1

X2 = 2

x3 = 5

10 x4 = (b1 - (a12 * X2) - (a13 * x3)) / a11

x5 = (b2 - (a21 * x4) - (a23 * x3)) / a22

x6 = (b3 - (a31 * X4) - (a32 * X5)) / a33

If (x4 - X1) > 0.00001 And (x5 - X2) > 0.00001 And (x6 - x3) > 0.00001 Then
16
‫ العل محع هاش‬: ‫داالعدا‬
X1 = x4

X2 = x5

x3 = x6

GoTo 10

Else

Print "x4="; x4

Print "x5="; x5

Print "x6="; x6

End If

End Sub

17
‫ العل محع هاش‬: ‫داالعدا‬
3- Sor Method :
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

X1 = Val(Text12.Text)

X2 = Val(Text12.Text)

x3 = Val(Text12.Text)

w = 1.2

X1 = 1

X2 = 2

x3 = 5

10 x4 = X1 + ((1.2 / 12) * (1 - (a11 * X1) - (a12* X2) + (a13 * x3)))

x5 = X2 + ((1.2 / 28) * (a21 - x4 - (a22 * X2) - (a23* x3)))

x6 = x3 + ((1.2 / 76) * (76 - ( a31* 4) - (a32 * x5) - (33 * x3)))


18
‫ العل محع هاش‬: ‫داالعدا‬
If Abs(x4 - X1) > 0.00001 And Abs(x5 - X2) > 0.00001 And Abs(x6 - x3) Then

X1 = x4

X2 = x5

x3 = x6

GoTo 10

Else

Print "x4="; x4

Print "x5="; x5

Print "x6="; x6

End If

End Sub

19
‫داالعدا ‪ :‬العل محع هاش‬

‫‪Interpolation‬‬

‫‪20‬‬
‫ العل محع هاش‬: ‫داالعدا‬
A-Derict Method :
1- First order ]y = a0 + a1x [
2- Second order ] y = a0 + a1x + a2x2 [
Private Sub Command1_Click()

a11 = Val(Text1.Text)

a12 = Val(Text2.Text)

a13 = Val(Text3.Text)

b1 = Val(Text4.Text)

a21 = Val(Text5.Text)

a22 = Val(Text6.Text)

a23 = Val(Text7.Text)

b2 = Val(Text8.Text)

a31 = Val(Text9.Text)

a32 = Val(Text10.Text)

a33 = Val(Text11.Text)

b3 = Val(Text12.Text)

x = Val(Text13.Text)

n = Val(Text14.Text)
If n = 1 Then
k = -a31 / a21
a31 = a31 + (k * a21)
a32 = a32 + (k * a22)
b3 = b3 + (k * b2)
a1 = b3 / a32
a0 = (b2 - (a1 * a22)) / a21
y = a0 + (a1 * x)
Print "a0=", a0
Print "a1=", a1
Print "y=", y

21
‫ العل محع هاش‬: ‫داالعدا‬
Else
k = -a21 / a11
a21 = a21 + (k * a11)
a22 = a22 + (k * a12)
a23 = a23 + (k * a13)
b2 = b2 + (k * b1)
k1 = -a31 / a11
a31 = a31 + (k1 * a11)
a32 = a32 + (k1 * a12)
a33 = a33 + (k1 * a13)
b3 = b3 + (k1 * b1)
k2 = -a32 / a22
a31 = a31 + (k2 * a21)
a32 = a32 + (k2 * a22)
a33 = a33 + (k2 * a23)
b3 = b3 + (k2 * b2)
a2 = b3 / a33
a1 = (b2 - (a23 * a2)) / a22
a0 = (b1 - (a12 * a1) - (a13 * a2)) / a11
y = a0 + (a1 * x) + (a2 * x ^ 2)
Print "a0="; a0
Print "a1="; a1
Print "a2="; a2
Print "y=", y
End If
End Sub

22
‫ العل محع هاش‬: ‫داالعدا‬
B- Newton’s divided difference polynomial :
1- Linear Interpolation ,first order ( y = a0 + a1(x-x0) )
2- Quadradic Interpolation ,second order (y = a0 + a1(x-x0) + a2(x-x0)(x-x1))
3- Cubic Interpolation , third order (y= a0+a1(x-x0) +a2(x-x0)(x-x1)+a3(x-x0)(x-x1)(x-x2))
Private Sub Command1_Click()

X0 = Val(Text1.Text)
X1 = Val(Text2.Text)
X2 = Val(Text3.Text)
X3 = Val(Text4.Text)
Y0 = Val(Text5.Text)
Y1 = Val(Text6.Text)
Y2 = Val(Text7.Text)
Y3 = Val(Text8.Text)
n = Val(Text9.Text)
x = Val(Text10.Text)
If n = 1 Then
a0 = Y0
a1 = (Y1 - Y0) / (X1 - X0)
y = a0 + (a1 * (x - X0))
Print "a0=", a0
Print "a1=", a1
Print "y=", y
End If
If n = 2 Then
a0 = Y0
a1 = (Y1 - Y0) / (X1 - X0)
k = (Y2 - Y1) / (X2 - X1)
a2 = (k - a1) / (X2 - X0)
y = a0 + (a1 * (x - X0)) + (a2 * (x - X0) * (x - X1))
Print "a0=", a0
Print "a1=", a1
Print "a2=", a2
Print "y=", y
End If
If n = 3 Then
a0 = Y0
a1 = (Y1 - Y0) / (X1 - X0)

23
‫ العل محع هاش‬: ‫داالعدا‬
k = (Y2 - Y1) / (X2 - X1)
a2 = (k - a1) / (X2 - X0)
k1 = (y3 - Y2) / (x3 - X2)
a3 = ((k1 - k) - (k - a1)) / (x3 - X0)
y = a0 + (a1 *(x - X0))+(a2 * (x - X0) * (x - X1))+(a3 * (x - X0)*(x - X1) * (x - X2))
Print "a0=", a0
Print "a1=", a1
Print "a2=", a2
Print "a3=", a3
Print "y=", y
End If
End Sub

24
‫ العل محع هاش‬: ‫داالعدا‬
C- Lagrangian Interpolation
1- Linear Interpolation ,first order ( y =l0*y0 + l1*y1 )
2- Quadradic Interpolation ,second order ( y = l0*y0 + l1*y1 + l2*y2 )
3- Cubic Interpolation , third order ( y = l0*y0 + l1*y1 + l2*y2 + l3*y3 )
Private Sub Command1_Click()

X0 = Val(Text1.Text)
X1 = Val(Text2.Text)
X2 = Val(Text3.Text)
X3 = Val(Text4.Text)
Y0 = Val(Text5.Text)
Y1 = Val(Text6.Text)
Y2 = Val(Text7.Text)
Y3 = Val(Text8.Text)
n = Val(Text9.Text)
x = Val(Text10.Text)
If n = 1 Then
l0 = (x - X1) / (x0 - X1)
l1 = (x - x0) / (X1 - x0)
y = l0 * y0 + l1 * Y1
Print "l0=", l0
Print "l1=", l1
Print "y=", y
End If
If n = 2 Then
l0 = ((x - X1) / (x0 - X1)) * ((x - X2) / (x0 - X2))
l1 = ((x - x0) / (X1 - x0)) * ((x - X2) / (X1 - X2))
l2 = ((x - x0) / (X2 - x0)) * ((x - X1) / (X2 - X1))
y = l0 * y0 + l1 * Y1 + l2 * Y2
Print "10=", 10
Print "l1=", l1
Print "l2=", l2
Print "y=", y
End If
If n = 3 Then
l0 = ((x - X1) / (x0 - X1)) * ((x - X2) / (x0 - X2)) * ((x - X3) / (x0 - X3))
l1 = ((x - x0) / (X1 - x0)) * ((x - X2) / (X1 - X2)) * ((x - X3) / (X1 - X3))
l2 = ((x - x0) / (X2 - x0)) * ((x - X1) / (X2 - X1)) * ((x - X3) / (X2 - X3))

25
‫ العل محع هاش‬: ‫داالعدا‬
l3 = ((x - x0) / (X3 - x0)) * ((x - X1) / (X3 - X1)) * ((x - X2) / (X3 - X2))
y = l0 * y0 + l1 * Y1 + l2 * Y2 + l3 * y3
Print "l0=", l0
Print "l1=", l1
Print "l2=", l2
Print "l3=", l3
Print "y=", y
End If
End Sub

26
‫داالعدا ‪ :‬العل محع هاش‬

‫‪Regression‬‬

‫‪27‬‬
‫ العل محع هاش‬: ‫داالعدا‬
1- Linear regression Deriction :
X1 = Val(Text1.Text)
X2 = Val(Text2.Text)
x3 = Val(Text3.Text)
x4 = Val(Text4.Text)
Y1 = Val(Text5.Text)
Y2 = Val(Text6.Text)
y3 = Val(Text7.Text)
y4 = Val(Text8.Text)
n = Val(Text9.Text)
X= Val(Text10.Text)
L = (X1 * Y1) + (X2 * Y2) + (x3 * y3) + (x4 * y4)
L1 = X1 + X2 + x3 + x4
L2 = Y1 + Y2 + y3 + y4
L3 = (X1 ^ 2) + (X2 ^ 2) + (x3 ^ 2) + (x4 ^ 2)
a1 = ((n * L) - (L1 * L2)) / ((n * L3) - ((L1) ^ 2))
a0 = (L2 / n) - ((a1 * L1) / n)
Y = a0 + ( a1*X )
Print "a1=", a1
Print "a0=", a0
Print "Y=", Y

28
‫ العل محع هاش‬: ‫داالعدا‬
2- Polynomial Model :
Private Sub Command1_Click()
X1 = Val(Text1.Text)
X2 = Val(Text2.Text)
x3 = Val(Text3.Text)
x4 = Val(Text4.Text)
Y1 = Val(Text5.Text)
Y2 = Val(Text6.Text)
y3 = Val(Text7.Text)
y4 = Val(Text8.Text)
n = Val(Text9.Text)
l = Y1 + Y2 + y3 + y4
l1 = (X1 * Y1) + (X2 * Y2) + (x3 * y3) + (x4 * y4)
l2 = ((X1 ^ 2) * Y1) + ((X2 ^ 2) * Y2) + ((x3 ^ 2) * y3) + ((x4 ^ 2) * y4)
d = X1 + X2 + x3 + x4
d1 = (X1 ^ 2) + (X2 ^ 2) + (x3 ^ 2) + (x4 ^ 2)
d2 = (X1 ^ 3) + (X2 ^ 3) + (x3 ^ 3) + (x4 ^ 3)
d3 = (X1 ^ 4) + (X2 ^ 4) + (x3 ^ 4) + (x4 ^ 4)
k = -d / n
d = d + (k * n)
d1 = d1 + (k * d)
d2 = d2 + (k * d1)
l1 = l1 + (k * l)
k1 = -d1 / n
d1 = d1 + (k1 * n)
d2 = d2 + (k1 * d)
d3 = d3 + (k1 * d1)
l2 = l2 + (k1 * l)
k2 = -d2 / d1
d1 = d1 + (k2 * d)
d2 = d2 + (k2 * d1)
d3 = d3 + (k2 * d2)
l2 = l2 + (k2 * l1)
a2 = d3 / l2
a1 = (l1 - (a2 * d2)) / d1
a0 = (l - (a1 * d) - (a2 * d1)) / n
Print " a0 =", a0 ; " a1 =", a1 ; " a2 =", a2
End Sub

29
‫داالعدا ‪ :‬العل محع هاش‬

‫أ رتقد‬
‫فهاتفها‬
‫ُزاحش‬
‫‪30‬‬

You might also like