その1
#include < stdio.h >
main(){
int i=1,x;
for(x=1; x<70 ;x++){
i=i*2;
printf("%d %d \n" ,i,x);
}
}
その2
#include < stdio.h >
main(){
int count=1;
int x=1;
int y=0;
while (y !=x){
y=x;
x=x*2;
printf("%d\n",count,x);
count++;
}
}
while :繰り返しの記号
y !=x :y=xでないならば count :番号を付けていく xとyが一致しない限り繰り返されるプログラム。計算機が計算できる範囲を超えたらx=yとなるのでここで計算が止まる。
2009年7月30日木曜日
2009年7月29日水曜日
n項まで計算できるsinのテイラー展開のプログラムです
#include < stdio.h >
#include < math.h >
main(){
char FileName[124];
FILE *fp;
int i, j, k, n, m, e;
float a, b, c, d;
double x;
float dx, s;
x = 0.0e0;
dx = 0.1e0;
x = -dx;
b = 0;
printf("結果の保存先ファイル名を入力してください>");
scanf("%s" , FileName);
fp = fopen(FileName , "w");
printf("テイラー展開のn項まで計算します。nを入力してください>");
scanf("%d", &n);
for(e=1; e<=70; e++){
x = x + dx;
for(m=1; m<=n*2; m = m + 4){
b=0;
for(i=1; i<=m; i = i + 4){
a = 1.0;
for(j=1; j<=i; j++){
a = a * 1/j;
}
a = a * pow(x,i);
b = b + a;
}
}
for(m=3; m<=n*2; m = m + 4){
c = 0;
for(i=3; i<=m; i = i + 4){
a = 1.0;
for(j=1; j<=i; j++){
a = a * 1/j;
}
a = a * pow(x,i);
c = c + a;
}
c = -c;
}
d = b + c;
printf("%d %f %e\n", e, x, d);
fprintf(fp,"%d %f %e\n", e, x, d);
}
fclose(fp);
return 0;
}
実行したら、最初に実行結果を出力するファイル名を記入してください。
その後に、n項まで計算するための"n"を入力してください。
#include < math.h >
main(){
char FileName[124];
FILE *fp;
int i, j, k, n, m, e;
float a, b, c, d;
double x;
float dx, s;
x = 0.0e0;
dx = 0.1e0;
x = -dx;
b = 0;
printf("結果の保存先ファイル名を入力してください>");
scanf("%s" , FileName);
fp = fopen(FileName , "w");
printf("テイラー展開のn項まで計算します。nを入力してください>");
scanf("%d", &n);
for(e=1; e<=70; e++){
x = x + dx;
for(m=1; m<=n*2; m = m + 4){
b=0;
for(i=1; i<=m; i = i + 4){
a = 1.0;
for(j=1; j<=i; j++){
a = a * 1/j;
}
a = a * pow(x,i);
b = b + a;
}
}
for(m=3; m<=n*2; m = m + 4){
c = 0;
for(i=3; i<=m; i = i + 4){
a = 1.0;
for(j=1; j<=i; j++){
a = a * 1/j;
}
a = a * pow(x,i);
c = c + a;
}
c = -c;
}
d = b + c;
printf("%d %f %e\n", e, x, d);
fprintf(fp,"%d %f %e\n", e, x, d);
}
fclose(fp);
return 0;
}
実行したら、最初に実行結果を出力するファイル名を記入してください。
その後に、n項まで計算するための"n"を入力してください。
2009年7月27日月曜日
2009年7月21日火曜日
2009年7月16日木曜日
3-4-3. gnuplotの使い方
使用例
ファイルreの2行目をx3行目をyとしてグラフを書く
astro-PBG4-12:~/tmp habe$ gnuplot <----- gnuplotの起動
G N U P L O T
Version 4.0 patchlevel 0
last modified Thu Apr 15 14:44:22 CEST 2004
System: Darwin 8.11.0
Terminal type set to 'x11'
gnuplot> plot "re"
gnuplot> plot "re" u 2:3
gnuplot> plot "re" u 2:3 with line
gnuplot> set yrange[-1:1]
gnuplot> plot "re" u 2:3 with line
gnuplot> exit
ファイルreの2行目をx3行目をyとしてグラフを書く
astro-PBG4-12:~/tmp habe$ gnuplot <----- gnuplotの起動
G N U P L O T
Version 4.0 patchlevel 0
last modified Thu Apr 15 14:44:22 CEST 2004
System: Darwin 8.11.0
Terminal type set to 'x11'
gnuplot> plot "re"
gnuplot> plot "re" u 2:3
gnuplot> plot "re" u 2:3 with line
gnuplot> set yrange[-1:1]
gnuplot> plot "re" u 2:3 with line
gnuplot> exit
登録:
投稿 (Atom)

