#include stdio.h
10年積累的網(wǎng)站設(shè)計制作、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有烏魯木齊免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
int main()
{
double x[4] = {0, 0, 0, 0};
double a[4][4] = {1, 2, 4, 8, 1, 3, 9, 27, 1, 4, 16, 64, 1, 5, 25, 125};
double y[4] = {10, 26, 58, 112};
double d[4][4], g[4];
int round = 5, i,j;
for (i=0; i4; ++i) {
g[i] = y[i] / a[i][i];
for (j=0; j4; ++j) {
d[i][j] = i==j ? 0 : -a[i][j]/a[i][i];
}
}
while (round--) {
for (i=0; i4; ++i) {
x[i] = g[i];
for (j=0; j4; ++j) {
x[i] += d[i][j] * x[j];
}
printf("%lf " , x[i]);
}
printf("\n");
}
}
高斯函數(shù)公式:f(x)=d*ad。高斯函數(shù)以大數(shù)學(xué)家約翰·卡爾·弗里德里希·高斯的名字命名。高斯函數(shù)應(yīng)用范圍很廣,在自然科學(xué)、社會科學(xué)、數(shù)學(xué)以及工程學(xué)等領(lǐng)域都能看到它的身影。
函數(shù)(function)的定義通常分為傳統(tǒng)定義和近代定義,函數(shù)的兩個定義本質(zhì)是相同的,只是敘述概念的出發(fā)點不同,傳統(tǒng)定義是從運動變化的觀點出發(fā),而近代定義是從集合、映射的觀點出發(fā)。
下面共有兩個程序,程序2 加入了圖形顯示
程序1
這個程序就是你要的。
# include "stdio.h"
# include "math.h"
# include "stdlib.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000 /*這個值為N可以定義的最大長度*/
# define N 100 /*產(chǎn)生隨機序列的點數(shù),注意不要大于MAX_N*/
/*產(chǎn)生均勻分布的隨機變量*/
void randa(float *x,int num);
/*產(chǎn)生瑞利分布的隨機變量*/
void randr(float *x,int num);
/*產(chǎn)生標(biāo)準(zhǔn)高斯分布的隨機變量*/
void randn(float *x,int num);
/*產(chǎn)生萊斯分布的隨機變量*/
void randl(float *x, float a, float b, int num);
void fshow(char *name,float *x,int num);
main()
{
float x[N];
int i;
/*
randa(x,N);
randr(x,N);
randl(x,10,10,N);
*/
randn(x,N);
/*此時x[N]就是所需要的高斯分布的序列*/
/*顯示該序列*/
fshow("x",x,N);
getch();
}
void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}
void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("\n");
printf(name);
printf(" = ");
L=6;
/*按照每行6個數(shù)據(jù)的格式顯示*/
for(i=0;inum;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("\n");
if(x[i]0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}
程序 2
以下程序加入了圖形顯示的效果,因此更加直觀,你可以參考一下。
/* 作者 Leo_nanjing
時間 2008.5.10
功能 生成各種分布的隨機變量,并顯示
*/
# include "stdio.h"
# include "math.h"
# include "graphics.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000
# define N 1000
void randa(float *x,int num);
void randr(float *x,int num);
void randn(float *x,int num);
void randl(float *x, float a, float b, int num);
void fshow(char *name,float *x,int num);
/*用于圖形顯示的部分*/
void init_graphic(unsigned color);
void plotxy(float *x, float *y, int num,int mode);
void plot(float *y,int num, int mode);
float max(float *x, int num);
float min(float *x, int num);
/*畫出該隨機序列的分布函數(shù)曲線*/
void plotpdf(float *x,int num,int part,int mode);
main()
{
float x[N];
int i;
randn(x,N);
fshow("x",x,N);
getch();
/*以下為圖形顯示部分*/
init_graphic(0);
/*顯示隨機序列*/
plot(x,N,1);
getch();
/*顯示其分布函數(shù)*/
plotpdf(x,N,20,0);
getch();
}
void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}
void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;inum;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("\n");
printf(name);
printf(" = ");
L=6;
for(i=0;inum;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("\n");
if(x[i]0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}
/*以下為圖形顯示的函數(shù)*/
void init_graphic(unsigned color)
{
int graphicdriver,graphicmode;
graphicdriver=DETECT;
graphicmode=1;
initgraph(graphicdriver,graphicmode,"E:\\turboc2\\");
setbkcolor(color);
}
void plotxy(float *x, float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
clrscr(0);
cleardevice();
setbkcolor(0);
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;inum;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("\n\n");
printf("%f",max_y);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\n");
printf("%f",(max_y+min_y)/2);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n");
printf("%f",min_y);
printf("\n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);
}
void plot(float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
float x[MAX_N];
clrscr(0);
cleardevice();
setbkcolor(0);
for(i=0;inum;i++) x[i]=i+1;
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;inum;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("\n\n");
printf("%f",max_y);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\n");
printf("%f",(max_y+min_y)/2);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n");
printf("%f",min_y);
printf("\n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);
}
void plotpdf(float *x,int num,int part,int mode)
{
int i,j;
float max_x,min_x,round,deltax,up,down,sum;
float xl[MAX_N],yl[MAX_N];
sum=0;
max_x=max(x,num);
min_x=min(x,num);
round=max_x-min_x;
deltax=round/part;
xl[0]=min_x;
for(i=1;i=part;i++)
{
xl[i]=min_x+deltax*i;
yl[i-1]=0;
up=xl[i];
down=xl[i-1];
for(j=0;jnum;j++)
{
if((x[j]up) (x[j]=down)) yl[i-1]=yl[i-1]+1;
}
yl[i-1]=yl[i-1]/num/deltax;
}
for(i=0;ipart;i++) sum=sum+yl[i];
plotxy(xl,yl,part,mode);
}
float max(float *x, int num)
{
int i;
float max;
max=x[0];
for(i=1;inum;i++)
{
if(x[i]max) max=x[i];
}
return max;
}
float min(float *x, int num)
{
int i;
float min;
min=x[0];
for(i=1;inum;i++)
{
if(x[i]min) min=x[i];
}
return min;
}
數(shù)學(xué)函數(shù)庫,一些數(shù)學(xué)計算的公式的具體實現(xiàn)是放在math.h里,具體有:
1、 三角函數(shù)
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函數(shù)
double asin (double); 結(jié)果介于[-PI/2,PI/2]
double acos (double); 結(jié)果介于[0,PI]
double atan (double); 反正切(主值),結(jié)果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圓值),結(jié)果介于[-PI,PI]
3 、雙曲三角函數(shù)
double sinh (double);
double cosh (double);
double tanh (double);
4 、指數(shù)與對數(shù)
double frexp(double value,int *exp);這是一個將value值拆分成小數(shù)部分f和(以2為底的)指數(shù)部分exp,并返回小數(shù)部分f,即f*2^exp。其中f取值在0.5~1.0范圍或者0。
double ldexp(double x,int exp);這個函數(shù)剛好跟上面那個frexp函數(shù)功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小數(shù)部分,iptr指向整數(shù)部分。
double log (double); 以e為底的對數(shù)
double log10 (double);以10為底的對數(shù)
double pow(double x,double y);計算以x為底數(shù)的y次冪
float powf(float x,float y); 功能與pow一致,只是輸入與輸出皆為浮點數(shù)
double exp (double);求取自然數(shù)e的冪
double sqrt (double);開平方
5 、取整
double ceil (double); 取上整,返回不比x小的最小整數(shù)
double floor (double); 取下整,返回不比x大的最大整數(shù),即高斯函數(shù)[x]
6 、絕對值
int abs(int i); 求整型的絕對值
double fabs (double);求實型的絕對值
double cabs(struct complex znum);求復(fù)數(shù)的絕對值
7 、標(biāo)準(zhǔn)化浮點數(shù)
double frexp (double f,int *p); 標(biāo)準(zhǔn)化浮點數(shù),f = x * 2^p,已知f求x,p (x介于[0.5,1])
double ldexp (double x,int p); 與frexp相反,已知x,p求f
8 、取整與取余
double modf (double,double*); 將參數(shù)的整數(shù)部分通過指針回傳,返回小數(shù)部分
double fmod (double,double); 返回兩參數(shù)相除的余數(shù)