资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java最小二乘法源代码 最小二乘法例子

利用java实现2^1,2^2,2^3,2^4......,代码!

如果算的数小,可以放在int类型或者double类型的变量里.

邹城网站建设公司成都创新互联,邹城网站设计制作,有大型网站制作公司丰富经验。已为邹城1000+提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的邹城做网站的公司定做!

太大的,就要放到BigInteger类型的变量里.

先看看放到int类型的变量代码

---------------------------------------------------------------

/*

* 数量比较小的情况下可以用这个来计算

* 太大了就存放不了了

*/

public class PowerNumber {

public static void main(String[] args) {

PowerNumber power = new PowerNumber();

// 准备求第几项的值

int n = 10;

// 这里先打印第4项

System.out.println("第" + n + "项结果是:" + power.powerNumberN(n));

// 这里打印前4项的和

System.out.println("前" + n + "项的和是:" + power.powerNumberSum(n));

}

// 计算第n项的值

int powerNumberN(int n) {

// 用来存放第n个数

int sum = 1;

for (int i = 1; i = n; i++) {

sum = 2 * sum;

}

return sum;

}

// 计算前n项的和

int powerNumberSum(int n) {

// 存放前n项的和

int sumNum = 0;

int frontN = n;

for (int i = 1; i = n; i++) {

// 存放第n项的值

int sum = 1;

for (int j = 1; j = frontN; j++) {

// 第n项的值

sum = 2 * sum;

}

//求前一项

frontN--;

// 求出的值加到总和里

sumNum = sumNum + sum;

}

return sumNum;

}

}

------------------------------------------------------

前4项的结果:

第4项结果是:16

前4项的和是:30

--------------------

前20项的结果:

第20项结果是:1048576

前20项的和是:2097150

----------------------

前40项的结果......

第40项结果是:0

前40项的和是:-2

================================================

再看看放到BigInteger类型的变量里的程序

---------------------------------

import java.math.BigInteger;

public class Power {

public static void main(String[] args) {

Power power = new Power();

// 准备求第几项的值

int n = 4;

// 这里先打印第4项

System.out.println("第" + n + "项结果是:" + power.powerNum(n));

// 这里打印前4项的和

System.out.println("前" + n + "项的和是:" + power.sumPowerNum(n));

}

// 计算第n项的值

BigInteger powerNum(int n) {

BigInteger num = BigInteger.ONE;

for (int i = 1; i = n; i++) {

// 为了防止结果过大,将结果放在BigInteger中

// 每次对结果乘以2

num = num.multiply(new BigInteger(new Integer(2).toString()));

}

// 打印2^n结果

return num;

}

// 计算前n项的值

BigInteger sumPowerNum(int n) {

// 存放前n项的值

BigInteger sumNum = BigInteger.ZERO;

int frontN = n;

for (int i = 1; i = n; i++) {

// 存放第n项的值

BigInteger num = BigInteger.ONE;

for (int j = 1; j = frontN; j++) {

// 为了防止结果过大,将结果放在BigInteger中

// 每次对结果乘以2

num = num.multiply(new BigInteger(new Integer(2).toString()));

}

// 每次循环让最大值减掉1,以计算前面的值

frontN--;

// 计算出第n项的值,将其放入总和sumNum中

sumNum = sumNum.add(num);

}

return sumNum;

}

}

---------------------------------------------------

前4项的结果:

第4项结果是:16

前4项的和是:30

--------------------------------------------------

前40项的结果:

第40项结果是:1099511627776

前40项的和是:2199023255550

---------------------------------------------------------------------------------------------

前400项的结果:

第400项结果是:2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493376

前400项的和是:5164499756173817179311838344006023748659411585658447025661318713081295244033682389259290706560275662871806343945494986750

-------------------------------------------------------------------------------------

前4000项的结果:

第4000项结果是:13182040934309431001038897942365913631840191610932727690928034502417569281128344551079752123172122033140940756480716823038446817694240581281731062452512184038544674444386888956328970642771993930036586552924249514488832183389415832375620009284922608946111038578754077913265440918583125586050431647284603636490823850007826811672468900210689104488089485347192152708820119765006125944858397761874669301278745233504796586994514054435217053803732703240283400815926169348364799472716094576894007243168662568886603065832486830606125017643356469732407252874567217733694824236675323341755681839221954693820456072020253884371226826844858636194212875139566587445390068014747975813971748114770439248826688667129237954128555841874460665729630492658600179338272579110020881228767361200603478973120168893997574353727653998969223092798255701666067972698906236921628764772837915526086464389161570534616956703744840502975279094087587298968423516531626090898389351449020056851221079048966718878943309232071978575639877208621237040940126912767610658141079378758043403611425454744180577150855204937163460902512732551260539639221457005977247266676344018155647509515396711351487546062479444592779055555421362722504575706910949376

前4000项的和是:26364081868618862002077795884731827263680383221865455381856069004835138562256689102159504246344244066281881512961433646076893635388481162563462124905024368077089348888773777912657941285543987860073173105848499028977664366778831664751240018569845217892222077157508155826530881837166251172100863294569207272981647700015653623344937800421378208976178970694384305417640239530012251889716795523749338602557490467009593173989028108870434107607465406480566801631852338696729598945432189153788014486337325137773206131664973661212250035286712939464814505749134435467389648473350646683511363678443909387640912144040507768742453653689717272388425750279133174890780136029495951627943496229540878497653377334258475908257111683748921331459260985317200358676545158220041762457534722401206957946240337787995148707455307997938446185596511403332135945397812473843257529545675831052172928778323141069233913407489681005950558188175174597936847033063252181796778702898040113702442158097933437757886618464143957151279754417242474081880253825535221316282158757516086807222850909488361154301710409874326921805025465102521079278442914011954494533352688036311295019030793422702975092124958889185558111110842725445009151413821898750

已知一组数据,用JAVA JFRAME利用最小二乘法求出该组数据的多项式拟合公式

这个问题,我好像回答过!

/**

* 最小二乘法计算类

*

* @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("无效的参数");

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("无效的参数");

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));

}

}

谁能给一个java编写的利用最小二乘法进行曲线拟合的算法?

最小二乘发拟合的是直线吧,不是曲线,非线性曲线拟合你的有模型,知道曲线的方程式。或者就是差值了,三次样条或者B样条。

最小二乘法,y=a0+a1*x1+a2*x2,要求使用java编写

因为知道多组数据,就是按照矩阵的方式来求.建立矩阵乘法之后,通过在两边同时乘以原矩阵的逆矩阵,然后就能得到三元方程组的解了.如果不会的话,可以上网查一下java实现矩阵乘法以及java实现逆矩阵..然后你的这个功能就完成了.

java 九九乘法表

肯定的啊.第二个程序循环

for (int j=1;j==i;j++){

System.out.print(i+"*"+j+"="+(i*j)+"\t");

}

i=1时,j=1,好吧,出来了1*1=1

j=2时,i==j不成立了,所以j不++了.所以j永远是2了.永远不等于,所以不会打印了.

i=2,3,4,5,6,7,8,9时

j开始等于1,结果j永远不会等于i,所以j永远是1了,后面的也就不会执行,不会打印了

谁能帮忙用java解这个最小二乘法建立的求参数的方程,已知x1,x2,y。求参数a,b1,b2三个参数的值

我是高级Java从业者,数学功底一般般,不给你翻译代码了。这类问题一般用matlab的多,用java处理计算问题的比较少。你可以参考国外大学的一些在线研究成果。如MIT的很多项目都直接放在网上。

他山之石,可以攻玉。这里给你找了一个模拟拟合的网站:


网页名称:java最小二乘法源代码 最小二乘法例子
分享地址:http://cdkjz.cn/article/hgidip.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220