matlab实验
任务概述
给定函数f(x)在m个采样点处的值f(xi)以及每个点的权重wi,求曲线拟合的正交多项式Pn(x)满足最小二乘误差||err||2=∑mi=1wi[f(xi)−Pn(xi)]2《TOL。
函数接口定义:
int OPA( double (*f)(double t), int m, double x[], double w[], double c[], double *eps );1
裁判程序样例:
#include 《stdio.h》
#include 《math.h》
#define MAX_m 200
#define MAX_n 5
double f1(double x)
{
return sin(x);
}
double f2(double x)
{
return exp(x);
}
int OPA( double (*f)(double t), int m, double x[], double w[], double c[], double *eps );
void print_results( int n, double c[], double eps)
{
int i;
printf(“%d\n”, n);
for (i=0; i《=n; i++)
printf(“%12.4e ”, c[i]);
printf(“\n”);
printf(“error = %9.2e\n”, eps);
printf(“\n”);
}
int main()
{
int m, i, n;
double x[MAX_m], w[MAX_m], c[MAX_n+1], eps;
m = 90;
for (i=0; i《m; i++) {
x[i] = 3.1415926535897932 * (double)(i+1) / 180.0;
w[i] = 1.0;
}
eps = 0.001;
n = OPA(f1, m, x, w, c, &eps);
print_results(n, c, eps);
m = 200;
for (i=0; i《m; i++) {
x[i] = 0.01*(double)i;
w[i] = 1.0;
}
eps = 0.001;
n = OPA(f2, m, x, w, c, &eps);
print_results(n, c, eps);
return 0;
}
/* Your function will be put here */
输出样例:
3
-2.5301e-03 1.0287e+00 -7.2279e-02 -1.1287e-01
error = 6.33e-05
4
1.0025e+00 9.6180e-01 6.2900e-01 7.0907e-03 1.1792e-01
error = 1.62e-04
全部0条评论
快来发表一下你的评论吧 !