如果算的數(shù)小,可以放在int類型或者double類型的變量里.
鄒城網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),鄒城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鄒城1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的鄒城做網(wǎng)站的公司定做!
太大的,就要放到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
這個(gè)問題,我好像回答過!
/**
* 最小二乘法計(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));
}
}
最小二乘發(fā)擬合的是直線吧,不是曲線,非線性曲線擬合你的有模型,知道曲線的方程式?;蛘呔褪遣钪盗耍螛訔l或者B樣條。
因?yàn)橹蓝嘟M數(shù)據(jù),就是按照矩陣的方式來求.建立矩陣乘法之后,通過在兩邊同時(shí)乘以原矩陣的逆矩陣,然后就能得到三元方程組的解了.如果不會(huì)的話,可以上網(wǎng)查一下java實(shí)現(xiàn)矩陣乘法以及java實(shí)現(xiàn)逆矩陣..然后你的這個(gè)功能就完成了.
肯定的啊.第二個(gè)程序循環(huán)
for (int j=1;j==i;j++){
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
i=1時(shí),j=1,好吧,出來了1*1=1
j=2時(shí),i==j不成立了,所以j不++了.所以j永遠(yuǎn)是2了.永遠(yuǎn)不等于,所以不會(huì)打印了.
i=2,3,4,5,6,7,8,9時(shí)
j開始等于1,結(jié)果j永遠(yuǎn)不會(huì)等于i,所以j永遠(yuǎn)是1了,后面的也就不會(huì)執(zhí)行,不會(huì)打印了
我是高級(jí)Java從業(yè)者,數(shù)學(xué)功底一般般,不給你翻譯代碼了。這類問題一般用matlab的多,用java處理計(jì)算問題的比較少。你可以參考國外大學(xué)的一些在線研究成果。如MIT的很多項(xiàng)目都直接放在網(wǎng)上。
他山之石,可以攻玉。這里給你找了一個(gè)模擬擬合的網(wǎng)站: