/
MrhoRK.c
184 lines (136 loc) · 4.15 KB
/
MrhoRK.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
// declare libraries
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <cpgplot.h>
#define N 200
//declared functions outside main
double gamma(double p), M(double r, double m, double p), rho(double r, double m, double p);
double M2(double r, double m, double p), M3(double r, double m, double p), M4(double r, double m, double p);
double rho2(double r, double m, double p), rho3(double r, double m, double p), rho4(double r, double m, double p);
double h = 0.01; //the initail parameter h, the step size
void main() //main code
{
printf("\nRUNGE-KUTTA METHOD\n\nR M Rho\n"); //printing titles for values displayed
double c = 10.0; //the parameter rho(c)
int n; //the integer steps, n
//declare arrays
float x[N];
float y[N];
float z[N];
//r,m,p as the radius, mass and density
double r,m,p;
//declare initial conditons for arrays
x[0] = h; //first array is for r=h
y[0] = (h*h*h/3)*c; //initial conditon for scaled mass (m)
z[0] = c*(1-((h*h*c)/(6*gamma(c)))); //initial conditon for rho
//for loop for n=0,1,...,200
for(n=0;n<N;n++)
{
//declared how x(n+1) relates to x(n), y(n+1) relates to y(n), z(n+1) relates to z(n)
x[n+1] = x[n]+h;
y[n+1] = y[n]+(h/6)*(M(x[n],y[n],z[n])+2*M2(x[n],y[n],z[n])+2*M3(x[n],y[n],z[n])+M4(x[n],y[n],z[n]));
z[n+1] = z[n]+(h/6)*(rho(x[n],y[n],z[n])+2*rho2(x[n],y[n],z[n])+2*rho3(x[n],y[n],z[n])+rho4(x[n],y[n],z[n]));
if(isnan(z[n+1]))
{
break;
}
//r,m,p will be declared in pg-plot
r = x[n+1];
m = y[n+1];
p = z[n+1];
printf("%.2e %.2e %.2e\n",x[n+1],y[n+1],z[n+1]); //printed values for x and y respectively
}
//Use pg-plot to plot mass and density
// cpgbeg starts a plotting page, in this case with 2x1 panels
cpgbeg(0,"?",2,1);
// sets colour: 1-black, 2-red, 3-green, 4-blue
cpgsci(1);
// sets line style: 1-solid, 2-dashed, 3-dot-dashed, 4-dotted
cpgsls(1);
// sets charachter height, larger number = bigger
cpgsch(1.);
// cpgpage() moves to the next panel
cpgpage();
// sets the axes limits in the panel
cpgswin(0,r,0,m);
// draw the axes
cpgbox("BCNST", 0.0, 0, "BCNST", 0.0, 0);
// label the bottom axis
cpgmtxt("B",2.,.5,.5,"radius");
// label the left axis
cpgmtxt("L",2.5,.5,.5,"saclaed mass");
// connect N points in ax and ay with a line
cpgline(n,x,y);
// cpgpage() moves to the next panel
cpgpage();
// sets the axes limits in the panel
cpgswin(0,r,0,c);
// draw the axes
cpgbox("BCNST", 0.0, 0, "BCNST", 0.0, 0);
// label the bottom axis
cpgmtxt("B",2.,.5,.5,"radius");
// label the left axis
cpgmtxt("L",2.5,.5,.5,"density");
// connect N points in ax and ay with a line
cpgline(n,x,z);
// close all pgplot applications
cpgend();
// end program
return;
}
//declared the function for gamma(x) = x^2/(3*sqrt(1+x^2))
double gamma(double p)
{
double e = cbrt(p);
double x = ((e*e)/(3*(sqrt(1+e*e))));
return x;
}
//declared the function for dm/dr
double M(double r, double m, double p)
{
double b = r*r*p; //coupled equation for dm/dr
return b; //return dm/dr
}
//declared the function for d(rho)/dr
double rho(double r, double m, double p)
{
double t = -((m*p)/(gamma(p)*r*r)); //coupled equation for d(rho)/dr
return t; //return d(rho)/dr
}
//declared the function for M2 from M
double M2(double r, double m, double p)
{
double Fn = M(r+h/2,m+(h/2)*M(r,m,p),p+(h/2)*rho(r,m,p));
return Fn;
}
//declared the function for rho2 from rho
double rho2(double r, double m, double p)
{
double Fn = rho(r+h/2,m+(h/2)*M(r,m,p),p+(h/2)*rho(r,m,p));
return Fn;
}
//declared the function for M3 from M2
double M3(double r, double m, double p)
{
double Fn = M(r+h/2,m+(h/2)*M2(r,m,p),p+(h/2)*rho2(r,m,p));
return Fn;
}
//declared the function for rho3 from rho2
double rho3(double r, double m, double p)
{
double Fn = rho(r+h/2,m+(h/2)*M2(r,m,p),p+(h/2)*rho2(r,m,p));
return Fn;
}
//declared the function for M4 from M3
double M4(double r, double m, double p)
{
double Fn = M(r+h,m+h*M3(r,m,p),p+h*rho3(r,m,p));
return Fn;
}
//declared the function for rho4 from rho3
double rho4(double r, double m, double p)
{
double Fn = rho(r+h,m+h*M3(r,m,p),p+h*rho3(r,m,p));
return Fn;
}