📄 d5r7.cpp
字号:
#include "iostream.h"
#include "math.h"
double fder(double x)
{
//derivative of func
double aaa;
aaa = 4.0 * x * ((x * x) - 1.0) * sin(x);
return aaa + pow(x , 2) * ((x * x) - 2.0) * cos(x);
}
void chder(double a, double b, double c[], double cder[], int n)
{
int j;
double con;
cder[n] = 0.0;
cder[n - 1] = 2 * (n - 1) * c[n];
if (n >= 3)
{
for (j = n - 2; j>=1; j--)
{
cder[j] = cder[j + 2] + 2 * j * c[j + 1];
}
}
con = 2.0 / (b - a);
for (j = 1; j<=n; j++)
{
cder[j]= cder[j] * con;
}
}
double func(double x)
{
return (x * x) * (x * x - 2.0) * sin(x);
}
void chebft(double a, double b, double c[], int n)
{
int k,nmax = 50;
double f[51];
const double pi = 3.14159265358979;
double bma,bpa,y,sum,fac;
bma = 0.5 * (b - a);
bpa = 0.5 * (b + a);
for (k = 1; k<=n; k++)
{
y = cos(pi * (k - 0.5) / n);
f[k] = func(y * bma + bpa);
}
fac = 2.0 / n;
for (int j = 1; j<=n; j++)
{
sum = 0.0;
for (k = 1; k<=n; k++)
{
sum = sum + f[k] * cos((pi * (j - 1)) * ((k - 0.5) / n));
}
c[j] = fac * sum;
}
}
double chebev(double a, double b, double c[], int m, double x)
{
if ((x - a) * (x - b) > 0.0)
{
cout<<"x not in range."<<endl;
}
double d = 0.0;
double sv,y,y2,dd = 0.0;
y = (2.0 * x - a - b) / (b - a);
y2 = 2.0 * y;
for (int j = m; j>=2; j--)
{
sv = d;
d = y2 * d - dd + c[j];
dd = sv;
}
return y * d - dd + 0.5 * c[1];
}
void main()
{
//program d5r7
//driver for routine chder
int nval = 40;
double a,b,x,aaa,pio2 = 1.5707963;
double c[41], cder[41];
a = -pio2;
b = pio2;
chebft(a, b, c, nval);
//test derivative
cout<<endl;
cout<<"How many terms in chebyshev evaluation?"<<endl;
//input mval , between 6 and 40, mval=0 to end
int mval = 20;
cout<<mval<<endl;
if ((mval <= 0) || (mval > nval))
{
return;
}
chder(a, b, c, cder, mval);
cout<<" x Actual Cheby. deriv."<<endl;
cout.setf(ios::fixed|ios::right);
cout.precision(6);
for (int i = -8; i<=8; i++)
{
x = i * pio2 / 10.0;
cout.width(13);
cout<<x;
cout.width(13);
cout<<fder(x);
aaa = chebev(a, b, cder, mval, x);
cout.width(13);
cout<<aaa<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -