Mustaqil ishi mavzu: Gauss usuli yordamida tenglamalar sistemasini yechish algoritmi Tekshirdi
Download 27.94 Kb.
|
1 2
Bog'liqGauss usuli
Yechish. Birinchi tenglamani ketma β ket 1,3,2 sonlarga ko`paytirib, so`ngra ikkinchi, uchinchi va to`rtinchi tenglamalardan birinchi tenglamani ayirsak,
π₯1 + 2π₯2 β 3π₯3 + 5π₯4 = 1 { π₯2 β 10π₯3 + 17π₯4 = β2 βπ₯2 + 10π₯3 β 17π₯4 = 2 βπ₯2 + 10π₯3 β 17π₯4 = 2 sistemaga ega bo`lamiz. Endi ikkinchi tenglamani uchinchi va to`rtinchi tenglamalarga qo`shib, natijada quyidagi sistemani hosil qilamiz: π₯1 + 2π₯2 β 3π₯3 + 5π₯4 = 1 { π₯2 β 10π₯3 + 17π₯4 = β2 0 = 0 0 = 0 Oxirgi ikkita tenglama 0 β π₯1 + 0 β π₯2 + 0 β π₯3 + 0 β π₯4 = 0 ko`rinishdagi tenglama bo`lib, u nomaβlumning har qanday qiymatida ham o`rinli bo`lgani uchun uni tashlab yuboramiz. Ikkinchi tenglamani qanoatlantiradigan noma`lumning qiymatini topsh uchun π₯3 va π₯4 larga ixtiyoriy qiymatlarni beramiz. Masalan, π₯3 = πΌ, π₯4 = π½ bo`lsin, u holda π₯2 = 10πΌ β 17π½ β 2 bo`ladi. Bu π₯2, π₯3, π₯4 larning qiymatlarini birinchi tenglamaga qo`yib π₯1 = β17πΌ + 29π½ + 5 ni topamiz. Sistemaning yechimi π₯1 = β17πΌ + 29π½ + 5; π₯2 = 10πΌ β 17π½ β 2; π₯3 = πΌ; π₯4 = π½ bo`lib πΌ va π½ ning ixtiyoriy qiymatlarida berilgan sistemaning hamma yechimlarini beradi. 6 β misol. Quyidagi chiziqli tenglamalar sistemasini Gauss usuli bilan yeching: π₯1 + π₯2 β 3π₯3 = 7 {2π₯1 + 3π₯2 + π₯3 = 1 2π₯1 β π₯2 + 2π₯3 = 6 Yechish. Birinchi tenglamaning barcha hadlarini 2 ga ko`paytirib, undan ikkinchi va uchinchi tenglamalarni ayiramiz. Natijada quyidagi ko`rinishdagi sistemaga ega bo`lamiz: π₯1 + π₯2 β 3π₯3 = 7 { βπ₯2 β 7π₯3 = 13 3π₯2 β 8π₯3 = 8 Ikkinchi va uchinchi tenglamalar faqat π₯2 va π₯3 nomalumlarga ega. Ikkinchi tenglamaning hadlarini 3 ga ko`paytirib, uchinchi tenglamaga qo`shamiz. Natijada quyidagi sistema hosil bo`ladi: π₯1 + π₯2 β 3π₯3 = 7 { βπ₯2 β 7π₯3 = 13 βπ₯3 = 13 Uchinchi tenglamadan: π₯3 = β13, buni ikkinchi tenglamaga qo`yib π₯2 noma`lumni topamiz: βπ₯2 β 7 β (β13) = 13, π₯2 = 78 π₯3 va π₯2 nomaβlumlarning qiymatlarini birinchi tenglamaga qo`yib π₯1 nomaβlumni topamiz. π₯1 + 78 β 3 β (β13) = 7, π₯1 = β110 Javob: (-110, 78, -13) Gauss usulining C# dasturlash tizimida koβrinishi using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Equations_Solver { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { textBox2.Clear(); double[][] rows = new double[textBox1.Lines.Length][]; for (int i = 0; i < rows.Length; i++) { rows[i] = (double[])Array.ConvertAll(textBox1.Lines[i].Split(' '), double.Parse); } int length = rows[0].Length; for (int i = 0; i < rows.Length - 1; i++) { for (int j = i; j < rows.Length; j++) { double[] d = new double[length]; for (int x = 0; x < length; x++) { if (i == j && rows[j][i] == 0) { bool changed = false; for (int z = rows.Length - 1; z > i; z--) { if (rows[z][i] != 0) { double[] temp = new double[length]; temp = rows[z]; rows[z] = rows[j]; rows[j] = temp; changed = true; } } if (!changed) { textBox2.Text += "No Solution\r\n"; return; } } if (rows[j][i] != 0) { d[x] = rows[j][x] / rows[j][i]; } else { d[x] = rows[j][x]; } } rows[j] = d; } for (int y = i + 1; y < rows.Length; y++) { double[] f = new double[length]; for (int g = 0; g < length; g++) { if (rows[y][i] != 0) { f[g] = rows[y][g] - rows[i][g]; } else { f[g] = rows[y][g]; } } rows[y] = f; } } double val = 0; int k = length - 2; double[] result = new double[rows.Length]; for (int i = rows.Length - 1; i >= 0; i--) { val = rows[i][length - 1]; for (int x = length - 2; x > k; x--) { val -= rows[i][x] * result[x]; } result[i] = val / rows[i][i]; if (result[i].ToString() == "NaN" || result[i].ToString().Contains("Infinity")) { textBox2.Text += "No Solution Found!\n"; return; } k--; } for (int i = 0; i < result.Length; i++) { textBox2.Text += string.Format("X{0} = {1}\r\n", i + 1, Math.Round(result[i], 10)); } } } } Download 27.94 Kb. Do'stlaringiz bilan baham: |
1 2
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling