Advertisement
Guest User

task1complete

a guest
Dec 5th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.03 KB | None | 0 0
  1. clc
  2. a = -1.5; b = -0.5;
  3. delta = 0.1;
  4. Passive_Search(a, b, 10^-4)
  5. Dichotomia(a, b, delta)
  6. Gold_Section(a, b)
  7. Fibonacci(a, b)
  8. Newton(a, b)
  9.  
  10.  
  11. function value = y(x)
  12. global func_call
  13. value = 26*x.^6 + 21*x.^5 + 18*x.^4 + 4*x.^3 + 10*x.^2 + 15*x.^1 + sin(13*x) + sin(6*x) + sin(3*x);
  14. func_call = func_call + 1;
  15. end
  16.  
  17. function value = dy(x)
  18. global func_call
  19. value = 156*x.^5 + 105*x.^4 + 72*x.^3 + 12*x.^2 + 20*x.^1 + 15 + 13*cos(13*x) + 6*cos(6*x) + 3*cos(3*x);
  20. func_call = func_call + 1;
  21. end
  22.  
  23. function value = ddy(x)
  24. global func_call
  25. value = 780*x.^4 + 420*x.^3 + 216*x.^2 + 24*x.^1 + 20 - 169*sin(13*x) - 36*sin(6*x) - 9*sin(3*x);
  26. func_call = func_call + 1;
  27. end
  28.  
  29. function value = r_d(number)
  30. exponent = floor(log10(abs(number))); % порядок старшей цифры
  31. amount = 4; % нужное количество верных цифр
  32. value = 10^(exponent - amount + 1)/2;
  33. end
  34.  
  35. function Passive_Search(a, b, epsilon)
  36. introduction = sprintf('\t\t\t\t Метод пассивного поиска\n');
  37. fprintf(introduction)
  38. counter = 0;
  39. min = y(a);
  40. for i = a:epsilon:b      
  41.     if y(i)<min
  42.         min = y(i);
  43.         argument = i;
  44.     end
  45.     counter = counter + 1;
  46. end
  47. formatSpec = 'X is %.3f, Y is %.3f, Count is %.f \n';
  48. fprintf(formatSpec, argument, min, counter-1)
  49. end
  50.  
  51. function Dichotomia(a, b, delta)
  52. introduction = sprintf('\t\t\t\t Метод дихотомии\n');
  53. fprintf(introduction)
  54. global func_call
  55. counter = 0;
  56. func_call = 0;
  57. while b-a > 2*r_d((a+b/2))
  58.     Delta_b = delta*(b-a);
  59.     c = (a+b)/2 - Delta_b;
  60.     d = (a+b)/2 + Delta_b;
  61.     if y(c) <= y(d)
  62.         counter = counter + 1;
  63.         b = d;
  64.         argument = c;
  65.         min = y(c);
  66.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  67.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  68.     else
  69.         counter = counter + 1;
  70.         a = c;
  71.         argument = d;
  72.         min = y(d);
  73.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  74.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  75.     end
  76. end
  77.  
  78. while b-a > 6*Delta_b
  79.     Delta_b = delta*(b-a);
  80.     c = (a+b)/2 - Delta_b;
  81.     d = (a+b)/2 + Delta_b;
  82.     if y(c) <= y(d)
  83.         counter = counter + 1;
  84.         b = d;
  85.         argument = c;
  86.         min = y(c);
  87.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  88.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  89.     else
  90.         counter = counter + 1;
  91.         a = c;
  92.         argument = d;
  93.         min = y(d);
  94.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  95.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  96.     end
  97. end
  98. end
  99.  
  100. function Gold_Section(a, b)
  101. introduction = sprintf('\t\t\t\t Метод золотого сечения\n');
  102. fprintf(introduction)
  103. counter = 0;
  104. global func_call
  105. func_call = 0;
  106. c = (3-sqrt(5))/2*(b-a) + a;
  107. d = (sqrt(5)-1)/2*(b-a) + a;
  108. y_c = y(c);
  109. y_d = y(d);
  110. while b-a > 2*r_d((a+b)/2)
  111.     if y_c <= y_d
  112.         b = d;
  113.         d = c;
  114.         y_d = y_c;
  115.         c = (3-sqrt(5))/2*(b-a) + a;
  116.         y_c = y(c);
  117.         argument = (a+b)/2;
  118.         min = y(argument);
  119.         counter = counter + 1;
  120.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  121.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  122.     else
  123.         a = c;
  124.         c = d;
  125.         y_c = y_d;
  126.         d = (sqrt(5)-1)/2*(b-a) + a;
  127.         y_d = y(d);
  128.         argument = (a+b)/2;
  129.         min = y(argument);
  130.         counter = counter + 1;
  131.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  132.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  133.     end
  134. end
  135. if y(c) < min
  136.     counter = counter + 1;
  137.     argument = c;
  138.     min = y(c);
  139.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  140.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  141. elseif y(d) < min
  142.     counter = counter + 1;
  143.     argument = d;
  144.     min = y(d);
  145.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  146.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  147. end
  148. end
  149.  
  150. function Fibonacci(a, b)
  151. introduction = sprintf('\t\t\t\t Метод Фибоначчи\n');
  152. fprintf(introduction)
  153. global func_call
  154. func_call = 0;
  155. counter = 0;
  156. fib_array = [1 1 2 3 5];
  157. index = 2;
  158. f1 = 1;
  159. f2 = 1;
  160. f3 = 2;
  161. while (b-a)/r_d((a+b)/2) > f3
  162.     index = index + 1;
  163.     f1 = f2;
  164.     f2 = f3;
  165.     f3 = f1 + f2;
  166. end
  167.  
  168. i = index;
  169. c = a + (b-a)*f1/f3;
  170. d = a + (b-a)*f2/f3;
  171. y_c = y(c);
  172. y_d = y(d);
  173. while b-a > 2*r_d((a+b/2))
  174.     if y_c <= y_d
  175.         b = d;
  176.         d = c;
  177.         y_d = y_c;
  178.         c = a + (b-a)*f1/f3;
  179.         y_c = y(c);
  180.         argument = (a+b)/2;
  181.         min = y(argument);
  182.         counter = counter + 1;
  183.         i = i - 1;
  184.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  185.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  186.     else
  187.         a = c;
  188.         c = d;
  189.         y_c = y_d;
  190.         d = a + (b-a)*f2/f3;
  191.         y_d = y(d);
  192.         argument = (a+b)/2;
  193.         min = y(argument);
  194.         counter = counter + 1;
  195.         i = i - 1;
  196.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  197.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  198.     end
  199. end
  200. if y(c) < min
  201.     counter = counter + 1;
  202.     argument = c;
  203.     min = y(c);
  204.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  205.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  206. elseif y(d) < min
  207.     counter = counter + 1;
  208.     argument = d;
  209.     min = y(d);
  210.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  211.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  212. end
  213. end
  214.  
  215. function Newton(a, b)
  216. introduction = sprintf('\t\t\t\t Метод касательных(Ньютона)\n');
  217. fprintf(introduction)
  218. dddy = @(x) 24+432*x+1260*x.^2+3120*x.^3-169*13*cos(13*x)-216*cos(6*x)-27*cos(3*x);
  219. if dy(a)*dddy(a) > 0
  220.     x0 = a;
  221.     x1 = b;
  222. else
  223.     x0 = b;
  224.     x1 = a;
  225. end
  226. global func_call
  227. func_call = 2;
  228. counter = 0;
  229. current_epsilon = realmax;
  230. while current_epsilon > r_d((a+b/2))
  231.     counter = counter + 1;
  232.     x1 = x0 - dy(x0)/ddy(x0);
  233.     current_epsilon = abs(x1 - x0);
  234.     minumum = y(x1);
  235.     tmp = [x0, x1];
  236.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  237.     fprintf(formatSpec, x1, minumum, counter, func_call, min(tmp), max(tmp))
  238.     x0 = x1;
  239. end
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement