Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 TO2Lab2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- #region
- double a, b, c, d, x0, x, n, limD, limG;
- #endregion
- private void TextBox8_TextChanged(object sender, EventArgs e)
- {
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- textBox6.Clear();
- a = double.Parse(textBox1.Text);
- b = double.Parse(textBox2.Text);
- c = double.Parse(textBox3.Text);
- d = double.Parse(textBox4.Text);
- if (radioButton1.Checked)
- {
- newton();
- }
- else if(radioButton2.Checked)
- {
- falsi();
- }
- else
- {
- textBox6.Text = "Wybierz metodę!";
- }
- }
- private void Button2_Click(object sender, EventArgs e)
- {
- textBox6.Clear();
- }
- public double f(double x)
- {
- return a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d;
- }
- public double df(double x)
- {
- return 3 * a * Math.Pow(x, 2) + 2 * b * x + c;
- }
- public void newton()
- {
- x0 = double.Parse(textBox5.Text);
- textBox6.SelectedText = "iteracja\t" + "x\t\t\t" + "f(x)" + Environment.NewLine;
- x = x0;
- for (int i = 0; i <= 25; i++)
- {
- x = x - f(x) / df(x);
- n = n + 1;
- textBox6.SelectedText = i + "\t" + x + "\t" + f(x) + Environment.NewLine;
- }
- }
- public void falsi()
- {
- limD = double.Parse(textBox7.Text);
- limG = double.Parse(textBox8.Text);
- if ((f(limD)*f(limG))>0)
- {
- textBox6.Text = "Błędne granice izolacji pierwiastka!";
- }
- else
- {
- textBox6.SelectedText = "iteracja\t\t" + "x\t" + "limD\t\t\t\t" + "limG" + Environment.NewLine;
- for(int i =1; i < 30; i++)
- {
- x = limG - f(limG) * (limG - limD) / (f(limG) - f(limD));
- if(f(x) == 0)
- {
- textBox6.Text = x.ToString() + " jest to wartość pierwiastka!";
- }
- else if (f(x) * f(limD) < 0)
- {
- limG = x;
- }
- else if (f(x) * f(limG) < 0)
- {
- limD = x;
- }
- textBox6.SelectedText = "iteracja;\t\t" + i + "x:\t" + limD + "\t\t" + limG + Environment.NewLine;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement