Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "conio.h"
- #include "math.h"
- #include<stdlib.h>
- #define RAND_MAX 2147483647
- typedef double (*Function)(double x, double y);
- typedef double (*Function2)(double t, double x, double y);
- typedef double (*Function1)(double x);
- double myFunc(double x, double y){
- //return 1 + 0.3*y + sin(x) - y*y;
- return cos(1.5*x+y)+1.5*(x-y);
- }
- double y1(double x, double y1, double y2){
- return y2;
- }
- double y2(double x, double y1, double y2){
- return (1/(x*x)-1)*y1-y2/x;
- }
- //for me a and b == 2
- //f
- double sysOne(double t, double x, double y){
- return exp((-1)*(x*x + t*t)) + 2*x;
- }
- //g
- double sysTwo(double t, double x, double y){
- return 2*y*y + t;
- }
- double funcP(double x){ return 2*x; }
- double funcG(double x){ return 2; }
- double funcF(double x){ return 4*x; }
- void resultOutput(double *y,double a,double b,double h){
- double x = a;
- int n=(b-a)/h+0.5;
- printf(" N X[i] Y[i]\n");
- for (int i=0;i<=n;i++){
- printf("%2d %10.6f %10.6f\n",i,x,y[i]);
- x+=h;
- }
- printf("\n");
- }
- void systemResultOutput(double *x, double *y, double a, double b, double h){
- double t = a;
- int n=(b-a)/h;
- printf(" i X[i] Y[i] Z[i]\n");
- for (int i=0;i<=n;i++){
- printf("%2d %10.6f %10.6f %10.6f\n",i,t,x[i],y[i]);
- t+=h;
- }
- printf("\n");
- }
- double rungeMethod(Function f, double a, double b, double y0, double h, double eps, double *y)
- {
- double delta;
- int n = (b-a)/h, i;
- y[0] = y0;
- double *tmp = new double[n+1];//temp array
- int multiply = 1; //mul
- do {
- //points values
- double y1 = y0;
- for (i=1;i<=n;i++){
- for (int j = 0; j < multiply; j++){
- double x = a + ((i-1)*multiply+j)*h;
- //koeff
- double k1 = h * f(x,y1);
- double k2 = h * f(x+h/2,y1+k1/2);
- double k3 = h * f(x+h/2,y1+k2/2);
- double k4 = h * f(x+h,y1+k3);
- double y2 = y1 + (k1+2*k2+2*k3+k4)/6;
- y1 = y2;
- }
- tmp[i] = y1;
- }
- //checks eps
- if (multiply == 1){
- delta = eps+1;
- }else{
- delta = 0;
- for (i=1;i<=n;i++){
- delta += fabs(tmp[i]-y[i]);
- }
- }
- //transfer to new array
- if (delta>eps){
- for (i=1; i<=n; i++) y[i]=tmp[i];
- multiply *= 2; //mul
- h/=2;
- }
- } while (delta>eps);
- return h;
- }
- double methodRKSystem(Function2 f, Function2 g, double a, double b,
- double x0, double y0, double h, double eps, double *x, double *y)
- {
- double delta;
- int n = (b-a)/h, i;
- x[0] = x0;
- y[0] = y0;
- double *tmpx = new double[n+1]; //tmp
- double *tmpy = new double[n+1]; //tmp
- int multiply = 1;
- do {
- //point values
- double x1 = x0;
- double y1 = y0;
- for (i=1;i<=n;i++){
- for (int j = 0; j < multiply; j++){
- double t = a + ((i-1)*multiply+j)*h;
- //koeff
- double k1 = h * f(t,x1,y1);
- double l1 = h * g(t,x1,y1);
- double k2 = h * f(t+h/2,x1+k1/2,y1+l1/2);
- double l2 = h * g(t+h/2,x1+k1/2,y1+l1/2);
- double k3 = h * f(t+h/2,x1+k2/2,y1+l2/2);
- double l3 = h * g(t+h/2,x1+k2/2,y1+l2/2);
- double k4 = h * f(t+h,x1+k3,y1+l3);
- double l4 = h * g(t+h,x1+k3,y1+l3);
- //new values
- double x2 = x1 + (k1+2*k2+2*k3+k4)/6;
- double y2 = y1 + (l1+2*l2+2*l3+l4)/6;
- x1 = x2;
- y1 = y2;
- }
- tmpx[i] = x1;
- tmpy[i] = y1;
- }
- //eps
- if (multiply==1){
- delta = eps+1;
- }else{
- delta = 0;
- for (i=1;i<=n;i++){
- delta += fabs(tmpx[i]-x[i]);
- delta += fabs(tmpy[i]-y[i]);
- }
- }
- //new array
- if (delta>eps){
- for (i=1;i<=n;i++) { x[i]=tmpx[i]; y[i]=tmpy[i]; }
- multiply *= 2;
- h/=2;
- }
- } while (delta>eps);
- return h;
- }
- double milnMethod(Function f, double a, double b, double y0, double h, double eps, double *y){
- double delta, tmp[5], dy[5];
- int div = 1;
- int n = (b-a)/h, i, j, k;
- y[0] = y0;
- double *prev = new double[n+1];
- do {
- //4 points by runge
- rungeMethod(f, a, a+h*3, y0, h, eps, tmp);
- double x = a;
- for (i=0; i<=n; i++) {
- y[i] = tmp[0];
- for (k=0; k<div; k++){
- //derivatives
- for(j=0; j<4; j++) dy[j] = f(x+h*j,tmp[j]);
- //extrapolation
- double yn = tmp[0] + 4*h/3*(2*dy[3]-dy[2]+2*dy[1]);
- //interpolation
- dy[4] = f(x+h*4,yn);
- yn = tmp[2] + h/3*(dy[2]+4*dy[3]+dy[4]);
- //1 step right
- tmp[4] = yn;
- x += h;
- for (int i=0;i<4;i++) tmp[i] = tmp[i+1];
- }
- }
- //check
- if (div==1){
- delta = eps+1;
- }else{
- delta = 0;
- for (i=0;i<n;i++) delta += fabs(y[i]-prev[i]);
- }
- if (delta>eps){
- div *= 2;
- h /= 2;
- for (i=0;i<n;i++) prev[i]=y[i];
- }
- } while (delta>eps);
- return h;
- }
- double adamsMethod(Function f, double a, double b, double y0, double h, double eps, double *y){
- double delta, tmp[5], dy[5];
- int div = 1;
- int n = (b-a)/h, i, j, k;
- y[0] = y0;
- double *prev = new double[n+1];
- do {
- //4 points frm Runge
- rungeMethod(f, a, a+h*3, y0, h, eps, tmp);
- double x = a;
- for (i=0; i<=n; i++) {
- y[i] = tmp[0];
- for (k=0; k<div; k++){
- //derivatives
- for(j=0; j<4; j++) dy[j] = f(x+h*j, tmp[j]);
- //extrapolation
- double yn = tmp[3] + h/24*(55*dy[3]-59*dy[2]+37*dy[1]-9*dy[0]);
- //interpolation
- dy[4] = f(x+h*4,yn);
- yn = tmp[3] + h/24*(9*dy[4]+19*dy[3]-5*dy[2]+dy[1]);
- //1 step right
- tmp[4] = yn;
- x += h;
- for(int i=0; i<4; i++) tmp[i] = tmp[i+1];
- }
- }
- //check
- if (div==1){
- delta = eps+1;
- }else{
- delta = 0;
- for (i=0;i<n;i++) delta += fabs(y[i]-prev[i]);
- }
- if (delta>eps){
- //h to the next
- div*=2;
- h/=2;
- for (i=0;i<n;i++) prev[i]=y[i];
- }
- } while (delta>eps);
- return h;
- }
- void runMethod(Function1 p, Function1 g, Function1 f, double a, double b, double h,
- double a0, double a1, double a2, double b0, double b1, double b2, double *y)
- {
- int n = ((b-a)/h+0.5)+1, i, j ;
- static double c[100],d[100];
- //straight
- double m0 = -2+h*p(a);
- double n0 = 1-h*p(a)+h*h*g(a);
- c[0] = (a1-a0*h)/(m0*(a1-a0*h)+n0*a1);
- d[0] = n0*a2*h/(a1-a0*h)+f(a)*h*h;
- for (i=1;i<n;i++){
- m0 = -2+h*p(a+h*i);
- n0 = 1-h*p(a+h*i)+h*h*g(a+h*i);
- c[i] = 1/(m0-n0*c[i-1]);
- d[i] = f(a+h*i)*h*h-n0*c[i-1]*d[i-1];
- }
- //feedback
- y[n-1] = (b1*c[n-3]*d[n-3]+b2*h)/(b1*(1+c[n-3])+b0*h);
- for (i=n-2;i>0;i--){
- y[i] = c[i-1]*(d[i-1]-y[i+1]);
- }
- y[0] = (a1*y[1]-a2*h)/(a1-a0*h);
- }
- void MKR(Function1 p, Function1 g, Function1 f, double a, double b, double h,
- double a0, double a1, double a2, double b0, double b1, double b2, double *y)
- {
- //matrix & right part vector
- int n = ((b-a)/h+0.5)+1, i, j ;
- static double m[50][50];
- static double v[100];
- for (i=0;i<n;i++){
- for (j=0;j<n;j++) m[i][j]=0;
- v[i]=0;
- }
- //main YR
- for (i=1;i<n-1;i++){
- m[i][i-1] = (1/h-p(a+i*h)/2)/h;
- m[i][i] = -2/(h*h)+g(a+i*h);
- m[i][i+1] = 1/(h*h)+p(a+i*h)/(2*h);
- v[i] = f(a+i*h);
- }
- //edge conditions
- m[0][0] = a0-a1/h; m[0][1] = a1/h; v[0] = a2;
- m[n-1][n-1] = b0+b1/h; m[n-1][n-2] = -b1/h; v[n-1] = b2;
- //solving system
- for (i=0;i<n;i++){
- //div str at diag el
- double diag = m[i][i];
- for (j=0;j<n;j++) m[i][j]/=diag;
- m[i][i]=1; v[i]/=diag;
- //rem i column by substr
- for (j=i+1;j<n;j++){
- double kf = m[j][i];
- for (int k=i;k<n;k++) m[j][k]-=kf*m[i][k];
- m[j][i]=0;
- v[j]-=kf*v[i];
- }
- }
- //feedback
- for (i=n-1;i>=0;i--){
- double yy = v[i];
- for (j=i+1;j<n;j++) yy-=y[j]*m[i][j];
- y[i] = yy;
- }
- }
- double Compare(double *y, double *y2, double a, double b, double h){
- int n = (b-a)/h+0.5;
- double delta = 0;
- for (int i=0;i<n;i++){
- double d = fabs(y[i]-y2[i*2]);
- if (d>delta) delta = d;
- }
- return delta;
- }
- int shootMethod(Function2 y1, Function2 y2, double a, double b, double h, double eps, double g1,
- double g2, double g3, double g4, double g5, double g6, double *y1r, double *y2r){
- int n = (b-a)/h+0.5;
- randomize();
- //double L1 = (qrand()*1.0)/RAND_MAX, L2 = (rand()*1.0)/RAND_MAX;
- double L1 = (rand()*1.0)/RAND_MAX, L2 =10+ (rand()*1.0)/RAND_MAX;
- double psi1, psi2;
- int i=1;
- //val at 1st point
- if (g2!=0){
- methodRKSystem(y1, y2, a, b, L1, (g3-g1*L1)/g2, h, eps, y1r, y2r);
- }else{
- methodRKSystem(y1, y2, a, b, (g3-g2*L1)/g1, L1, h, eps, y1r, y2r);
- }
- psi1 = g4*y1r[n-1]+g5*y2r[n-1]-g6;
- while (1){
- //koshi y1(a)=L
- if (g2!=0){
- methodRKSystem(y1, y2, a, b, L2,(g3-g1*L2)/g2, h, eps, y1r, y2r);
- }else{
- methodRKSystem(y1, y2, a, b, (g3-g2*L2)/g1, L2, h, eps, y1r, y2r);
- }
- psi2 = g4*y1r[n-1]+g5*y2r[n-1]-g6;
- if (fabs(psi1)+fabs(psi2)<eps) break;
- //corrected L
- double L = L2 - (L2-L1)*psi2/(psi2-psi1);
- L1 = L2; L2 = L;
- psi1 = psi2;
- i++;
- }
- return i;
- }
- void output(double *x, double *y, double a, double b, double h){
- double t = a;
- int n=(b-a)/h;
- printf(" i X[i] Y1[i] Y2[i]\n");
- for (int i=0;i<=n;i++){
- printf("%2d %10.6f %10.6f %10.6f\n",i,t,x[i],y[i]);
- t+=h;
- }
- printf("\n");
- }
- int main()
- {
- int result = 0,key=0;
- double a, b, h, eps, y0, x0;
- double a0, a1, a2, b0, b1, b2;
- double y[100], x[100];
- double g1, g2, g3, g4, g5, g6;
- while(1)
- {
- clrscr();
- printf("\n1) - ЊҐв®¤ ђгЈҐ-Љгвв .");
- printf("\n2) - ЊҐв®¤ ђгЈҐ-Љгвв ¤«п бЁб⥬ га ўҐЁ©.");
- printf("\n3) - ЊҐв®¤ Ђ¤ ¬б .");
- printf("\n4) - ЊҐв®¤ ЊЁ« .");
- printf("\n5) - ЊҐв®¤ Є®Ґзле а §®б⥩.");
- printf("\n6) - ЊҐв®¤ Їа®Ј®ЄЁ.");
- printf("\n7) - ЊҐв®¤ бв५мЎл.");
- printf("\n0) - ‚л室");
- printf("\nЌ ¦¬ЁвҐ Є« ўЁ¦г ўлЎа ®Ј® ў ¬Ё ЇгЄв ¬Ґо. ");
- //int menuOpt = 0;
- //scanf("%d", &menuOpt);
- key = getch();
- switch (key)
- {
- case 48: exit(1);
- case 49:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, eps, h, y(0): ");
- scanf("%lg %lg %lg %lg %lg", &a, &b, &eps, &h, &y0);
- rungeMethod(myFunc, a, b, y0, h, eps, y);
- resultOutput(y, a, b, h);
- break;
- }
- case 50:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, eps, h, y(0), x(0): ");
- scanf("%lg %lg %lg %lg %lg %lg", &a, &b, &eps, &h, &y0, &x0);
- methodRKSystem(sysOne, sysTwo, a, b, x0, y0, h, eps, x, y);
- systemResultOutput(x, y, a, b, h);
- break;
- }
- case 51:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, eps, h, y(0): ");
- scanf("%lg %lg %lg %lg %lg", &a, &b, &eps, &h, &y0);
- adamsMethod(myFunc, a, b, y0, h, eps, y);
- resultOutput(y, a, b, h);
- break;
- }
- case 52:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, eps, h, y(0): ");
- scanf("%lg %lg %lg %lg %lg", &a, &b, &eps, &h, &y0);
- milnMethod(myFunc, a, b, y0, h, eps, y);
- resultOutput(y, a, b, h);
- break;
- }
- case 53:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, h: ");
- scanf("%lg %lg %lg", &a, &b, &h);
- eps=0.000001; a0=1; a1=-0.5; a2=2; b0=0; b1=1; b2=4;
- MKR(funcP, funcG, funcF, a, b, h, a0, a1, a2, b0, b1, b2, y);
- MKR(funcP, funcG, funcF, a, b, h/2, a0, a1, a2, b0, b1, b2, x);
- resultOutput(y, a, b, h);
- printf("\nЏ®ЈаҐи®бвм: %10.6f\n", Compare(y,x,a,b,h)/3);
- break;
- }
- case 54:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b: ");
- scanf("%lg %lg", &a, &b);
- eps=0.001; h=0.05; a0=1; a1=-0.5; a2=2; b0=0; b1=1; b2=4;
- runMethod(funcP, funcG, funcF, a, b, h, a0, a1, a2, b0, b1, b2, y);
- runMethod(funcP, funcG, funcF, a, b, h/2, a0, a1, a2, b0, b1, b2, x);
- resultOutput(y, a, b, h);
- printf("\nЏ®ЈаҐи®бвм: %10.6f\n", Compare(y,x,a,b,h)/3);
- break;
- }
- case 55:
- {
- printf("\n\n‚ўҐ¤ЁвҐ a, b, h: ");
- scanf("%lg %lg %lg", &a, &b, &h);
- eps=0.000001; g1=0; g2=1; g3=1.2; g4=1; g5=0; g6=2.1;
- shootMethod(y1, y2, a, b, h, eps, g1, g2, g3, g4, g5, g6, x, y);
- output(x, y, a, b, h);
- break;
- }
- }
- printf("\n\nЌ ¦¬ЁвҐ «оЎго Є« ўЁиг...");
- getch();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment