teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

誤字

2023/01/16 12:47

投稿

riui
riui

スコア4

title CHANGED
@@ -1,1 +1,1 @@
1
- 磁力の関係についてのシュミレーションができません。
1
+ 磁力の関係についてのシュミレーションができません。(訂正版)
body CHANGED
File without changes

2

誤字

2023/01/16 12:45

投稿

riui
riui

スコア4

title CHANGED
File without changes
body CHANGED
@@ -6,12 +6,13 @@
6
6
  ```> ### C
7
7
  #include<stdio.h>
8
8
  #include<stdlib.h>
9
+ #include<math.h>
9
- #define N(20)
10
+ #define N (20)
10
11
 
11
12
  int main(){
12
13
  double J = 2.0e-21;
13
- double E;
14
+ double E1,E2,T,P,kbT,N;
14
- int i,j,n,N;
15
+ int i,j,n,p;
15
16
  int m[N][N];
16
17
 
17
18
  //磁化を代入する
@@ -23,20 +24,35 @@
23
24
  m[i][j]=(rand()%2)*2-1;
24
25
 
25
26
  //ランダム磁化②
26
- if((double)rand() RAND_MAX > 0.5)m[i][j]-1;
27
+ if((double)rand() / RAND_MAX > 0.5)m[i][j] = 1;
27
28
  else m[i][j]=-1;
28
29
  }
29
30
  }
30
31
  //エネルギー計算
31
- E=0.0;
32
+ E1=0.0;
32
33
  for(j=0;j<N;j++){
33
34
  for(i=0;i<N;i++){
34
- if(i < N-1)E += -J*m[i][j]*m[i+1][j];
35
+ if(i < N-1)E1 += -J*m[i][j]*m[i+1][j];
35
- if(j < N-1)E += -J*m[i][j]*m[i][j+1];
36
+ if(j < N-1)E1 += -J*m[i][j]*m[i][j+1];
36
37
  }
37
38
  }
39
+ //ランダムで磁化を変化させるサイトを決定
40
+ i = rand()%20;
41
+ j = rand()%20;
42
+ m[i][j] = -m[i][j];
43
+ //エネルギー計算
44
+ E2=0.0;
45
+ for(j=0;j<N;j++){
46
+ for(i=0;i<N;i++){
47
+ if(i < N-1)E2 += -J*m[i][j]*m[i+1][j];
48
+ if(j < N-1)E2 += -J*m[i][j]*m[i][j+1];
49
+ }
50
+ }
51
+ for(T=10;T<500;T=T+10){
52
+ kbT = kbT * (double)T;
53
+ p = 1.0/(1.0+exp(-(E1-E2)/(kbT) ));
38
- printf("%e \n",E);
54
+ printf("% lf %e %e %e\n",(double)T,P,E1,E2);
39
- //E=-1.52e-18となる。
55
+ }
40
56
  }
41
57
  ```
42
58
  ### 実現したいこと
@@ -46,39 +62,13 @@
46
62
 
47
63
  ```
48
64
  エラーメッセージ
49
- 2.c:3:11: error: "20" may not appear in macro parameter list
50
- #define N(20)
51
- ^~
52
65
  2.c: In function 'main':
66
+ 2.c:4:12: error: expected identifier or '(' before numeric constant
67
+ #define N (20)
68
+ ^~
53
- 2.c:8:14: error: conflicting types for 'P'
69
+ 2.c:8:23: note: in expansion of macro 'N'
54
- int i,j,n,N,P;
70
+ double E1,E2,T,P,kbT,N;
55
- ^
71
+ ^
56
- 2.c:7:17: note: previous declaration of 'P' was here
57
- double E1,E2,T,P,kb;
58
- ^
59
- 2.c:20:21: error: expected ')' before numeric constant
60
- if((double)rand() RAND_MAX > 0.5)m[i][j]-1;
61
- ~ ^
62
- )
63
- 2.c:45:3: error: 'kbT' undeclared (first use in this function); did you mean 'kb'?
64
- kbT = kb * (double)T;
65
- ^~~
66
- kb
67
- 2.c:45:3: note: each undeclared identifier is reported only once for each function it appears in
68
- 2.c:46:3: error: 'p' undeclared (first use in this function)
69
- p = 1.0/(1.0+exp(-(E1-E2)/(kbT) ));
70
- ^
71
- 2.c:46:16: warning: implicit declaration of function 'exp' [-Wimplicit-function-declaration]
72
- p = 1.0/(1.0+exp(-(E1-E2)/(kbT) ));
73
- ^~~
74
- 2.c:46:16: warning: incompatible implicit declaration of built-in function 'exp'
75
- 2.c:46:16: note: include '<math.h>' or provide a declaration of 'exp'
76
- 2.c:3:1:
77
- +#include <math.h>
78
- #define N(20)
79
- 2.c:46:16:
80
- p = 1.0/(1.0+exp(-(E1-E2)/(kbT) ));
81
- ^~~
82
72
  ```
83
73
 
84
74
 

1

文法

2023/01/16 11:24

投稿

riui
riui

スコア4

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,42 @@
2
2
  下のプログラムを立ててこれを利用してシュミレーションをしてみたいのですが、エラーの内容や改善方法がよくわかりません。
3
3
  助けて欲しいです。
4
4
  何卒宜しくお願い致します。
5
+
5
- ```> ### ここに言語を入力
6
+ ```> ### C
7
+ #include<stdio.h>
8
+ #include<stdlib.h>
9
+ #define N(20)
10
+
11
+ int main(){
12
+ double J = 2.0e-21;
13
+ double E;
14
+ int i,j,n,N;
15
+ int m[N][N];
16
+
17
+ //磁化を代入する
18
+ for( j = 0; j < N; j++){
19
+ for( i = 0; i < N; i++){
20
+ m[i][j]=1;
21
+
22
+ //ランダム磁化①
23
+ m[i][j]=(rand()%2)*2-1;
24
+
25
+ //ランダム磁化②
26
+ if((double)rand() RAND_MAX > 0.5)m[i][j]-1;
27
+ else m[i][j]=-1;
28
+ }
29
+ }
30
+ //エネルギー計算
6
- コード
31
+ E=0.0;
32
+ for(j=0;j<N;j++){
33
+ for(i=0;i<N;i++){
34
+ if(i < N-1)E += -J*m[i][j]*m[i+1][j];
35
+ if(j < N-1)E += -J*m[i][j]*m[i][j+1];
36
+ }
37
+ }
38
+ printf("%e \n",E);
39
+ //E=-1.52e-18となる。
40
+ }
7
41
  ```
8
42
  ### 実現したいこと
9
43
  計算値を用いたgnuplotでグラフを作成したいです。
@@ -12,7 +46,6 @@
12
46
 
13
47
  ```
14
48
  エラーメッセージ
15
- ```
16
49
  2.c:3:11: error: "20" may not appear in macro parameter list
17
50
  #define N(20)
18
51
  ^~
@@ -46,45 +79,8 @@
46
79
  2.c:46:16:
47
80
  p = 1.0/(1.0+exp(-(E1-E2)/(kbT) ));
48
81
  ^~~
49
- ### 該当のソースコード
50
-
51
- ```C
52
-
53
82
  ```
54
- #include<stdio.h>
55
- #include<stdlib.h>
56
- #define N(20)
57
83
 
58
- int main(){
59
- double J = 2.0e-21;
60
- double E;
61
- int i,j,n,N;
62
- int m[N][N];
63
-
64
- //磁化を代入する
65
- for( j = 0; j < N; j++){
66
- for( i = 0; i < N; i++){
67
- m[i][j]=1;
68
-
69
- //ランダム磁化①
70
- m[i][j]=(rand()%2)*2-1;
71
-
72
- //ランダム磁化②
73
- if((double)rand() RAND_MAX > 0.5)m[i][j]-1;
74
- else m[i][j]=-1;
75
- }
76
- }
77
- //エネルギー計算
78
- E=0.0;
79
- for(j=0;j<N;j++){
80
- for(i=0;i<N;i++){
81
- if(i < N-1)E += -J*m[i][j]*m[i+1][j];
82
- if(j < N-1)E += -J*m[i][j]*m[i][j+1];
83
- }
84
- }
85
- printf("%e \n",E);
86
- //E=-1.52e-18となる。
87
- }
88
84
 
89
85
  ### 試したこと
90
86
  1.温度300K, 500Kにおけるステップ数(横軸)と磁化(縦軸)のグラフを作成しなさい。