-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurvaRotacion.c
257 lines (186 loc) · 5.41 KB
/
CurvaRotacion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* COMPILO CON cc CurvaRotacion.c -o CurvaRotacion.x -lm Y ./CurvaRotacion.x */
#define MAG_CAR 32
#define FILAS 301
#define COLS 2
/* CTES DIMENSIONALES EN KPC */
#define B_b 0.2497
#define B_d 5.16
#define A_d 0.3105
#define A_h 64.3
void Lector_Datos(void); // LEE LOS DATOS, LOS GUARDA EN MATRICES Y GENERA ARCHIVO
double V_Rad(double R, double M_b, double M_d, double M_h); // FUNCION V_rad(R)
double Probabilidad(double D_exp, double D_rad); // Likelihood observados, modelo
double Aleatorio(void);
double Prom(double *X);
void Imprime_en_Cons(void);
void M_H(int N);
void Ajuste(void);
double *R_exp;
double *V_exp;
double *P;
double *M_b;
double *M_d;
double *M_h;
double *V_rad_ajuste;
double M_b_prom;
double M_d_prom;
double M_h_prom;
int N = 1000;
int main(){
Lector_Datos();
M_H(N);
Imprime_en_Cons();
Ajuste();
return 0;
}
void Lector_Datos(void){
FILE *datos = fopen("RadialVelocities.dat","r");
double **M_Datos;
M_Datos = malloc(sizeof(double *)*FILAS*COLS);
// SEPARA LOS DATOS EN FILAS Y DESPUES EN COLUMNAS DE ACUERDO A EL DELIMITADOR
char linea[MAG_CAR];
char *split_linea;
int i = 0, j = 0;
i = 0;
while(fgets(linea, MAG_CAR, datos) != NULL){
j = 0;
M_Datos[i] = malloc(sizeof(double)*MAG_CAR);
split_linea = strtok(linea, " ");
while (split_linea != NULL)
{
M_Datos[i][j] = atof(split_linea);
split_linea = strtok(NULL, " ");
j += 1;
}
i +=1;
}
/* GENERA ARCHIVOS DE DATOS */
FILE *Datos = fopen("Datos.dat", "w");
for(int i = 0; i < FILAS; i++){
fprintf(Datos, "%lf" " " "%lf" , M_Datos[i][0], M_Datos[i][1]);
fprintf(Datos, "\n");
}
fclose(Datos);
/* MATRICES DE DATOS EXPERIMENTALES */
R_exp = malloc(sizeof(double *)*FILAS);
for(int i = 0; i < FILAS; i++){
R_exp[i] = M_Datos[i][0];
//printf("R[%d]=%lf", i, R[i] );
//printf("\n");
}
V_exp = malloc(sizeof(double *)*FILAS);
for(int i = 0; i < FILAS; i++){
V_exp[i] = M_Datos[i][1];
//printf("V[%d]=%lf", i, V[i]);
//printf("\n");
}
}
double Probabilidad(double D_exp, double D_rad){
double chi_2 = pow((D_exp - D_rad)/0.1, 2);
return exp(-(1.0/2.0)*chi_2);
}
double V_Rad(double R_exp, double M_b, double M_d, double M_h){
return (sqrt(M_b)*R_exp/pow(pow(R_exp, R_exp)+pow(B_b, B_b), 3/4))+(sqrt(M_d)*R_exp/pow(pow(R_exp, R_exp)+pow((B_b+A_d), (B_b+A_d)), 3/4))+(sqrt(M_h)/pow(pow(R_exp, R_exp)+pow(A_h, 2), 1/4));
}
double Aleatorio(void){
double r = (double) rand()/RAND_MAX; // Lo arreglo para los bethas de N-H
if(r !=1 ){
return r; // LO NORMALIZO PORQUE NO QUIERO NUMEROS TAN GRANDES
}
}
void M_H(int N){
M_b = malloc(sizeof(double *)*N);
M_d = malloc(sizeof(double *)*N);
M_h = malloc(sizeof(double *)*N);
P = malloc(sizeof(double *)*N);
double *Mb_prima = malloc(sizeof(double *)*N);
double *Md_prima = malloc(sizeof(double *)*N);
double *Mh_prima = malloc(sizeof(double *)*N);
double *P_prima = malloc(sizeof(double *)*N);
double *P_temp = malloc(sizeof(double *)*N);
M_b[0] = Aleatorio();
M_d[0] = Aleatorio();
M_h[0] = Aleatorio();
P[0] = Probabilidad(R_exp[0], V_Rad(R_exp[0], M_b[0], M_d[0], M_h[0]));
/*printf("%lf", M_b[0]);
printf("\n");
printf("%lf", M_d[0]);
printf("\n");
printf("%lf", M_h[0]);
printf("\n");
printf("%lf", P[0]);
printf("\n");*/
for(int i = 0; i < N; i++){
Mb_prima[i] = Aleatorio();
Md_prima[i] = Aleatorio();
Mh_prima[i] = Aleatorio();
P_prima[i] = Probabilidad(R_exp[i], V_Rad(R_exp[i], Mb_prima[i], Md_prima[i], Mh_prima[i]));
P_temp[i] = Probabilidad(R_exp[i], V_Rad(R_exp[i], M_b[i], M_d[i], M_h[i]));
double alpha = (double)abs(P_prima[i-1]/P_temp[i-1]);
//printf("[%d]%lf", i, alpha);
//printf("\n");
if( alpha >=1.0 ){
M_b[i] = Mb_prima[i-1];
M_d[i] = Md_prima[i-1];
M_h[i] = Mh_prima[i-1];
P[i] = P_prima[i-1];
}else{
double betha = 1.0/Aleatorio();
if( betha <= alpha ){
M_b[i] = Mb_prima[i-1];
M_d[i] = Md_prima[i-1];
M_h[i] = Mh_prima[i-1];
P[i] = P_prima[i-1];
}else{
M_b[i] = Mb_prima[i-1];
M_d[i] = Md_prima[i-1];
M_h[i] = Mh_prima[i-1];
P[i] = P_temp[i-1];
}
}
//printf("%lf" " " "%lf", P_prima[i], P[i]);
//printf("\n");
//printf("[%d]%lf", i, M_b[i]);
//printf("\n");
}
}
double Prom(double *X){
float suma = 0.0;
for(int i = 0; i < N; i++){
suma += X[i];
}
float prom = suma/N;
return prom;
}
void Imprime_en_Cons(void){
M_b_prom = Prom(M_b);
M_d_prom = Prom(M_d);
M_h_prom = Prom(M_h);
printf("M_b=%lf", M_b_prom);
printf("\n");
printf("M_d=%lf", M_d_prom);
printf("\n");
printf("M_h=%lf", M_h_prom);
printf("\n");
}
void Ajuste(void){
M_b_prom = Prom(M_b);
M_d_prom = Prom(M_d);
M_h_prom = Prom(M_h);
V_rad_ajuste = malloc(sizeof(double *)*N);
for(int i = 0; i < N; i++){
V_rad_ajuste[i] = (sqrt(M_b_prom)*R_exp[i]/pow(pow(R_exp[i], R_exp[i])+pow(B_b, B_b), 3/4))+(sqrt(M_d_prom)*R_exp[i]/pow(pow(R_exp[i], R_exp[i])+pow((B_b+A_d), (B_b+A_d)), 3/4))+(sqrt(M_h_prom)/pow(pow(R_exp[i], R_exp[i])+pow(A_h, 2), 1/4));
//printf("[%d]%lf", i, V_rad_ajuste[i]);
//printf("\n");
}
FILE *Ajuste = fopen("Ajuste.dat", "w");
for(int i = 0; i < FILAS; i++){
fprintf(Ajuste, "%lf", V_rad_ajuste[i]);
fprintf(Ajuste, "\n");
}
fclose(Ajuste);
}