Читайте также:
|
|
Imports System.Math
Imports System.Drawing, System.Drawing.Text
Public Class Form1
Inherits System.Windows.Forms.Form
Dim a As Single, b As Single, id As Boolean
Dim eps As Single
Dim nf As Integer
Private Sub btnВычислить_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnВычислить.Click
Dim xw As Single, it As Integer
Dim Flag As Boolean
lblРезультаты.Visible = False
lblГрафик.Visible = False
lblИтерации.Visible = False
lblКорень.Visible = False
id = False
Try
a = CSng(txta.Text)
Catch
MsgBox("Введенное значение a не число? Поправьте!")
Exit Sub
End Try
Try
b = CSng(txtb.Text)
Catch
MsgBox("Введенное значение b не число? Поправьте!")
Exit Sub
End Try
Try
eps = CSng(txteps.Text)
Catch
MsgBox("Введенное значение eps не число? Поправьте!")
Exit Sub
End Try
' Проверка корректности данных
If a >= b Then
MsgBox("Нарушено условие a < b? Поправьте!")
Exit Sub
End If
If eps <= 0 Then
MsgBox("Допустимая ошибка eps <= 0? Поправьте!")
Exit Sub
End If
id = True
' Вычисление корня
koren(100, a, b, eps, xw, it, Flag)
' Вывод результатов вычислений
If Flag Then
lblКорень.ForeColor = Color.Red
lblКорень.Text = "Решение не получено!"
lblКорень.Visible = True
'Запись в файл
PrintLine(nf, "Решение не получено!")
Else: lblИтерации.Text = "Решение получено! Выполнено " & it.ToString & " итераций"
lblКорень.ForeColor = Color.Black
lblКорень.Text = "Значение корня = " & Format(xw, "0.#######")
lblКорень.Visible = True
lblИтерации.Visible = True
lblГрафик.Visible = True
'Запись в файл
PrintLine(nf, a, b, eps, it, xw)
End If
pbxГрафик.Invalidate()
End Sub
Private Sub btnВыход_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnВыход.Click
FileClose(nf)
End
End Sub
Private Function f1(ByVal x As Single) As Single
f1 = 1/(3+Sin3,6x) - x
End Function
Private Function f2(ByVal x As Single) As Single
f2 = 1/(3+Sin3,6x)
End Function
Private Sub koren(ByVal pred As Integer, ByVal a As Single, ByVal b As Single, ByVal eps As Single, ByRef xw As Single, ByRef it As Integer, ByRef Flag As Boolean)
Dim xn1 As Single, xs As Single
Dim fxn1 As Single, fxn2 As Single
Dim d As Single, Bool As Boolean
xn1 = (a + b) / 2
it = 0
fxn1 = f1(xn1)
fxn2 = f2(xn1)
Do
xs = f2 ( xn1 )
it = it + 1
d = xs - xn1
xn1 = xs
Bool = Abs(d) < eps Or it >= pred
Loop Until Bool
If it <= pred Then
Flag = False
xw = xs
Else
Flag = True
End If
End Sub
Private Sub pbxГрафик_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbxГрафик.Paint
Dim x, ab, dx As Single
Dim i, n As Integer
n = 10
ab = b - a
dx = ab / n
If id Then
Dim mx As Single
Dim sx, sy, rx, ry, h, w As Integer
h = pbxГрафик.Height
w = pbxГрафик.Width
mx = 0.9 * w / ab
sx = w * (0.05 - 0.9 * a / ab)
sy = h / 2
Dim G As Graphics
G = e.Graphics
G.TranslateTransform(sx, sy)
G.ScaleTransform(mx, mx)
Dim P1, P2 As Pen
P1 = New Pen(Color.Black, 1 / mx)
P2 = New Pen(Color.Red, 1 / mx)
Dim x1, x2, y1, y2 As Single
x1 = a - 0.02 * ab
y1 = 0
x2 = b + 0.02 * ab
y2 = 0
G.DrawLine(P1, x1, y1, x2, y2)
x1 = 0
y1 = -h / 2 + 0.01 * mx
x2 = 0
y2 = h / 2 - 0.01 * mx
G.DrawLine(P1, x1, y1, x2, y2)
x1 = a
y1 = f1(x1)
x2 = x1 + dx
y2 = f1(x2)
For i = 1 To n
G.DrawLine(P2, x1, y1, x2, y2)
x1 = x2
y1 = y2
x2 = x2 + dx
y2 = f1(x2)
Next
Dim ШагШкалы As Single
ШагШкалы = CInt(ab) / 10
For x = CInt(a * 10) / 10 To b + ШагШкалы / 2 Step ШагШкалы
G.DrawLine(P1, x, -CSng(ab / 100), x, CSng(ab / 100))
Dim fnt As New Font("Times New Roman", 7 / mx, FontStyle.Regular)
Dim Br As New SolidBrush(Color.Black)
G.DrawString(Format(x, "0.0"), fnt, Br, x, ab / 100)
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
nf = FreeFile()
FileOpen(nf, "D:\Аляутдинов\frz.txt", OpenMode.Output)
PrintLine(nf, "a", "b", "Доп. ошибка", "Вып. итераций", "Знач. корня")
FileClose(nf)
nf = FreeFile()
FileOpen(nf, "D:\Аляутдинов\frz.txt", OpenMode.Append)
End Sub
Дата добавления: 2015-08-02; просмотров: 39 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Блок-схема алгоритма подзадачи вычисления корня | | | ВВЕДЕНИЕ |