Advertisement
Pafnytiu

Методы вычислений 1

Oct 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication2
  8. {
  9. class Program
  10. {
  11.  
  12. static double MyFunc(double x)
  13. {
  14. return x * Math.Cos(x);
  15. }
  16. //x*Math.Cos(x)
  17.  
  18. static double FuncPlus(double next, double current, double h)
  19. {
  20. return next - current / h;
  21. }
  22.  
  23. static double FuncMinus(double previous, double current, double h)
  24. {
  25. return current - previous / h;
  26. }
  27.  
  28. static double FuncMiddle(double next, double previous, double h)
  29. {
  30. return next - previous / 2 * h;
  31. }
  32.  
  33. static void Main(string[] args)
  34. {
  35. double e = 0.0001, a = 0.0, b = 1.7, x = 0, h = 0.1, sum = 0;
  36. for (x = a; x <= b; x += h)
  37. {
  38. double current = x;
  39. double next = 1;
  40. double previous = 1;
  41. double Plus = 0, Minus = 0, Middle = 0, Func = 0;
  42. int k = 0;
  43. while (Math.Abs(current) >= e)
  44. {
  45. sum += current;
  46. previous = current;
  47. Minus = FuncMinus(previous, current, h);
  48. next = current * (-((x * x) / (2 * (k + 1) * (2 * k + 1))));
  49. Plus = FuncPlus(next, current, h);
  50. current = next;
  51.  
  52. Middle = FuncMiddle(next, previous, h);
  53. Func = MyFunc(x);
  54. k++;
  55. }
  56. Console.WriteLine("x: {0} \t sum: {1} \t f(x): {2} \t f_+(x): {3} \t f_-(x): {4} \t f_0(x): {5}", x, sum, Func, Plus, Minus, Middle);
  57. sum = 0;
  58. }
  59. Console.Read();
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement