如果算的數(shù)小,可以放在int類型或者double類型的變量里.
目前創(chuàng)新互聯(lián)建站已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、樅陽網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
太大的,就要放到BigInteger類型的變量里.
先看看放到int類型的變量代碼
---------------------------------------------------------------
/*
* 數(shù)量比較小的情況下可以用這個(gè)來計(jì)算
* 太大了就存放不了了
*/
public class PowerNumber {
public static void main(String[] args) {
PowerNumber power = new PowerNumber();
// 準(zhǔn)備求第幾項(xiàng)的值
int n = 10;
// 這里先打印第4項(xiàng)
System.out.println("第" + n + "項(xiàng)結(jié)果是:" + power.powerNumberN(n));
// 這里打印前4項(xiàng)的和
System.out.println("前" + n + "項(xiàng)的和是:" + power.powerNumberSum(n));
}
// 計(jì)算第n項(xiàng)的值
int powerNumberN(int n) {
// 用來存放第n個(gè)數(shù)
int sum = 1;
for (int i = 1; i = n; i++) {
sum = 2 * sum;
}
return sum;
}
// 計(jì)算前n項(xiàng)的和
int powerNumberSum(int n) {
// 存放前n項(xiàng)的和
int sumNum = 0;
int frontN = n;
for (int i = 1; i = n; i++) {
// 存放第n項(xiàng)的值
int sum = 1;
for (int j = 1; j = frontN; j++) {
// 第n項(xiàng)的值
sum = 2 * sum;
}
//求前一項(xiàng)
frontN--;
// 求出的值加到總和里
sumNum = sumNum + sum;
}
return sumNum;
}
}
------------------------------------------------------
前4項(xiàng)的結(jié)果:
第4項(xiàng)結(jié)果是:16
前4項(xiàng)的和是:30
--------------------
前20項(xiàng)的結(jié)果:
第20項(xiàng)結(jié)果是:1048576
前20項(xiàng)的和是:2097150
----------------------
前40項(xiàng)的結(jié)果......
第40項(xiàng)結(jié)果是:0
前40項(xiàng)的和是:-2
================================================
再看看放到BigInteger類型的變量里的程序
---------------------------------
import java.math.BigInteger;
public class Power {
public static void main(String[] args) {
Power power = new Power();
// 準(zhǔn)備求第幾項(xiàng)的值
int n = 4;
// 這里先打印第4項(xiàng)
System.out.println("第" + n + "項(xiàng)結(jié)果是:" + power.powerNum(n));
// 這里打印前4項(xiàng)的和
System.out.println("前" + n + "項(xiàng)的和是:" + power.sumPowerNum(n));
}
// 計(jì)算第n項(xiàng)的值
BigInteger powerNum(int n) {
BigInteger num = BigInteger.ONE;
for (int i = 1; i = n; i++) {
// 為了防止結(jié)果過大,將結(jié)果放在BigInteger中
// 每次對(duì)結(jié)果乘以2
num = num.multiply(new BigInteger(new Integer(2).toString()));
}
// 打印2^n結(jié)果
return num;
}
// 計(jì)算前n項(xiàng)的值
BigInteger sumPowerNum(int n) {
// 存放前n項(xiàng)的值
BigInteger sumNum = BigInteger.ZERO;
int frontN = n;
for (int i = 1; i = n; i++) {
// 存放第n項(xiàng)的值
BigInteger num = BigInteger.ONE;
for (int j = 1; j = frontN; j++) {
// 為了防止結(jié)果過大,將結(jié)果放在BigInteger中
// 每次對(duì)結(jié)果乘以2
num = num.multiply(new BigInteger(new Integer(2).toString()));
}
// 每次循環(huán)讓最大值減掉1,以計(jì)算前面的值
frontN--;
// 計(jì)算出第n項(xiàng)的值,將其放入總和sumNum中
sumNum = sumNum.add(num);
}
return sumNum;
}
}
---------------------------------------------------
前4項(xiàng)的結(jié)果:
第4項(xiàng)結(jié)果是:16
前4項(xiàng)的和是:30
--------------------------------------------------
前40項(xiàng)的結(jié)果:
第40項(xiàng)結(jié)果是:1099511627776
前40項(xiàng)的和是:2199023255550
---------------------------------------------------------------------------------------------
前400項(xiàng)的結(jié)果:
第400項(xiàng)結(jié)果是:2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493376
前400項(xiàng)的和是:5164499756173817179311838344006023748659411585658447025661318713081295244033682389259290706560275662871806343945494986750
-------------------------------------------------------------------------------------
前4000項(xiàng)的結(jié)果:
第4000項(xiàng)結(jié)果是:13182040934309431001038897942365913631840191610932727690928034502417569281128344551079752123172122033140940756480716823038446817694240581281731062452512184038544674444386888956328970642771993930036586552924249514488832183389415832375620009284922608946111038578754077913265440918583125586050431647284603636490823850007826811672468900210689104488089485347192152708820119765006125944858397761874669301278745233504796586994514054435217053803732703240283400815926169348364799472716094576894007243168662568886603065832486830606125017643356469732407252874567217733694824236675323341755681839221954693820456072020253884371226826844858636194212875139566587445390068014747975813971748114770439248826688667129237954128555841874460665729630492658600179338272579110020881228767361200603478973120168893997574353727653998969223092798255701666067972698906236921628764772837915526086464389161570534616956703744840502975279094087587298968423516531626090898389351449020056851221079048966718878943309232071978575639877208621237040940126912767610658141079378758043403611425454744180577150855204937163460902512732551260539639221457005977247266676344018155647509515396711351487546062479444592779055555421362722504575706910949376
前4000項(xiàng)的和是:26364081868618862002077795884731827263680383221865455381856069004835138562256689102159504246344244066281881512961433646076893635388481162563462124905024368077089348888773777912657941285543987860073173105848499028977664366778831664751240018569845217892222077157508155826530881837166251172100863294569207272981647700015653623344937800421378208976178970694384305417640239530012251889716795523749338602557490467009593173989028108870434107607465406480566801631852338696729598945432189153788014486337325137773206131664973661212250035286712939464814505749134435467389648473350646683511363678443909387640912144040507768742453653689717272388425750279133174890780136029495951627943496229540878497653377334258475908257111683748921331459260985317200358676545158220041762457534722401206957946240337787995148707455307997938446185596511403332135945397812473843257529545675831052172928778323141069233913407489681005950558188175174597936847033063252181796778702898040113702442158097933437757886618464143957151279754417242474081880253825535221316282158757516086807222850909488361154301710409874326921805025465102521079278442914011954494533352688036311295019030793422702975092124958889185558111110842725445009151413821898750
/**
* 最小二乘法計(jì)算類
*
* @author Administrator
*
*/
public class LeastSquareMethod {
private double[] x;
private double[] y;
private double[] weight;
private int m;
private double[] coefficient;
public LeastSquareMethod(double[] x, double[] y, int m) {
if (x == null || y == null || x.length 2 || x.length != y.length
|| m 2)
throw new IllegalArgumentException("無效的參數(shù)");
this.x = x;
this.y = y;
this.m = m;
weight = new double[x.length];
for (int i = 0; i x.length; i++) {
weight[i] = 1;
}
}
public LeastSquareMethod(double[] x, double[] y, double[] weight, int m) {
if (x == null || y == null || weight == null || x.length 2
|| x.length != y.length || x.length != weight.length || m 2)
throw new IllegalArgumentException("無效的參數(shù)");
this.x = x;
this.y = y;
this.m = m;
this.weight = weight;
}
public double[] getCoefficient() {
if (coefficient == null)
compute();
return coefficient;
}
public double fit(double v) {
if (coefficient == null)
compute();
if (coefficient == null)
return 0;
double sum = 0;
for (int i = 0; i coefficient.length; i++) {
sum += Math.pow(v, i) * coefficient[i];
}
return sum;
}
private void compute() {
if (x == null || y == null || x.length = 1 || x.length != y.length
|| x.length m || m 2)
return;
double[] s = new double[(m - 1) * 2 + 1];
for (int i = 0; i s.length; i++) {
for (int j = 0; j x.length; j++)
s[i] += Math.pow(x[j], i) * weight[j];
}
double[] f = new double[m];
for (int i = 0; i f.length; i++) {
for (int j = 0; j x.length; j++)
f[i] += Math.pow(x[j], i) * y[j] * weight[j];
}
double[][] a = new double[m][m];
for (int i = 0; i m; i++) {
for (int j = 0; j m; j++) {
a[i][j] = s[i + j];
}
}
coefficient = Algorithm.multiLinearEquationGroup(a, f);
}
/**
* @param args
*/
public static void main(String[] args) {
LeastSquareMethod l = new LeastSquareMethod(
new double[] { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 },
new double[] { 37.84, 44.55, 45.74, 63.8, 76.67, 105.59, 178.48, 355.27, 409.92 },
new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19 },
2);
double[] x = l.getCoefficient();
for (double xx : x) {
System.out.println(xx);
}
System.out.println(l.fit(2009));
}
}
用正交多項(xiàng)式與一般的多項(xiàng)式擬合結(jié)果一樣,先前用正交多項(xiàng)式擬合的主要出發(fā)點(diǎn)是為了在求取多項(xiàng)式系數(shù)及進(jìn)行假設(shè)測驗(yàn)時(shí)時(shí)簡化計(jì)算,而這種簡化在matlab條件下已經(jīng)沒有意義了,用polyfit可以進(jìn)行任意多項(xiàng)式的擬合。大可不必再用正交多項(xiàng)式。
x2nbsp這其實(shí)就是一個(gè)多元線性回歸問題;2; nbsp,1);
x2=rand(10;
gt,用regress函數(shù)就可以了: x1=rand(10.8651其中前四行的作用是產(chǎn)生測試數(shù)據(jù),真正需要的只有最后一行代碼,得到的a依次為a1、a2,1);
a1=1,1)])
a =
ones(10,1)*0.1;
a=regress(y,[x1 a2=2;a0=3;1;y=a1*x1+a2*x2+a0+randn(10;nbsp.1448
nbsp.1743
2
最小二乘發(fā)擬合的是直線吧,不是曲線,非線性曲線擬合你的有模型,知道曲線的方程式。或者就是差值了,三次樣條或者B樣條。
我是高級(jí)Java從業(yè)者,數(shù)學(xué)功底一般般,不給你翻譯代碼了。這類問題一般用matlab的多,用java處理計(jì)算問題的比較少。你可以參考國外大學(xué)的一些在線研究成果。如MIT的很多項(xiàng)目都直接放在網(wǎng)上。
他山之石,可以攻玉。這里給你找了一個(gè)模擬擬合的網(wǎng)站:
文章名稱:java最小二乘算法代碼,最小二乘法編程計(jì)算
本文URL:http://chinadenli.net/article26/hesecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、做網(wǎng)站、關(guān)鍵詞優(yōu)化、自適應(yīng)網(wǎng)站、、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)