/**
创新互联公司服务项目包括向阳网站建设、向阳网站制作、向阳网页制作以及向阳网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,向阳网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到向阳省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
* 最小二乘法计算类
*
* @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));
}
}
int i; 这里的i就是一个变量。它是对应于常量来说的。
在java里不是叫函数,是叫方法。
比如public String getHelloWorld(String str);这就是一个方法。
函数的自变量就是函数内部定义的一个变量。也叫局部变量,它只在函数的内部被使用。
比如public void getHelloWorld(String str){
String strHelloWorld = str;这里定义的strHelloWorld就是一个自变量
}
上面的str就是函数的参数。
你好!
以下是我的代码:
clc
x=linspace(0,4*pi,30);
N=[2,3,4,6];
hold on
y=cos(x);
plot(x,y,'r.')
p2=polyfit(x,y,2);
y2=polyval(p2,x);
plot(x,y2,'color','b')
hold on
p3=polyfit(x,y,3);
y3=polyval(p3,x);
plot(x,y3,'color','g')
p4=polyfit(x,y,4);
y4=polyval(p4,x);
plot(x,y4,'color','m')
p6=polyfit(x,y,6);
y6=polyval(p6,x);
plot(x,y6,'color','c')
legend('原函数','N=2','N=3','N=4','N=6')
我的计算图片:
N=2,N=3重合了!!!
package mainjava;
import java.util.Scanner;
public class mihanshu {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
System.out.println(mihanshu(x,n));
}
public static int mihanshu(int x,int n)
{
if(n == 0)
{
return 1;
}
return x*mihanshu(x,n-1);
}
}
用递归解决,诸如y = x^n 每次调用n都减一,当n = 0时,返回1。即可 如果还有不懂,可追问,我现在给你编写一下,稍等。