N!kitoZZ
01-06-2011, 18:10
Привет помогающим, и всем кто просто читает. Нужно написать на шарпе программу, которая будет вычислять значение интеграла с заданной точностью и рисовать график функции.
Функция такая: (1 / (x+2)^2) -1
Большая часть кода есть. Беда в том, что в точке -2 - разрыв. Т.е. эту точку надо обойти. Все свои извращенские манипуляции на эту тему выложу кодом. Кстати кода два, увидите почему.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class myCalculatedFunction
{
public double top;
public double bottom;
public double eps = 0.01;
public double MyFunc(double x)
{
double value = 0.0;
value = 1/(Math.Pow((x+2),2))-1;
if(x==(-2.0))
value = 0;
//if (value >= 30)
//value = 0;
return value;
}
public myCalculatedFunction()
{
top = 0.0;
bottom = 0.0;
}
public double Integral()
{
double integr1 = 0;
//double integr2 = 0;
if(top < bottom)
return 0.0;
double x = bottom;
/* if (x <= (-2))
{
for (x = bottom; x <= (-2.01); x += eps)
{
integr1 = integr1 + MyFunc(x) * eps;
for (x=(-2.01); x <= top; x += eps)
{
if (x == (-2.0))
x += eps;
integr2 = integr2 + MyFunc(x) * eps;
}
}
return integr1 + integr2;
}
else if(x > (-2))*/
for (x = bottom; x <= top; x += eps)
{
if(x==(-2-eps))
x=(-2+eps);
integr1 = integr1 + MyFunc(x) * eps;
}
return integr1;
}
}
}
А, да, еще баттон "рассчет" :) Компилилось сие в VS 2010, в прочих баги будут, ибо нет в предыдущих версиях chart'a(ографик).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
myCalculatedFunction fun = new myCalculatedFunction();
double d1 = Double.Parse(tbTop.Text);
double d2 = Double.Parse(tbBottom.Text);
if(d2 >= d1)
return;
fun.top = d1;
fun.bottom = d2;
double integ = fun.Integral();
chart1.Series.Clear();
chart1.Series.Add("MyFunction");
chart1.Titles.Clear();
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
for (double d = d2; d <= d1; d = d + 0.01)
{
if (d == (-2.01))
d = (-1.99);
chart1.Series[0].Points.AddXY(d, fun.MyFunc(d));
}
chart1.Titles.Add("Graphic of your function");
label3.Text = "Integral your function in limits: [" + d1.ToString() + "; " + d2.ToString() + "] will " + integ.ToString();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Просьба на закомментенные части внимания не обращать, ибо долго резать, да и, вдруг че пригодится)
Помогите, коль сможете, уж больно у меня препод вредный, сказал что точку обрыва надо обойти слева и справа на интервал точности, а как это реализовать даже приблизительно не намекнул))
Премного благодарен )
Функция такая: (1 / (x+2)^2) -1
Большая часть кода есть. Беда в том, что в точке -2 - разрыв. Т.е. эту точку надо обойти. Все свои извращенские манипуляции на эту тему выложу кодом. Кстати кода два, увидите почему.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class myCalculatedFunction
{
public double top;
public double bottom;
public double eps = 0.01;
public double MyFunc(double x)
{
double value = 0.0;
value = 1/(Math.Pow((x+2),2))-1;
if(x==(-2.0))
value = 0;
//if (value >= 30)
//value = 0;
return value;
}
public myCalculatedFunction()
{
top = 0.0;
bottom = 0.0;
}
public double Integral()
{
double integr1 = 0;
//double integr2 = 0;
if(top < bottom)
return 0.0;
double x = bottom;
/* if (x <= (-2))
{
for (x = bottom; x <= (-2.01); x += eps)
{
integr1 = integr1 + MyFunc(x) * eps;
for (x=(-2.01); x <= top; x += eps)
{
if (x == (-2.0))
x += eps;
integr2 = integr2 + MyFunc(x) * eps;
}
}
return integr1 + integr2;
}
else if(x > (-2))*/
for (x = bottom; x <= top; x += eps)
{
if(x==(-2-eps))
x=(-2+eps);
integr1 = integr1 + MyFunc(x) * eps;
}
return integr1;
}
}
}
А, да, еще баттон "рассчет" :) Компилилось сие в VS 2010, в прочих баги будут, ибо нет в предыдущих версиях chart'a(ографик).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
myCalculatedFunction fun = new myCalculatedFunction();
double d1 = Double.Parse(tbTop.Text);
double d2 = Double.Parse(tbBottom.Text);
if(d2 >= d1)
return;
fun.top = d1;
fun.bottom = d2;
double integ = fun.Integral();
chart1.Series.Clear();
chart1.Series.Add("MyFunction");
chart1.Titles.Clear();
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
for (double d = d2; d <= d1; d = d + 0.01)
{
if (d == (-2.01))
d = (-1.99);
chart1.Series[0].Points.AddXY(d, fun.MyFunc(d));
}
chart1.Titles.Add("Graphic of your function");
label3.Text = "Integral your function in limits: [" + d1.ToString() + "; " + d2.ToString() + "] will " + integ.ToString();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Просьба на закомментенные части внимания не обращать, ибо долго резать, да и, вдруг че пригодится)
Помогите, коль сможете, уж больно у меня препод вредный, сказал что точку обрыва надо обойти слева и справа на интервал точности, а как это реализовать даже приблизительно не намекнул))
Премного благодарен )