Skip to content

Instantly share code, notes, and snippets.

@kuzemkon
Created November 29, 2016 10:44
Show Gist options
  • Save kuzemkon/096949a2ff9093616e008bcefd672a68 to your computer and use it in GitHub Desktop.
Save kuzemkon/096949a2ff9093616e008bcefd672a68 to your computer and use it in GitHub Desktop.
11 lab numberical methods
#include <stdio.h>
#include <math.h>
double c = 5;
double x0=0.0;
double y0=1.0;
double f(double x, double y){
return 2 * pow(x, 2) + y;
}
double yt(double x){
return c * exp(x) - 2 * pow(x, 2) - 4 * x - 4;
}
double rk(double x,double y,double h){
double k1 = f(x, y);
double k2 = f(x + h / 2, y + h / 2 * k1);
double k3 = f(x + h / 2, y + h / 2 * k2);
double k4 = f(x + h, y + h * k3);
return y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
}
double rk2(double x0,double y0, double h) {
double y1=rk(x0,y0,h/2);
double y2=rk(x0+h/2,y1,h/2);
return y2;
}
int main(int argc, char **argv) {
double h=1E-2;
double xn=10;
FILE * file = fopen("out .txt", "wt");
double x1=x0;
double y1=y0;
double x2=x0;
double y2=y0;
do {
x0=x1;
y0=y1;
double y00=y2;
double x1=x0+h;
double y1=rk(x0,y0,h);
double y2=rk(x0,y00,h);
fprintf(file, "x1=%lf y1=%lf eps=%e x2=%lf y2=%lf eps=%e\n", x1, y1, fabs(y1 - yt(x1)),x2, y2, fabs(16.0/15.0*(y2 - y1)));
}
while (x1 < xn);
fclose(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment