Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  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.  
  11. namespace TO2Lab2
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         #region
  20.         double a, b, c, d, x0, x, n, limD, limG;
  21.         #endregion
  22.         private void TextBox8_TextChanged(object sender, EventArgs e)
  23.         {
  24.  
  25.         }
  26.  
  27.         private void Button1_Click(object sender, EventArgs e)
  28.         {
  29.             textBox6.Clear();
  30.             a = double.Parse(textBox1.Text);
  31.             b = double.Parse(textBox2.Text);
  32.             c = double.Parse(textBox3.Text);
  33.             d = double.Parse(textBox4.Text);
  34.  
  35.             if (radioButton1.Checked)
  36.             {
  37.                 newton();
  38.             }
  39.  
  40.             else if(radioButton2.Checked)
  41.             {
  42.                 falsi();
  43.             }
  44.             else
  45.             {
  46.                 textBox6.Text = "Wybierz metodę!";
  47.             }
  48.         }
  49.  
  50.         private void Button2_Click(object sender, EventArgs e)
  51.         {
  52.             textBox6.Clear();
  53.         }
  54.         public double f(double x)
  55.         {
  56.             return a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d;
  57.         }
  58.         public double df(double x)
  59.         {
  60.             return 3 * a * Math.Pow(x, 2) + 2 * b * x + c;
  61.         }
  62.  
  63.         public void newton()
  64.         {
  65.             x0 = double.Parse(textBox5.Text);
  66.             textBox6.SelectedText = "iteracja\t" + "x\t\t\t" + "f(x)" + Environment.NewLine;
  67.             x = x0;
  68.             for (int i = 0; i <= 25; i++)
  69.             {
  70.                 x = x - f(x) / df(x);
  71.                 n = n + 1;
  72.                 textBox6.SelectedText = i + "\t" + x + "\t" + f(x) + Environment.NewLine;
  73.  
  74.             }
  75.         }
  76.         public void falsi()
  77.         {
  78.             limD = double.Parse(textBox7.Text);
  79.             limG = double.Parse(textBox8.Text);
  80.  
  81.             if ((f(limD)*f(limG))>0)
  82.             {
  83.                 textBox6.Text = "Błędne granice izolacji pierwiastka!";
  84.             }
  85.             else
  86.             {
  87.                 textBox6.SelectedText = "iteracja\t\t" + "x\t" + "limD\t\t\t\t" + "limG" + Environment.NewLine;
  88.  
  89.                 for(int i =1; i < 30; i++)
  90.                 {
  91.                     x = limG - f(limG) * (limG - limD) / (f(limG) - f(limD));
  92.                     if(f(x) == 0)
  93.                     {
  94.                         textBox6.Text = x.ToString() + " jest to wartość pierwiastka!";
  95.                     }
  96.                     else if (f(x) * f(limD) < 0)
  97.                     {
  98.                         limG = x;
  99.                    
  100.                     }
  101.                     else if (f(x) * f(limG) < 0)
  102.                     {
  103.                         limD = x;
  104.                        
  105.                     }
  106.                     textBox6.SelectedText = "iteracja;\t\t" + i + "x:\t" + limD + "\t\t" + limG + Environment.NewLine;
  107.                 }
  108.             }
  109.     }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement