⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d2r1.java

📁 用JAVA写的数值计算方法中的拉格朗日插值算法的程序
💻 JAVA
字号:
import java.text.*;
public class d2r1
{
   public static void main(String[] args)
   {
	
	//river for routine polint
	int nfunc,i,n=9;
	double xa[]=new double[11];
	double ya[]=new double[11];
	double x=0.0;
	double f=0.0;
	double y=0.0;
	double dy=0.0;
	n=9;  //输入插值节点个数
	double pi=3.1415926;
	d2r1F g=new d2r1F();
	DecimalFormat form=new DecimalFormat("0.000000");
	System.out.println("Generation of interpolation tables;");
	System.out.println("...sin(x) 0<x<pi");
	System.out.println("...exp(x) 0<x<1");
	System.out.println("How many entries go in these tables?(note:n<10)");
	System.out.println(n);
	for ( nfunc=1 ; nfunc<=2 ; nfunc++)
	{
		if(nfunc==1)
		{
			System.out.println("sine function from 0 to pi");
			for ( i= 1; i<= n ; i++)
			{
				xa[i]=(double)i*pi/n;
				ya[i]=Math.sin(xa[i]);
			}
		}
		else 
		{
			if (nfunc ==2)
			{
				System.out.println("exponential function from 0 to 1");
			    for(i=1; i<=n ; i++)
			    {
			    	xa[i]=(double)i*1/n;
			    	ya[i]=Math.exp(xa[i]);
			    }
			}
			
			 else   return;
		}
		
		System.out.println("x   f(x)   interpolated   error");
		for (i=1; i<=10; i++)
		{
			if(nfunc==1)
			{
				x=(-0.05+(double) i/10)*pi;
				f=Math.sin(x);
				
			}
			else if (nfunc == 2)
			{
				x=(-0.05+(double)i/10);
				f=Math.exp(x);
				
			}
			g.polint(xa,ya,n,x,y,dy);
			System.out.print(""+form.format(x));
			System.out.print(""+form.format(f));
			System.out.print(""+form.format(g._y));
			System.out.println(""+form.format(g._dy));
		
		}
	}
	
   }
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -