用jfreechart繪制光滑曲線,利用最小二乘法數(shù)學(xué)原理計(jì)算,供大家參考,具體內(nèi)容如下
繪制圖形:
代碼:
fittingcurve.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
package org.jevy; import java.util.arraylist; import java.util.list; import org.jfree.chart.chartfactory; import org.jfree.chart.chartpanel; import org.jfree.chart.jfreechart; import org.jfree.chart.axis.valueaxis; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.plot.xyplot; import org.jfree.chart.renderer.xy.xyitemrenderer; import org.jfree.chart.renderer.xy.xylineandshaperenderer; import org.jfree.data.xy.xydataset; import org.jfree.data.xy.xyseries; import org.jfree.data.xy.xyseriescollection; import org.jfree.ui.applicationframe; import org.jfree.ui.refineryutilities; public class fittingcurve extends applicationframe{ list< double > equation = null ; //設(shè)置多項(xiàng)式的次數(shù) int times = 2 ; public fittingcurve(string title) { super (title); //使用最小二乘法計(jì)算擬合多項(xiàng)式中各項(xiàng)前的系數(shù)。 /* 請(qǐng)注意: 多項(xiàng)式曲線參數(shù)計(jì)算 與 chart圖表生成 是分開(kāi)處理的。 多項(xiàng)式曲線參數(shù)計(jì)算: 負(fù)責(zé)計(jì)算多項(xiàng)式系數(shù), 返回多項(xiàng)式系數(shù)list。 chart圖表生成: 僅僅負(fù)責(zé)按照給定的數(shù)據(jù)繪圖。 比如對(duì)給定的點(diǎn)進(jìn)行連線。 本實(shí)例中,光滑的曲線是用密度很高的點(diǎn)連線繪制出來(lái)的。 由于我們計(jì)算出了多項(xiàng)式的系數(shù),所以我們讓x軸數(shù)據(jù)按照很小的步長(zhǎng)增大,針對(duì)每一個(gè)x值,使用多項(xiàng)式計(jì)算出y值, 從而得出點(diǎn)眾多的(x,y)組。 把這些(x, y)組成的點(diǎn)連線繪制出來(lái),則顯示出光滑的曲線。 xyseries為jfreechart繪圖數(shù)據(jù)集, 用于繪制一組有關(guān)系的數(shù)據(jù)。 xyseries對(duì)應(yīng)于x,y坐標(biāo)軸數(shù)據(jù)集, 添加數(shù)據(jù)方式為: xyseries s.add(x,y); xyseriescollection 為xyseries的集合, 當(dāng)需要在一個(gè)chart上繪制多條曲線的時(shí)候,需要把多條曲線對(duì)應(yīng)的xyseries添加到xyseriescollection 添加方法:dataset.addseries(s1); dataset.addseries(s2); */ //多項(xiàng)式的次數(shù)從高到低,該函數(shù)需要的參數(shù):x軸數(shù)據(jù)<list>,y軸數(shù)據(jù)<list>,多項(xiàng)式的次數(shù)<2> this .equation = this .getcurveequation( this .getdata().get( 0 ), this .getdata().get( 1 ), this .times); //生成chart jfreechart chart = this .getchart(); chartpanel chartpanel = new chartpanel(chart); chartpanel.setpreferredsize( new java.awt.dimension( 500 , 270 )); chartpanel.setmousezoomable( true , false ); setcontentpane(chartpanel); } public static void main(string[] args) { // todo auto-generated method stub fittingcurve demo = new fittingcurve( "xyfittingcurve" ); demo.pack(); refineryutilities.centerframeonscreen(demo); demo.setvisible( true ); } //生成chart public jfreechart getchart(){ //獲取x和y軸數(shù)據(jù)集 xydataset xydataset = this .getxydataset(); //創(chuàng)建用坐標(biāo)表示的折線圖 jfreechart xychart = chartfactory.createxylinechart( "二次多項(xiàng)式擬合光滑曲線" , "x軸" , "y軸" , xydataset, plotorientation.vertical, true , true , false ); //生成坐標(biāo)點(diǎn)點(diǎn)的形狀 xyplot plot = (xyplot) xychart.getplot(); xyitemrenderer r = plot.getrenderer(); if (r instanceof xylineandshaperenderer) { xylineandshaperenderer renderer = (xylineandshaperenderer) r; renderer.setbaseshapesvisible( false ); //坐標(biāo)點(diǎn)的形狀是否可見(jiàn) renderer.setbaseshapesfilled( false ); } valueaxis yaxis = plot.getrangeaxis(); yaxis.setlowermargin( 2 ); return xychart; } //數(shù)據(jù)集按照邏輯關(guān)系添加到對(duì)應(yīng)的集合 public xydataset getxydataset() { //預(yù)設(shè)數(shù)據(jù)點(diǎn)數(shù)據(jù)集 xyseries s2 = new xyseries( "點(diǎn)點(diǎn)連線" ); for ( int i= 0 ; i<data.get( 0 ).size(); i++){ s2.add(data.get( 0 ).get(i),data.get( 1 ).get(i)); } // 擬合曲線繪制 數(shù)據(jù)集 xyseries s1 = new xyseries("擬合曲線"); //獲取擬合多項(xiàng)式系數(shù),equation在構(gòu)造方法中已經(jīng)實(shí)例化 list< double > list = this .equation; //獲取預(yù)設(shè)的點(diǎn)數(shù)據(jù) list<list< double >> data = this .getdata(); //get max and min of x; list< double > xlist = data.get( 0 ); double max = this .getmax(xlist); double min = this .getmin(xlist); double step = max - min; double x = min; double step2 = step/ 800.0 ; //按照多項(xiàng)式的形式 還原多項(xiàng)式,并利用多項(xiàng)式計(jì)算給定x時(shí)y的值 for ( int i= 0 ; i< 800 ; i++){ x = x + step2; int num = list.size()- 1 ; double temp = 0.0 ; for ( int j= 0 ; j<list.size(); j++){ temp = temp + math.pow(x, (num-j))*list.get(j); } s1.add(x, temp); } //把預(yù)設(shè)數(shù)據(jù)集合擬合數(shù)據(jù)集添加到xyseriescollection xyseriescollection dataset = new xyseriescollection(); dataset.addseries(s1); dataset.addseries(s2); return dataset; } //模擬設(shè)置繪圖數(shù)據(jù)(點(diǎn)) public list<list< double >> getdata(){ //x為x軸坐標(biāo) list< double > x = new arraylist< double >(); list< double > y = new arraylist< double >(); for ( int i= 0 ; i< 10 ; i++){ x.add(- 5.0 +i); } y.add( 26.0 ); y.add( 17.1 ); y.add( 10.01 ); y.add( 5.0 ); y.add( 2.01 ); y.add( 1.0 ); y.add( 2.0 ); y.add( 5.01 ); y.add( 10.1 ); y.add( 17.001 ); list<list< double >> list = new arraylist<list< double >>(); list.add(x); list.add(y); return list; } //以下代碼為最小二乘法計(jì)算多項(xiàng)式系數(shù) //最小二乘法多項(xiàng)式擬合 public list< double > getcurveequation(list< double > x, list< double > y, int m){ if (x.size() != y.size() || x.size() <= m+ 1 ){ return new arraylist< double >(); } list< double > result = new arraylist< double >(); list< double > s = new arraylist< double >(); list< double > t = new arraylist< double >(); //計(jì)算s0 s1 …… s2m for ( int i= 0 ; i<= 2 *m; i++){ double si = 0.0 ; for ( double xx:x){ si = si + math.pow(xx, i); } s.add(si); } //計(jì)算t0 t1 …… tm for ( int j= 0 ; j<=m; j++){ double ti = 0.0 ; for ( int k= 0 ; k<y.size(); k++){ ti = ti + y.get(k)*math.pow(x.get(k), j); } t.add(ti); } //把s和t 放入二維數(shù)組,作為矩陣 double [][] matrix = new double [m+ 1 ][m+ 2 ]; for ( int k= 0 ; k<m+ 1 ; k++){ double [] matrixi = matrix[k]; for ( int q= 0 ; q<m+ 1 ; q++){ matrixi[q] = s.get(k+q); } matrixi[m+ 1 ] = t.get(k); } for ( int p= 0 ; p<matrix.length; p++){ for ( int pp= 0 ; pp<matrix[p].length; pp++){ system.out.print( " matrix[" +p+ "][" +pp+ "]=" +matrix[p][pp]); } system.out.println(); } //把矩陣轉(zhuǎn)化為三角矩陣 matrix = this .matrixconvert(matrix); //計(jì)算多項(xiàng)式系數(shù),多項(xiàng)式從高到低排列 result = this .matrixcalcu(matrix); return result; } //矩陣轉(zhuǎn)換為三角矩陣 public double [][] matrixconvert( double [][] d){ for ( int i= 0 ; i<d.length- 1 ; i++){ double [] dd1 = d[i]; double num1 = dd1[i]; for ( int j=i; j<d.length- 1 ;j++ ){ double [] dd2 = d[j+ 1 ]; double num2 = dd2[i]; for ( int k= 0 ; k<dd2.length; k++){ dd2[k] = (dd2[k]*num1 - dd1[k]*num2); } } } for ( int ii= 0 ; ii<d.length; ii++){ for ( int kk= 0 ; kk<d[ii].length; kk++) system.out.print(d[ii][kk]+ " " ); system.out.println(); } return d; } //計(jì)算一元多次方程前面的系數(shù), 其排列為 xm xm-1 …… x0(多項(xiàng)式次數(shù)從高到低排列) public list< double > matrixcalcu( double [][] d){ int i = d.length - 1 ; int j = d[ 0 ].length - 1 ; list< double > list = new arraylist< double >(); double res = d[i][j]/d[i][j- 1 ]; list.add(res); for ( int k=i- 1 ; k>= 0 ; k--){ double num = d[k][j]; for ( int q=j- 1 ; q>k; q--){ num = num - d[k][q]*list.get(j- 1 -q); } res = num/d[k][k]; list.add(res); } return list; } //獲取list中double數(shù)據(jù)的最大最小值 public double getmax(list< double > data){ double res = data.get( 0 ); for ( int i= 0 ; i<data.size()- 1 ; i++){ if (res<data.get(i+ 1 )){ res = data.get(i+ 1 ); } } return res; } public double getmin(list< double > data){ double res = data.get( 0 ); for ( int i= 0 ; i<data.size()- 1 ; i++){ if (res>data.get(i+ 1 )){ res = data.get(i+ 1 ); } } return res; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/jvl_jevy/article/details/5456232