Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.32 KB | None | 0 0
  1. clc
  2. a = -0.3; 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 = 16*x.^6+10*x.^4+4*x.^3+21*x.^2+17*x+sin(12*x)+2*sin(10*x)+sin(18*x)+2*sin(13*x);
  14. func_call = func_call + 1;
  15. end
  16.  
  17. function value = dy(x)
  18. global func_call
  19. value = 17+42*x+12*x.^2+40*x.^3+96*x.^5+20*cos(10*x)+12*cos(12*x)+26*cos(13*x)+18*cos(18*x);
  20. func_call = func_call + 1;
  21. end
  22.  
  23. function value = ddy(x)
  24. global func_call
  25. value = 2*(21+12*x+60*x.^2+240*x.^4-100*sin(10*x)-72*sin(12*x)-169*sin(13*x)-162*sin(18*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];
  157. index = 2;
  158. while (b-a)/r_d((a+b)/2) > fib_array(index)
  159.     index = index + 1;
  160.     fib_array(index) = fib_array(index-2) + fib_array(index-1);
  161. end
  162.  
  163. i = index;
  164. c = a + (b-a)*fib_array(i-2)/fib_array(i);
  165. d = a + (b-a)*fib_array(i-1)/fib_array(i);
  166. y_c = y(c);
  167. y_d = y(d);
  168. while b-a > 2*r_d((a+b/2))
  169.     if y_c <= y_d
  170.         b = d;
  171.         d = c;
  172.         y_d = y_c;
  173.         c = a + (b-a)*fib_array(i-2)/fib_array(i);
  174.         y_c = y(c);
  175.         argument = (a+b)/2;
  176.         min = y(argument);
  177.         counter = counter + 1;
  178.         i = i - 1;
  179.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  180.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  181.     else
  182.         a = c;
  183.         c = d;
  184.         y_c = y_d;
  185.         d = a + (b-a)*fib_array(i-1)/fib_array(i);
  186.         y_d = y(d);
  187.         argument = (a+b)/2;
  188.         min = y(argument);
  189.         counter = counter + 1;
  190.         i = i - 1;
  191.         formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  192.         fprintf(formatSpec, argument, min, counter, func_call, a, b)
  193.     end
  194. end
  195. if y(c) < min
  196.     counter = counter + 1;
  197.     argument = c;
  198.     min = y(c);
  199.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  200.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  201. elseif y(d) < min
  202.     counter = counter + 1;
  203.     argument = d;
  204.     min = y(d);
  205.     formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
  206.     fprintf(formatSpec, argument, min, counter, func_call, a, b)
  207. end
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement