1、二、算法框图三、算法程序class Interpolationpublic: Interpolation(int num, double x1, double x2, double func); double ComputeForwardValue(double x); / compute forward interpolation value Interpolation();private: void GetForwardTable(); / get the forward differential table int m_num; / the number of interpolation
2、 points double m_x1, m_x2; / the first point m_x1 and last point m_x2 double m_step; / the interpolation step double* m_func; / the function value of interpolation points double* m_ftable; / the forward differential table;#include limitsusing namespace std;#define NUM 11/上?面?输入需要a多少个?样本#define MIN 0
3、区?间?的最小?值#define MAX 10大int main() /下?y的 double funcNUM= 0,1,4,9,16,25,36,49,64,81,100 ; /上 double x1=MIN, x2=MAX, x; int num=NUM; char flag=Y; Interpolation test(num, x1, x2, func); while(flag=) coutx; if (!cin) / checking failure state cin.clear(); / clear failure tag cin.ignore(numeric_limits:max
4、(), n); / clear input buffer continue; if(xx2) -Invalid input:x-endl;Only the number between x1 and x2 is valid. else Forward interpolation value:test.ComputeForwardValue(x+0.001) coutendlflag; return 0;Interpolation:Interpolation(int num, double x1, double x2, double func) m_num = num; m_x1 = x1; m
5、_x2 = x2; m_step = (m_x2-m_x1)/(num-1); m_func = new doublem_num; m_ftable = new doublem_num; for (int i=0; i=i; -j) m_ftablej = m_ftablej-m_ftablej-1;double Interpolation:ComputeForwardValue(double x) double* coef; /coefficient talbe double result, t; int i; coef = new doublem_num; t = (x-m_x1)/m_s
6、tep; for (i=1, coef0=1; +i) /compute the coefficient table coefi = coefi-1*(t-i+1)/i; for (i=0, result=0; result += m_ftablei*coefi; delete coef; return result;四、算法实现例1.当时求在范围内任意的值。解:(1)在程序中输入11个样本: 0,1,4,9,16,25,36,49,64,81,100、且输入MIN:0,MAX:10,样本容量:11。(2)在运行程序中输入范围内的任意的值,如:1.2,1.4等。(3)程序提示答案和提示是否继续,是则输入大写Y,否则输入大写N。例2.当给出15个 : 1.38, 1.48, 1.58, 1.69, 1.81, 1.94, 2.10, 2.28, 2.50, 2.76, 3.06, 3.41, 3.83, 4.33, 4.93,且给出范围:求出任意(1)给出15个题目上显示的值:4000,MAX:11000,样本容量:15。1001,5200等。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1