• API
• FAQ
• Tools
• Archive
daily pastebin goal
51%
SHARE
TWEET

# Untitled

a guest Apr 16th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <math.h>
3.
4. //dy/dt = -y(t)+ e^-t * cos(t)
5.
6. double h, fx, fy;
7. double euler(double t0, double y0, double h, double(*f)(double , double ));
8. double f(double fx, double fy);
9. double der(double t, double y);
10. double sol(double t, double y);
11. double rk(double x0, double y0, double h, double(*f)(double , double ));
12. double error(double t0, double y0, double(*f)(double, double), double(*g)(double, double, double , double));
13.
14. void main()
15. {
16.    // FILE *fi;
17.     int num;
18.     double t0 = 0;
19.     double y0 = 0;
20.     printf("state number of steps = ");
21.     scanf("%d", &num);
22.     printf("\nand their size =");
23.     scanf("%lf", &h);
24.    // fi = fopen("graph.xls", "w");
25.     for(int i = 0; i < num; i++)
26.     {
27.         y0 = rk(t0, y0, h, der);
28.         t0 += h;
29.         printf("y %d = %lf t %d = %lf",i,y0,i,t0);
30.         printf("error = %lf \n",fabs((sol(t0 + h, y0)-rk(t0, y0, h, der))/sol(t0 + h, y0)));
31.       //  fprintf(*fi,"%lf %lf %lf \n", t0, y0, fabs((sol(t0, y0)-rk(t0, y0, h, der))/rk(t0, y0, h, der)));
32.     }
33.     t0 = 0;
34.      for(int i = 0; i < num; i++)
35.     {
36.         y0 = euler(t0, y0, h, der);
37.         t0 += h;
38.         printf("y %d = %lf t %d = %lf ",i,y0,i,t0);
39.         printf("error = %lf \n",fabs((sol(t0 +h, y0)-euler(t0, y0, h, der))/sol(t0 + h, y0)));
40.     }
41.
42. //tell it to draw graph in fi
43.   //   fclose(fi);
44. }
45.
46. double euler(double t0, double y0, double h, double(*f)(double , double ))
47. {
48.    return y0 + h*f(t0, y0);
49. }
50.
51. double der(double t, double y)
52. {
53. return -1*y + cos(t)* exp(-1*t);
54. }
55.
56. double sol(double t, double y)
57. {
58. return sin(t)* exp(-1*t);
59. }
60.
61. double rk(double t0, double y0, double h, double(*f)(double , double))
62. {
63.     double q1 = h * f(t0, y0);
64.     double q2 = h * f(t0 + h/2, y0 + q1/2);
65.     double q3 = h * f(t0 + h/2, y0 + q2/2);
66.     double q4 = h * f(t0 +h, y0 + q3);
67.     return y0 + (q1 + q2 + q3 + q4)/6;
68. }
69. /*
70. double error(double t0, double y0, double(*f)(double, double), double(*g)(double, double, double , double(*asd)(double t0, double y0))
71. {
72. return abs((f(t0, y0)- g(t0, y0, h, asd(t0, y0))/f(t0, y0));
73. }*/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top