C programming

Hello everyone. I am coding a program which is about Runge Kutta 4th order numerical method for solving coupled differential equations. The problem is I don't get any result after putting all the variables. I really need help please.

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
#include<stdio.h>
#include<math.h>
float f(float t, float a){
	return -1 * 6.65 * pow(10, -14);
}
float y(float t, float a, float b, float c){
	return 6.64 * pow(10, -14) * a - (9.91 * pow(10, -13) * b * c);  
}
float z(float t, float b, float c){
	return -1 * 9.91 * pow(10, -13) * b * c;
}
float q(float t, float b, float c){
	return 9.91 * pow(10,-13) * b * c;
}
int main (void){
	int i , j ;
	float RA[1000], RB[1000], RC[1000], RD[1000], K[1000], G[1000], L[1000], M[1000], a, b, c, d, h, t ,n ;
	
	printf("Lutfen SO2 için konsantrasyon degeri giriniz:\n");
	scanf("%f", &RA[0]);
	
	printf("Lutfen SO3 için konsantrasyon degeri giriniz:\n");
	scanf("%f", &RB[0]);
	
	printf("Lutfen H2O için konsantrasyon degeri giriniz:\n");
	scanf("%f", &RC[0]);
	
	printf("Lutfen H2SO4 için konsantrasyon degeri giriniz:\n");
	scanf("%f", &RD[0]);
	

	
	 
	printf("Lutfen sureyi belirleyiniz:\n");
	scanf("%f", &n);
	
	printf("Lutfen adim sayisini belirleyiniz:");
	scanf("%f", &h);
	
	t = 0;
	
	for(i = 0; t <= n; i++ ){
		K[0] = f(t , RA[i]);
		G[0] = y(t, RA[i], RB[i], RC[i]);
		L[0] = z(t, RB[i], RC[i]);
		M[0] = q(t, RB[i], RC[i]);
		
		K[1] = f(t + 0.5 * h, RA[i] + 0.5 * K[0]);
		G[1] = y(t + 0.5 * h, RA[i] + 0.5 * K[0], RB[i] + 0.5 * G[0], RC[i] + 0.5 * L[0] );
		L[1] = z(t + 0.5 * h, RB[i] + 0.5 * G[0], RC[i] + 0.5 * L[0]);
		M[1] = q(t + 0.5 * h, RB[i] + 0.5 * G[0], RC[i] + 0.5 * L[0]);
		
		K[2] = f(t + 0.5 * h, RA[i] + 0.5 * K[1]);
		G[2] = y(t + 0.5 * h, RA[i] + 0.5 * K[1], RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1] );
		L[2] = z(t + 0.5 * h, RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1]);
		M[2] = q(t + 0.5 * h, RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1]);
		
		K[3] = f(t + h, RA[i] +  K[2]);
		G[3] = y(t + h, RA[i] +  K[2], RB[i] + G[2], RC[i] + L[2] );
		L[3] = z(t + h, RB[i] +  G[2], RC[i] + L[2]);
		M[3] = q(t + h, RB[i] +  G[2], RC[i] + L[2]);
		
		RA[i+1] =  RA[i] + (1/6) * (K[0] + 2 * K[1] + 2 * K[2] + K[3]) * h;
		RB[i+1] =  RB[i] + (1/6) * (G[0] + 2 * G[1] + 2 * G[2] + G[3]) * h;
		RC[i+1] =  RC[i] + (1/6) * (L[0] + 2 * L[1] + 2 * L[2] + L[3]) * h;
		RD[i+1] =  RD[i] + (1/6) * (M[0] + 2 * M[1] + 2 * M[2] + M[3]) * h;
		
		t = t + h;
		
		printf("Belirlenen Sicakliktaki konsantrasyon degerleri = %f\n", i, RA[i+1], RB[i+1], RC[i+1], RD[i+1]);
		
 
		
	}
	return 0;
}

Last edited on
The "not any result" hints that line 70 does not evaluate. If it does not, then condition on line 42 is always false and the loop is skipped. The values of 't' and 'n' on line 41 are thus of interest.


Regardless, you do compute (1/6) on lines 63-66. That is integer division and the result is exactly 0. 0 * foo == 0.
You could replace:
(1/6) * (...) * h
with
(...) * h / 6


PS. You have arrays that have 1000 elements each, but you use only the 4 first elements of them.
On the other hand your loop could iterate more than 1000 times and produce out of range error with the other arrays.
Thank you for your answer.
I don't really understand where the problem is. I did same project with just only one function and used the same "for loop".
#include<stdio.h>
#include<math.h>
float f( float a){
return -1.00 * 6.00 * (
pow(10.00, -1.00* 14.00)) * a;
}
float y( float a, float b, float c){
return 6.00 * pow(10.00, -1.00 * 14.00) * a - (9.00 * pow(10.00, -1.00 * 13.00) * b * c);
}
float z( float b, float c){
return -1.00 * 9.00 * (pow(10.00, -1.00 * 13.00)) * b * c;
}
float q( float b, float c){
return 9.00 * pow(10.00, -1.00 * 13.00) * b * c;
}
int main (void){
int i , j ;
float RA[1000], RB[1000], RC[1000], RD[1000], K[1000], G[1000], L[1000], M[1000], a, b, c, d, h, t ,n ;

printf("Lutfen SO2 için konsantrasyon degeri giriniz:\n");
scanf("%f", &RA[0]);

printf("Lutfen SO3 için konsantrasyon degeri giriniz:\n");
scanf("%f", &RB[0]);

printf("Lutfen H2O için konsantrasyon degeri giriniz:\n");
scanf("%f", &RC[0]);

printf("Lutfen H2SO4 için konsantrasyon degeri giriniz:\n");
scanf("%f", &RD[0]);

/*printf("1.Reaksiyon için hiz sabiti giriniz:\n");
scanf("%f", k1);

printf("2.Reaksiyon için hiz sabiti giriniz:\n");
scanf("%f", k2);*/


printf("Lutfen sureyi belirleyiniz:\n");
scanf("%f", &n);

printf("Lutfen adim sayisini belirleyiniz:");
scanf("%f", &h);

t = 0;

for(i = 0; t <= n; i++ ){
K[1] = f( RA[i]);
G[1] = y( RA[i], RB[i], RC[i]);
L[1] = z( RB[i], RC[i]);
M[1] = q( RB[i], RC[i]);

K[2] = f( RA[i] + 0.5 * K[1]);
G[2] = y( RA[i] + 0.5 * K[1], RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1] );
L[2] = z( RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1]);
M[2] = q( RB[i] + 0.5 * G[1], RC[i] + 0.5 * L[1]);

K[3] = f( RA[i] + 0.5 * K[2]);
G[3] = y( RA[i] + 0.5 * K[2], RB[i] + 0.5 * G[2], RC[i] + 0.5 * L[2] );
L[3] = z( RB[i] + 0.5 * G[2], RC[i] + 0.5 * L[2]);
M[3] = q( RB[i] + 0.5 * G[2], RC[i] + 0.5 * L[2]);

K[4] = f( RA[i] + K[3]);
G[4] = y( RA[i] + K[3], RB[i] + G[3], RC[i] + L[3] );
L[4] = z( RB[i] + G[3], RC[i] + L[3]);
M[4] = q( RB[i] + G[3], RC[i] + L[3]);

RA[i+1] = RA[i] + (K[1] + 2 * K[2] + 2 * K[3] + K[4]) * h / 6;
RB[i+1] = RB[i] + (G[1] + 2 * G[2] + 2 * G[3] + G[4]) * h / 6;
RC[i+1] = RC[i] + (L[1] + 2 * L[2] + 2 * L[3] + L[4]) * h / 6;
RD[i+1] = RD[i] + (M[1] + 2 * M[2] + 2 * M[3] + M[4]) * h / 6;

t = t + h;

printf("Belirlenen Sicakliktaki konsantrasyon degerleri = %f\n", RA[i+1] );
printf("Belirlenen Sicakliktaki konsantrasyon degerleri = %f\n", RB[i+1] );
printf("Belirlenen Sicakliktaki konsantrasyon degerleri = %f\n", RC[i+1] );
printf("Belirlenen Sicakliktaki konsantrasyon degerleri = %f\n", RD[i+1] );

}



return 0;
}

Made some change on it. I get result now but the functions that I defined don't work.
change all the floats to doubles or you will probably have numerical problems on some problems.

be specific about what isnt working.
Registered users can post here. Sign in or register to post.