.NET 4.x Ошибка автоматической настройки интервалов из-за недопустимого значения в точке
09.09.2018, 05:53. Показов 7587. Ответов 0
System.InvalidOperationException: "Объект Axis - ошибка автоматической настройки интервалов из-за недопустимого значения в точке либо минимального или максимального значения оси."
Я в новосозданный мною проект WindowsForms (без кода) закидываю код который мне помогли сделать.
Но произошла какая-то ошибка несовместимая с моим уравнением на моменте когда я уже нажимал на "Найти x и построить графики". Вероятно, неверные данные. Либо там стоит ограничение по вводу числа. Не пойму что не так и в чем ошибка.
У меня было задание: Создать приложение позволяющее решать методом Ньютона и методом половинного деления нелинейные уравнения (согласно варианту), а также выполнять графическое построение графиков этих уравнений с помощью элемента управления Chart. При выполнении задания обязательно пользоваться делегатами. Предусмотреть проверку корректности ввода исходных данных в приложение и обработку исключительных ситуаций. Создать в приложении справочную систему. 1) logx– x + 2=0; 2) 2 x^3 +x^2 –x-2=0
Form1.Designer.cs
Кликните здесь для просмотра всего текста
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
| namespace Lab4VP
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.CalculateBtn = new System.Windows.Forms.Button();
this.Func1Lbl = new System.Windows.Forms.Label();
this.Func2Lbl = new System.Windows.Forms.Label();
this.HFunc2Lbl = new System.Windows.Forms.Label();
this.HFunc1Lbl = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.Chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
((System.ComponentModel.ISupportInitialize)(this.Chart1)).BeginInit();
this.SuspendLayout();
//
// CalculateBtn
//
this.CalculateBtn.Location = new System.Drawing.Point(475, 42);
this.CalculateBtn.Name = "CalculateBtn";
this.CalculateBtn.Size = new System.Drawing.Size(215, 53);
this.CalculateBtn.TabIndex = 0;
this.CalculateBtn.Text = "Найти x и построить графики";
this.CalculateBtn.UseVisualStyleBackColor = true;
this.CalculateBtn.Click += new System.EventHandler(this.CalculateBtn_Click);
//
// Func1Lbl
//
this.Func1Lbl.AutoSize = true;
this.Func1Lbl.Location = new System.Drawing.Point(29, 42);
this.Func1Lbl.Name = "Func1Lbl";
this.Func1Lbl.Size = new System.Drawing.Size(32, 13);
this.Func1Lbl.TabIndex = 1;
this.Func1Lbl.Text = "X1 = ";
//
// Func2Lbl
//
this.Func2Lbl.AutoSize = true;
this.Func2Lbl.Location = new System.Drawing.Point(29, 82);
this.Func2Lbl.Name = "Func2Lbl";
this.Func2Lbl.Size = new System.Drawing.Size(32, 13);
this.Func2Lbl.TabIndex = 2;
this.Func2Lbl.Text = "X2 = ";
//
// HFunc2Lbl
//
this.HFunc2Lbl.AutoSize = true;
this.HFunc2Lbl.Location = new System.Drawing.Point(189, 82);
this.HFunc2Lbl.Name = "HFunc2Lbl";
this.HFunc2Lbl.Size = new System.Drawing.Size(32, 13);
this.HFunc2Lbl.TabIndex = 4;
this.HFunc2Lbl.Text = "X2 = ";
//
// HFunc1Lbl
//
this.HFunc1Lbl.AutoSize = true;
this.HFunc1Lbl.Location = new System.Drawing.Point(189, 42);
this.HFunc1Lbl.Name = "HFunc1Lbl";
this.HFunc1Lbl.Size = new System.Drawing.Size(32, 13);
this.HFunc1Lbl.TabIndex = 3;
this.HFunc1Lbl.Text = "X1 = ";
this.HFunc1Lbl.Click += new System.EventHandler(this.HFunc1Lbl_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(189, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 13);
this.label1.TabIndex = 5;
this.label1.Text = "Решение методом половинного деления:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(151, 13);
this.label2.TabIndex = 6;
this.label2.Text = "Решение методом Ньютона:";
//
// Chart1
//
chartArea1.Name = "ChartArea1";
this.Chart1.ChartAreas.Add(chartArea1);
legend1.Name = "Legend1";
this.Chart1.Legends.Add(legend1);
this.Chart1.Location = new System.Drawing.Point(15, 123);
this.Chart1.Name = "Chart1";
series1.ChartArea = "ChartArea1";
series1.Legend = "Legend1";
series1.Name = "Series1";
this.Chart1.Series.Add(series1);
this.Chart1.Size = new System.Drawing.Size(670, 335);
this.Chart1.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(702, 476);
this.Controls.Add(this.Chart1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.HFunc2Lbl);
this.Controls.Add(this.HFunc1Lbl);
this.Controls.Add(this.Func2Lbl);
this.Controls.Add(this.Func1Lbl);
this.Controls.Add(this.CalculateBtn);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.HelpButton = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowIcon = false;
this.Text = "Лабораторная №4";
this.Load += new System.EventHandler(this.Form1_Load);
this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);
((System.ComponentModel.ISupportInitialize)(this.Chart1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button CalculateBtn;
private System.Windows.Forms.Label Func1Lbl;
private System.Windows.Forms.Label Func2Lbl;
private System.Windows.Forms.Label HFunc2Lbl;
private System.Windows.Forms.Label HFunc1Lbl;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.DataVisualization.Charting.Chart Chart1;
}
} |
|
Form1.cs
Кликните здесь для просмотра всего текста
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
| 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;
using System.Windows.Forms.DataVisualization.Charting;
namespace Lab4VP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Chart1.Series.Clear();
Chart1.ChartAreas.Clear();
Chart1.Series.Add("График 1");
Chart1.Series.Add("График 2");
Chart1.ChartAreas.Add("График 1");
Chart1.ChartAreas.Add("График 2");
Chart1.ChartAreas[0].Position.X = 0;
Chart1.ChartAreas[1].Position.X = 40;
Chart1.ChartAreas[0].Position.Width = 40;
Chart1.ChartAreas[1].Position.Width = 40;
Chart1.ChartAreas[0].Position.Height = 100;
Chart1.ChartAreas[1].Position.Height = 100;
Chart1.Series[0].ChartType = SeriesChartType.Spline;
Chart1.Series[1].ChartType = SeriesChartType.Spline;
Chart1.Series[0].ChartArea = "График 1";
Chart1.Series[1].ChartArea = "График 2";
Chart1.Series[0].BorderWidth = 2;
Chart1.Series[1].BorderWidth = 2;
Chart1.Series[1].Color = Color.PaleVioletRed;
Chart1.ChartAreas[0].AxisY.Interval = 5;
Chart1.ChartAreas[0].AxisX.Interval = 2.5;
Chart1.ChartAreas[1].AxisY.Interval = 100;
Chart1.ChartAreas[1].AxisX.Interval = 2.5;
}
private delegate double FuncDeleg(double x);
static double Fun1(double x)
{
return Math.Log10(x) - x + 2.0;
}
static double Fun1_derivative(double x)
{
return 1/x * Math.Log(10) - 1.0;
}
static double Fun2(double x)
{
return 2 * Math.Pow(x, 3.0) + Math.Pow(x, 2.0) - x - 2.0;
}
static double Fun2_derivative(double x)
{
return 6 * Math.Pow(x, 2.0) + 2.0 * x - 1.0;
}
static double Newton(double x, double eps, FuncDeleg fun, FuncDeleg fun_derivative)
{
double y = x;
try
{
if ((x < -10) || (x > 10))
MessageBox.Show("Вне диапазона");
}
catch (Exception ex)
{
MessageBox.Show("Вне диапазона");
}
x = x - fun(x) / fun_derivative(x);
if (Math.Abs(y - x) >= eps)
{
return Newton(x, eps, fun, fun_derivative);
}
else
{
return x;
}
}
static double HalfInterval(double a, double b, double eps, FuncDeleg fun)
{
var fa = fun(a);
var fb = fun(b);
double c;
if (Math.Abs(fa) < eps)
c = a;
else if (Math.Abs(fb) < eps)
c = b;
else if (fa * fb > 0)
return b + 1;
do
{
c = (a + b) / 2.0;
var fc = fun(c);
if (Math.Abs(fun(c)) < eps) break;
if (fa * fc > 0) { a = c; }
else b = c;
} while ((b - a > eps) && (fa * fb < 0));
return c;
}
private void CalculateBtn_Click(object sender, EventArgs e)
{
FuncDeleg f1 = new FuncDeleg(Fun1);
FuncDeleg f1d = new FuncDeleg(Fun1_derivative);
FuncDeleg f2 = new FuncDeleg(Fun2);
FuncDeleg f2d = new FuncDeleg(Fun2_derivative);
Func1Lbl.Text = $"X1 = {Newton(0, 0.01, f1, f1d)}";
HFunc1Lbl.Text = $"X1 = {HalfInterval(0, 10, 0.01, f1)}";
Func2Lbl.Text = $"X2 = {Newton(0, 0.01, f2, f2d)}";
HFunc2Lbl.Text = $"X2 = {HalfInterval(-10, 10, 0.01, f2)}";
for (int i = 0; i < 20; i++)
{
Chart1.Series[0].Points.AddXY(i, Fun1(i));
Chart1.Series[1].Points.AddXY(i - 9, Fun2(i - 9));
}
CalculateBtn.Enabled = false;
}
private void HFunc1Lbl_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
{
}
}
} |
|
Program.cs
Кликните здесь для просмотра всего текста
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab4VP
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
} |
|
Я взял переделал под свои функции код
с
Кликните здесь для просмотра всего текста
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| private delegate double FuncDeleg(double x);
static double Fun1(double x)
{
return x* Math.Log10(x+3.0)-1.0;
}
static double Fun1_derivative(double x)
{
return Math.Log10(x + 3.0) + x / ((x + 3.0) * Math.Log(10));
}
static double Fun2(double x)
{
return Math.Pow(x, 3.0) + Math.Pow(x, 2.0) + 2.0 * x + 3.0;
}
static double Fun2_derivative(double x)
{
return 3 * Math.Pow(x, 2.0) + 2.0 * x + 2.0;
}
................ |
|
на
Кликните здесь для просмотра всего текста
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| private delegate double FuncDeleg(double x);
static double Fun1(double x)
{
return Math.Log10(x) - x + 2.0;
}
static double Fun1_derivative(double x)
{
return 1/x * Math.Log(10) - 1.0;
}
static double Fun2(double x)
{
return 2 * Math.Pow(x, 3.0) + Math.Pow(x, 2.0) - x - 2.0;
}
static double Fun2_derivative(double x)
{
return 6 * Math.Pow(x, 2.0) + 2.0 * x - 1.0;
}
...... |
|
в Form1.cs и у меня начали возникать ошибки и что там не так я не пойму.
Я проверил пошаговым нажатием F11
у меня ошибка стоит видимо с очень большой вероятностью в коде Form1.cs в 78 строке
| C# | 1
| if ((x < -10) || (x > 10)) |
|
Короче я запутался какой промежуток нада брать (или может быть проблема вообще не в нем
0
|