?? d10r5.cpp
字號:
#include <process.h>
#include <math.h>
#include <iomanip.h>
#include <iostream.h>
double bessj0(double x)
{
double p1,p2,p3,p4,p5,q1,q2,q3,q4,q5;
double r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,s5,s6;
double y,bbb,ccc,aaa,temp,eee,ddd,ax,xx,z;
p1 = 1.0; p2 = -0.001098628627;
p3 = 0.00002734510407; p4 = -0.000002073370639;
p5 = 2.093887211e-07;
q1 = -0.01562499995; q2 = 0.0001430488765;
q3 = -0.000006911147651; q4 = 7.621095161e-07;
q5 = -9.34945152e-08;
r1 = 57568490574.0; r2 = -13362590354.0;
r3 = 651619640.7; r4 = -11214424.18;
r5 = 77392.33017; r6 = -184.9052456;
s1 = 57568490411.0; s2 = 1029532985.0;
s3 = 9494680.718; s4 = 59272.64853;
s5 = 267.8532712; s6 = 1.0;
if (fabs(x) < 8.0)
{
y = x * x;
bbb = y * (r4 + y * (r5 + y * r6));
aaa = r1 + y * (r2 + y * (r3 + bbb));
ccc = y * (s3 + y * (s4 + y * (s5 + y * s6)));
temp = aaa / (s1 + y * (s2 + ccc));
}
else
{
ax = fabs(x);
z = 8.0 / ax;
y = z * z;
xx = ax - 0.785398164;
ccc = y * (p3 + y * (p4 + y * p5));
aaa = p1 + y * (p2 + ccc);
ddd = y * (q3 + y * (q4 + y * q5));
eee = z * sin(xx) * (q1 + y * (q2 + ddd));
temp = sqrt(0.636619772 / ax) * (cos(xx) * aaa - eee);
}
return temp;
}
double func(double x)
{
double t;
t=bessj0( x);
return t;
}
void zbrak(double x1, double x2, int n, double xb1[], double xb2[], int& nb)
{
int nbb,i;
double dx,x,fc,fp;
nbb = nb;
nb = 0;
x = x1;
dx = (x2 - x1)/ n;
fp = func(x);
for (i = 1 ;i<=n;i++)
{
x = x + dx;
fc = func(x);
if ((fc * fp) < 0.0 )
{
nb = nb + 1;
xb1[nb] = x - dx;
xb2[nb] = x;
}
fp = fc;
if (nbb == nb) exit(1);
}
}
double rtflsp(double x1, double x2, double xacc)
{
int maxit,j;
double fl,fh,xl,xh,swap,dx,temp,f,del;
maxit = 30;
fl = func(x1);
fh = func(x2);
if (fl * fh > 0)
{
cout<< "root must be bracketed for false position"<<endl;
}
if (fl < 0)
{
xl = x1;
xh = x2;
}
else
{
xl = x2;
xh = x1;
swap = fl;
fl = fh;
fh = swap;
}
dx = xh - xl;
for (j = 1; j<=maxit; j++)
{
temp = xl + dx * fl / (fl - fh);
f = func(temp);
if (f < 0)
{
del = xl - temp;
xl = temp;
fl = f;
}
else
{
del = xh - temp;
xh = temp;
fh = f;
}
dx = xh - xl;
if ((fabs(del) < xacc) || (f == 0)) break;
}
return temp;
cout<< "rtflsp exceed maximum iterations";
}
void main()
{
//program d10r5
//driver for routine rtflsp
int n,nbmax,nb,i;
double x1,x2,xacc,root,xb1[20], xb2[20];
n = 100;
nbmax = 20;
x1 = 1.0;
x2 = 50.0;
nb = nbmax;
zbrak(x1, x2, n, xb1, xb2, nb);
cout<<endl;
cout<< "Roots of Bessj0:"<<endl;
cout<<endl;
cout<< " x f(x)"<<endl;
for (i = 1; i<=nb; i++)
{
xacc = (0.0000010) * (xb1[i] + xb2[i]) / 2.0;
root = rtflsp(xb1[i], xb2[i], xacc);
cout<< setw(6)<<"root "<<i;
cout<< setw(12)<<root;
cout<< setw(18)<<bessj0(root)<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -