Guest User

Untitled

a guest
Sep 17th, 2019
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. using MathNet.Numerics.LinearAlgebra;
  12. using MathNet.Numerics.LinearAlgebra.Factorization;
  13.  
  14. namespace Pvz1
  15. {
  16. public partial class Form1 : Form
  17. {
  18. List<Timer> Timerlist = new List<Timer>();
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. Initialize();
  24. }
  25.  
  26. // ---------------------------------------------- PUSIAUKIRTOS METODAS ----------------------------------------------
  27.  
  28. double x1, x2, xtemp; // izoliacijos intervalo pradžia ir galas, vidurio taškas
  29. int N = 1000; // maksimalus iteracijų skaičius
  30. int iii; // iteracijos numeris
  31.  
  32. Series Fx, X1X2, XMid; // naudojama atvaizduoti f-jai, šaknų rėžiams ir vidiniams taškams
  33.  
  34.  
  35. /// <summary>
  36. /// Sprendžiama lygtis F(x) = 0
  37. /// </summary>
  38. /// <param name="x">funkcijos argumentas</param>
  39. /// <returns></returns>
  40. private double F(double x)
  41. {
  42. return (double)(0.25 * Math.Pow(x, 5) + 0.68 * Math.Pow(x, 4) - 1.65 * Math.Pow(x, 3) - 5.26 * Math.Pow(x, 2) - 1.91 * x + 1.36);
  43. //return (double)(Math.Pow(Math.E,(-x))*Math.Cos(x)*Math.Sin(Math.Pow(x,2)-1));
  44. }
  45.  
  46. private void drawChart()
  47. {
  48. ClearForm(); // išvalomi programos duomenys
  49. PreparareForm(-10, 10, -2, 2);
  50. x1 = -8.6; // izoliacijos intervalo pradžia
  51. x2 = 5.6; // izoliacijos intervalo galas
  52. iii = 0; // iteraciju skaičius
  53. richTextBox1.AppendText("Iteracija x F(x) x1 x2 F(x1) F(x2) \n");
  54. // Nubraižoma f-ja, kuriai ieskome saknies
  55. Fx = chart1.Series.Add("F(x)");
  56. Fx.ChartType = SeriesChartType.Line;
  57.  
  58. X1X2 = chart1.Series.Add("X1X2");
  59. X1X2.MarkerStyle = MarkerStyle.Circle;
  60. X1X2.MarkerSize = 8;
  61. X1X2.ChartType = SeriesChartType.Point;
  62.  
  63. X1X2.Points.AddXY(x1, 0);
  64. X1X2.Points.AddXY(x2, 0);
  65.  
  66. double x = -10;
  67. for (int i = 0; i < 200; i++)
  68. {
  69. Fx.Points.AddXY(x, F(x));
  70. x = x + 0.1;
  71. }
  72. Fx.BorderWidth = 3;
  73. }
  74.  
  75. // MYGTUKAS STYGU METODAS
  76. private void Button6_Click(object sender, EventArgs e)
  77. {
  78. ClearForm();
  79. richTextBox1.AppendText("hi");
  80. }
  81.  
  82. // MYGTUKAS SKENAVIMO METODAS
  83. private void button3_Click(object sender, EventArgs e)
  84. {
  85. drawChart();
  86.  
  87. List<Range> rangeList = scan(x1, x2, 0.1, false);
  88. richTextBox1.AppendText("1");
  89. rangeList.ForEach(range => stygu(range));
  90. }
  91.  
  92. public class Range
  93. {
  94. public double x1 { get; private set; }
  95. public double x2 { get; private set; }
  96.  
  97. public Range(double x1, double x2)
  98. {
  99. this.x1 = x1;
  100. this.x2 = x2;
  101. }
  102.  
  103. public double getMiddlePoint()
  104. {
  105. return (x1 + x2) / 2.0;
  106. }
  107. }
  108.  
  109. public List<Range> scan(double x1, double x2, double delta, bool tikslinimas)
  110. {
  111. double x = x1;
  112. double end = x1;
  113. List<Range> rangeList = new List<Range>();
  114. while (x2 >= x)
  115. {
  116. end = x + delta;
  117.  
  118. if (Math.Sign(F(x)) != Math.Sign(F(end)))
  119. {
  120. Range newRange = new Range(x, end);
  121. rangeList.Add(newRange);
  122.  
  123. if (tikslinimas)
  124. return rangeList;
  125. }
  126.  
  127. x = end;
  128. }
  129. return rangeList;
  130. }
  131.  
  132. public void skenavimo(Range range)
  133. {
  134. double tol = 1e-6;
  135. int i = 0;
  136. double step = 0.01;
  137. double x0 = range.x1;
  138. double x1 = range.x2;
  139.  
  140. while (Math.Abs(F(x0)) > tol)
  141. {
  142. richTextBox1.AppendText(String.Format(" {0,6:d} {1,12:f7} {2,12:f7} {3,12:f7} {4,12:f7} {5,12:f7} {6,12:f7}\n",
  143. i, x0, F(x0), x0, x1, F(x0), F(x1)));
  144. Range temp = scan(x0, x1, step, true)[0];
  145. x0 = temp.x1;
  146. x1 = temp.x2;
  147. step = step / 10;
  148. i++;
  149. }
  150. richTextBox1.AppendText("Skaiciavimas Baigtas");
  151. }
  152.  
  153. public void stygu(Range range)
  154. {
  155. double x0 = range.x1;
  156. double x1 = range.x2;
  157.  
  158. double k = Math.Abs(F(x0) / F(x1));
  159. double xmid = (x0 + k * x1) / (1.0 + k);
  160. double tol = 1e-8;
  161. int i = 0;
  162.  
  163. while (Math.Abs(F(xmid)) > tol)
  164. {
  165. richTextBox1.AppendText(String.Format(" {0,6:d} {1,12:f7} {2,12:f7} {3,12:f7} {4,12:f7} {5,12:f7} {6,12:f7}\n",
  166. i, xmid, F(xmid), x0, x1, F(x0), F(x1)));
  167. if (Math.Sign(F(x0)) == Math.Sign(F(xmid)))
  168. {
  169. x0 = xmid;
  170. }
  171. else
  172. {
  173. x1 = xmid;
  174. }
  175. k = Math.Abs(F(x0) / F(x1));
  176. xmid = (x0 + k * x1) / (1.0 + k);
  177. i++;
  178. }
  179. richTextBox1.AppendText("\nSkaiciavimas Baigtas\n");
  180. }
  181.  
  182.  
  183. /// <summary>
  184. /// timer2 iteracijoje atliekami veiksmai
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. private void timer2_Tick(object sender, EventArgs e)
  189. {
  190. xtemp = (x1 + x2) / 2; // apskaiciuojamas vidurinis taskas
  191.  
  192.  
  193. if (Math.Abs(F(xtemp)) > 1e-6 & iii <= N)
  194. // tikrinama salyga, ar funkcijos absoliuti reiksme daugiau uz nustatyta (norima)
  195. // tiksluma ir nevirsytas maksimalus iteraciju skaicius
  196. {
  197. X1X2.Points.Clear();
  198. XMid.Points.Clear();
  199.  
  200. X1X2.Points.AddXY(x1, 0);
  201. X1X2.Points.AddXY(x2, 0);
  202. XMid.Points.AddXY(xtemp, 0);
  203.  
  204. richTextBox1.AppendText(String.Format(" {0,6:d} {1,12:f7} {2,12:f7} {3,12:f7} {4,12:f7} {5,12:f7} {6,12:f7}\n",
  205. iii, xtemp, F(xtemp), x1, x2, F(x1), F(x2)));
  206. if (Math.Sign((double)F(x1)) != Math.Sign((double)F(xtemp)))
  207. {
  208. x2 = xtemp;
  209. }
  210. else
  211. {
  212. x1 = xtemp;
  213. }
  214. iii = iii + 1;
  215.  
  216. }
  217. else
  218. // skaiciavimai stabdomi
  219. {
  220. richTextBox1.AppendText("Skaičiavimai baigti");
  221. timer2.Stop();
  222. }
  223. }
  224.  
  225.  
  226. // ---------------------------------------------- PARAMETRINĖS FUNKCIJOS ----------------------------------------------
  227.  
  228. List<PointF> data = new List<PointF>();
  229. Series S1;
  230.  
  231. /// <summary>
  232. /// Parametrinis interpoliavimas
  233. /// </summary>
  234. private void button5_Click(object sender, EventArgs e)
  235. {
  236. ClearForm(); // išvalomi programos duomenys
  237. PreparareForm(-10, 10, -10, 10);
  238. data.Clear();
  239. // apskaičiuojamos funkcijos reikšmės
  240. for (int i = 0; i < 400; i++)
  241. {
  242. float x = i / 50f * (float)(Math.Sin(2 * i / 10f));
  243. float y = i / 50f * (float)(Math.Sin(i / 10f));
  244. data.Add(new PointF(x, y));
  245. }
  246. S1 = chart1.Series.Add("S1");
  247. S1.BorderWidth = 9;
  248. S1.ChartType = SeriesChartType.Line;
  249.  
  250. timer3.Enabled = true;
  251. timer3.Interval = 15;
  252. timer3.Start();
  253. }
  254.  
  255.  
  256. private void timer3_Tick(object sender, EventArgs e)
  257. {
  258. Series S1 = chart1.Series[0];
  259. int pointsSoFar = S1.Points.Count;
  260. if (pointsSoFar < data.Count)
  261. {
  262. S1.Points.AddXY(data[pointsSoFar].X, data[pointsSoFar].Y);
  263. }
  264. else
  265. {
  266. timer1.Stop();
  267. }
  268. }
  269.  
  270.  
  271. // ---------------------------------------------- TIESINĖ ALGEBRA ----------------------------------------------
  272.  
  273. /// <summary>
  274. /// Tiesine algebra (naudojama MathNet)
  275. /// </summary>
  276. private void button2_Click(object sender, EventArgs e)
  277. {
  278. ClearForm();
  279.  
  280. double[,] x = { { 1, 2, 3 }, { 3, 4, 5 }, { 6, 5, 8 } };
  281. // iš masyvo sugeneruoja matricą, is matricos išskiria eilutę - suformuoja vektorių
  282. Matrix<double> m = Matrix<double>.Build.DenseOfArray(x);
  283. Vector<double> v = m.Row(1);
  284. richTextBox1.AppendText("\nMatrica m:\n");
  285. richTextBox1.AppendText(m.ToString());
  286.  
  287. richTextBox1.AppendText("\nVektorius v:\n");
  288. richTextBox1.AppendText(v.ToString());
  289.  
  290. richTextBox1.AppendText("\ntranspose(m):\n");
  291. richTextBox1.AppendText(m.Transpose().ToString());
  292.  
  293. Matrix<double> vm = v.ToRowMatrix();
  294. richTextBox1.AppendText("\nvm = v' - toRowMatrix()\n");
  295. richTextBox1.AppendText(vm.ToString());
  296.  
  297. Vector<double> v1 = m * v;
  298. richTextBox1.AppendText("\nv1 = m * v\n");
  299. richTextBox1.AppendText(v1.ToString());
  300. richTextBox1.AppendText("\nmin(v1)\n");
  301. richTextBox1.AppendText(v1.Min().ToString());
  302.  
  303. Matrix<double> m1 = m.Inverse();
  304. richTextBox1.AppendText("\ninverse(m)\n");
  305. richTextBox1.AppendText(m1.ToString());
  306.  
  307. richTextBox1.AppendText("\ndet(m)\n");
  308. richTextBox1.AppendText(m.Determinant().ToString());
  309.  
  310. // you must add reference to assembly system.Numerics
  311. Evd<double> eigenv = m.Evd();
  312. richTextBox1.AppendText("\neigenvalues(m)\n");
  313. richTextBox1.AppendText(eigenv.EigenValues.ToString());
  314.  
  315. LU<double> LUanswer = m.LU();
  316. richTextBox1.AppendText("\nMatricos M LU skaida\n");
  317. richTextBox1.AppendText("\nMatrica L:\n");
  318. richTextBox1.AppendText(LUanswer.L.ToString());
  319. richTextBox1.AppendText("\nMatrica U:\n");
  320. richTextBox1.AppendText(LUanswer.U.ToString());
  321.  
  322. QR<double> QRanswer = m.QR();
  323. richTextBox1.AppendText("\nMatricos M QR skaida\n");
  324. richTextBox1.AppendText("\nMatrica Q:\n");
  325. richTextBox1.AppendText(QRanswer.Q.ToString());
  326. richTextBox1.AppendText("\nMatrica R:\n");
  327. richTextBox1.AppendText(QRanswer.R.ToString());
  328.  
  329. Vector<double> v3 = m.Solve(v);
  330. richTextBox1.AppendText("\nm*v3 = v sprendziama QR metodu\n");
  331. richTextBox1.AppendText(v3.ToString());
  332. richTextBox1.AppendText("Patikrinimas\n");
  333. richTextBox1.AppendText((m * v3 - v).ToString());
  334.  
  335. }
  336.  
  337.  
  338. // ---------------------------------------------- KITI METODAI ----------------------------------------------
  339.  
  340. /// <summary>
  341. /// Uždaroma programa
  342. /// </summary>
  343. private void button1_Click(object sender, EventArgs e)
  344. {
  345. Close();
  346. }
  347.  
  348. /// <summary>
  349. /// Išvalomas grafikas ir consolė
  350. /// </summary>
  351. private void button4_Click(object sender, EventArgs e)
  352. {
  353. ClearForm();
  354. }
  355.  
  356.  
  357. public void ClearForm()
  358. {
  359. richTextBox1.Clear(); // isvalomas richTextBox1
  360. // sustabdomi timeriai jei tokiu yra
  361. foreach (var timer in Timerlist)
  362. {
  363. timer.Stop();
  364. }
  365.  
  366. // isvalomos visos nubreztos kreives
  367. chart1.Series.Clear();
  368. }
  369. }
  370. }
RAW Paste Data