go
3587 / 1367 / 130
Регистрация: 16.04.2009
Сообщений: 4,527
|
27.11.2011, 14:35
|
|
yangicher, а сразу нельзя было сказать?
вообще это проблемы студии
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
| #include <math.h>
#include <stdio.h>
const float a=0,b=2; // bounds of the interval
const int num_points=10, // number of points to solve
num_starting_points=4; // number of points to solve with Runge-Kutta method
//float x0=0,y0=1; // starting conditions
float f(float x, float y)
{
return x+y; // y'=x+y
}
// this function realises Runge-Kutta method for n starting points
void calculate(float *y)
{
float k1,k2,k3,k4,x,yi,h;
h=(b-a)/num_points; // step
yi=1; x=0;
for (int i=0;i<num_starting_points;i++)
{
k1=h*f(x,yi);
k2=h*f(x+h/2,yi+k1/2);
k3=h*f(x+h/2,yi+k2/2);
k4=h*f(x+h,yi+k3);
yi+=(k1+2*k2+2*k3+k4)/6;
x+=h;
*(y+i+1)=yi;
}
}
void main(void)
{
float y[num_points+1],h;
y[0]=1;
float x0=0;
// apply Runge-Kutta method
calculate(y);
h=(b-a)/num_points;
// extrapolating
for (int i=num_starting_points;i<num_points;i++)
y[i] = y[i-1]+h/24*(55*f(x0+(i-1)*h,y[i-1])-
59*f(x0+(i-2)*h,y[i-2])+
37*f(x0+(i-3)*h,y[i-3])-
9*f(x0+(i-4)*h,y[i-4]));
printf("X\t\tY\t\tExact solution\n");
for (int i=0;i<num_points;i++)
printf("%f\t%f\t%f\n",(x0+i*h),y[i],(2*exp(x0+i*h)-(x0+i*h)-1));
getchar ();
} |
|
Добавлено через 7 минут
А вообще вот
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
| #include <math.h>
#include <stdio.h>
const float a=0,b=2; // bounds of the interval
const int num_points=10, // number of points to solve
num_starting_points=4; // number of points to solve with Runge-Kutta method
float x0=0,yy=1; // starting conditions
float f(float x, float y)
{
return x+y; // y'=x+y
}
// this function realises Runge-Kutta method for n starting points
void calculate(float *y)
{
float k1,k2,k3,k4,x,yi,h;
h=(b-a)/num_points; // step
yi=yy; x=x0;
for (int i=0;i<num_starting_points;i++)
{
k1=h*f(x,yi);
k2=h*f(x+h/2,yi+k1/2);
k3=h*f(x+h/2,yi+k2/2);
k4=h*f(x+h,yi+k3);
yi+=(k1+2*k2+2*k3+k4)/6;
x+=h;
*(y+i+1)=yi;
}
}
void main(void)
{
float y[num_points+1],h;
y[0]=yy;
// apply Runge-Kutta method
calculate(y);
h=(b-a)/num_points;
// extrapolating
for (int i=num_starting_points;i<num_points;i++)
y[i] = y[i-1]+h/24*(55*f(x0+(i-1)*h,y[i-1])-
59*f(x0+(i-2)*h,y[i-2])+
37*f(x0+(i-3)*h,y[i-3])-
9*f(x0+(i-4)*h,y[i-4]));
printf("X\t\tY\t\tExact solution\n");
for (int i=0;i<num_points;i++)
printf("%f\t%f\t%f\n",(x0+i*h),y[i],(2*exp(x0+i*h)-(x0+i*h)-1));
} |
|
1
|