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

📄 d2r4.cpp

📁 Visual C++ 常用数值算法集 源代码
💻 CPP
字号:
# include<iostream.h>
# include<math.h>
# include<iomanip.h>

void main()
{
    //program d2r4
    //driver for routine splint,which calls spline
    int nfunc,i,np;
    double xa[11], ya[11], y2[11],pi,yp1,ypn,x,f,y;
	np = 10;
    pi = 3.141593;
    for( nfunc = 1; nfunc<=2; nfunc++)
	{
        if (nfunc == 1 )
		{
            cout<<endl;
            cout<<"Sine function from 0 to pi"<<endl;
            for (i = 1;i<=np;i++)
			{
                xa[i] = double(i * pi) /double( np);
                ya[i] = sin(xa[i]);
            }
            yp1 = cos(xa[1]);
            ypn = cos(xa[np]);
		}
        else 
		{
			if (nfunc == 2)
			{
				cout<<endl;
				cout<<"Exponential function from 0 to 1"<<endl;
				for (i = 1;i<=np;i++)
				{
					xa[i] = double(1* i) /double(np);
					ya[i] = exp(xa[i]);
				}
				yp1 = exp(xa[1]);
				ypn = exp(xa[np]);
			}
           
		}
		//call spline to get second derivatives
		spline(xa, ya, np, yp1, ypn, y2);
		//call splint for interpolations
		cout<< "       x            f(x)     interpolation"<<endl;
		for (i = 1; i<=10; i++)
		{
			if (nfunc == 1)
			{
				x = (-0.05 + double(i) / 10.0) * pi;
				f = sin(x);
			}
			else
			{
				if( nfunc == 2 )
				{
					x = (-0.05 + double(i) / 10.0);
					f = exp(x);
				}
			}
			splint(xa, ya, y2, np, x, y);
			cout<<setprecision(6)<<setiosflags(ios::fixed);
			cout<<setw(12)<<x;
			cout<<setw(14)<<f;
			cout<<setw(14)<<y<<endl;
		}
        cout<<"**********************************"<<endl;
    }
}

⌨️ 快捷键说明

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