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
|
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<conio.h>
float rung4(float x, float y, float h);
float maks(float *y0, float *y1, float n);
float F( float x, float y );
int main()
{
// clrscr();
int i;
// float h;
double x,y, y0,x0,y1,n,f,k1,k2,k3,k4, h, eps=0.00001 ;
// float max(float *y0, float *y1, float n)
{
/* float max=fabs(y0[0]-y1[0]);
for (i=1; i<n; i++)
if (fabs(y0[0]-y1[2*i>max])
max =fabs(y0[0]-y1[2*i>max);
return max;
}
*/
do
{
printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(x0=0; x0<n; x0=x0+h)
{
f=F(x0,y0);
k1 = h * f;
f = F(x0+h/2,y0+k1/2);
k2 = h * f;
f = F(x0+h/2,y0+k2/2);
k3 = h * f;
f = F(x0+h/2,y0+k2/2);
k4 = h * f;
y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
}
printf("\n\n y(%.4lf) = %.3lf ",x0+h,y1);
y0=y1;
}
while
maks(((y0,y1, n/2)/15)>=eps);
puts("Runge-kutta 4th order");
for(i=0; i<n/2+1; i++)
printf(" y[%f]=%.5f\n", x0+h,y1);
}
getch();
return eps;
}
system("pause");
}
|