资源简介
public static void main(String args[]){
boolean flag1 = true;
while(flag1){
Scanner as = new Scanner(System.in);
System.out.println("-----------------------------------------------------");
System.out.println("-------------------插值法求解近似值-------------------");
System.out.println("-------------------请选择插值方法---------------------");
System.out.println("--------------------1、拉格朗日-----------------------");
System.out.println("--------------------2、牛顿插值-----------------------");
System.out.println("--------------------退出请按0-------------------------");
System.out.println("-----------------------------------------------------");
flag=as.nextInt();
if(flag == 0) {
flag1=false;
System.out.println("您已退出程序!欢迎下次使用。");
}
if(flag == 1){
int n = 1;
float x = 0;
System.out.println("请输入n:");
代码片段和文件信息
import java.util.Scanner;
public class Lagrange {
static int flag = 0;
public static float lagrange(float x0[]float y0[]int n float x) {
int ij;
float p[] = new float[100];
float y = 0;
for(i=0;i p[i] = y0[i];
for(j=0;j if(i!=j) p[i] *= (x-x0[j])/(x0[i]-x0[j]);
}
y += p[i];
}
return y;
}
public static void newton(float x0[]float y0[]int n) {
int ij;
float p[] = new float[100];
for(i=1;i p[0] = y0[i];
for(j=0;j p[j+1] = (p[j] - y0[j])/(x0[i] - x0[j]);
y0[i] = p[i];
}
}
}
public static void main(String args[]){
boolean flag1 = true;
while(flag1){
Scanner as = new Scanner(System.in);
System.out.println(“-----------------------------------------------------“);
System.out.println(“-------------------插值法求解近似值-------------------“);
System.out.println(“-------------------请选择插值方法---------------------“);
System.out.println(“--------------------1、拉格朗日-----------------------“);
System.out.println(“--------------------2、牛顿插值-----------------------“);
System.out.println(“--------------------退出请按0-------------------------“);
System.out.println(“-----------------------------------------------------“);
flag=as.nextInt();
if(flag == 0) {
flag1=false;
System.out.println(“您已退出程序!欢迎下次使用。“);
}
if(flag == 1){
int n = 1;
float x = 0;
System.out.println(“请输入n:“);
n = as.nextInt();
System.out.println(“请输入对应数组x0:“);
//System.out.println(n);
float x0[] = new float[n];
float y0[] = new float[n];
for(int i=0;i x0[i] = as.nextFloat();
}
System.out.println(“请输入对应数组y0:“);
for(int i=0;i y0[i] = as.nextFloat();
}
System.out.println(“请输入值x:“);
x = as.nextFloat();
System.out.println(“多项式解为:“+lagrange(x0y0nx));
}
if(flag == 2){
int i n = 0;
float x = 0b;
System.out.println(“请输入n:“);
n = as.nextInt();
System.out.println(“请输入对应数组x0:“);
//System.out.println(n);
float x0[] = new float[n];
float y0[] = new float[n];
for(i=0;i x0[i] = as.nextFloat();
}
System.out.println(“请输入对应数组y0:“);
for(i=0;i y0[i] = as.nextFloat();
}
System.out.println(“请输入x的值“);
x = as.nextFloat();
newton(x0 y0 n);
b = y0[n-1];
for(i=n-2;i>=0;i--){
b = b*(x - x0[i]) + y0[i];
}
System.out.println(“多项式解为:“+b);
//5
//x0=0.4 0.55 0.65 0.8 0.9
//y0=0.41075 0.5781 0.69675 0.88811 1.02652
//x=0.596 x=0.895
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3208 2010-12-02 00:37 Lagrange.java
----------- --------- ---------- ----- ----
3208 1
- 上一篇:java 求解非线性方程 二分法牛顿法弦截法
- 下一篇:矩阵乘法的java实现
评论
共有 条评论