Advertisement
Guest User

Untitled

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 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 WindowsFormsApp1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         double r, l, extr, x ;
  16.  
  17.         double f (double x)
  18.         {
  19.             return Math.Exp(x) - 2 * x - 3;
  20.         }
  21.         public Form1()
  22.         {
  23.             InitializeComponent();            
  24.         }
  25.  
  26.         public void button1_Click(object sender, EventArgs e)
  27.         {
  28.             chart1.ChartAreas[0].AxisY.ScaleView.Zoom(-100, 100);
  29.             chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
  30.             chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
  31.             chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
  32.             chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;
  33.             for (double x = l; x < r; x++)
  34.             {
  35.                 chart1.Series[0].Points.AddXY(x, Math.Exp(x) - 2 * x - 3);
  36.                
  37.             }
  38.             for (double x = l; x < r; x +=.1d)
  39.             {
  40.                 dataGridView1.Rows.Add(Math.Round(x,1), Math.Round(f(x),1));
  41.             }
  42.         }            
  43.  
  44.         public void numericUpDown1_ValueChanged(object sender, EventArgs e)
  45.         {
  46.              l = Convert.ToDouble(numericUpDown1.Value);
  47.            
  48.         }
  49.  
  50.         public void numericUpDown2_ValueChanged(object sender, EventArgs e)
  51.         {
  52.              r = Convert.ToDouble(numericUpDown2.Value);
  53.            
  54.         }
  55.  
  56.         public double Dfdx(double x)
  57.         {
  58.             return Math.Exp(x) - 2;
  59.         }
  60.  
  61.  
  62.         double ExtrSearch(double l, double r, double epsilon= 0.001)
  63.         {
  64.             double m = (l + r) / 2;
  65.             if (r - l <= 2 * epsilon) return m;
  66.             double dl = Dfdx(l);
  67.             double dr = Dfdx(r);
  68.             double dm = Dfdx(m);
  69.             if (dl * dr > 0) throw new Exception("Экстремума на этом интервале нет.");
  70.             if (dm * dl > 0) return ExtrSearch(m, r);
  71.             else return ExtrSearch(l, m);
  72.            
  73.         }
  74.  
  75.         private void button4_Click(object sender, EventArgs e)
  76.         {
  77.             extr = ExtrSearch(l, r);
  78.             label3.Text = $"{ Math.Round(extr, 4)}";
  79.         }
  80.        
  81.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  82.         {
  83.  
  84.         }
  85.  
  86.         public void label3_Click_1(object sender, EventArgs e)
  87.         {
  88.          
  89.         }      
  90.  
  91.         public void label5_Click(object sender, EventArgs e)
  92.         {
  93.            
  94.         }
  95.  
  96.  
  97.         private void button2_Click(object sender, EventArgs e)
  98.         {
  99.             this.Close();
  100.         }
  101.  
  102.        
  103.         public void Form1_Load(object sender, EventArgs e)
  104.         {
  105.  
  106.         }        
  107.  
  108.         private void chart1_Click(object sender, EventArgs e)
  109.         {
  110.  
  111.         }
  112.  
  113.         private void button5_Click(object sender, EventArgs e)
  114.         {
  115.             dataGridView1.Rows.Clear();
  116.             foreach (DataGridViewRow row in dataGridView1.Rows)
  117.             {
  118.                 chart1.Series[0].Points.Clear();
  119.             }
  120.  
  121.         }
  122.  
  123.         private void менюToolStripMenuItem_Click(object sender, EventArgs e)
  124.         {
  125.  
  126.         }
  127.  
  128.         private void отчетToolStripMenuItem_Click(object sender, EventArgs e)
  129.         {
  130.             Form2 form2 = new Form2(extr);
  131.             form2.ShowDialog();
  132.         }
  133.  
  134.         private void label1_Click(object sender, EventArgs e)
  135.         {
  136.  
  137.         }
  138.  
  139.         private void label2_Click(object sender, EventArgs e)
  140.         {
  141.  
  142.         }      
  143.  
  144.         private void label3_Click(object sender, EventArgs e)
  145.         {
  146.            
  147.         }          
  148.                
  149.        
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement