Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- a = -1.5; b = -0.5;
- delta = 0.1;
- Passive_Search(a, b, 10^-4)
- Dichotomia(a, b, delta)
- Gold_Section(a, b)
- Fibonacci(a, b)
- Newton(a, b)
- function value = y(x)
- global func_call
- 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);
- func_call = func_call + 1;
- end
- function value = dy(x)
- global func_call
- 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);
- func_call = func_call + 1;
- end
- function value = ddy(x)
- global func_call
- 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);
- func_call = func_call + 1;
- end
- function value = r_d(number)
- exponent = floor(log10(abs(number))); % порядок старшей цифры
- amount = 4; % нужное количество верных цифр
- value = 10^(exponent - amount + 1)/2;
- end
- function Passive_Search(a, b, epsilon)
- introduction = sprintf('\t\t\t\t Метод пассивного поиска\n');
- fprintf(introduction)
- counter = 0;
- min = y(a);
- for i = a:epsilon:b
- if y(i)<min
- min = y(i);
- argument = i;
- end
- counter = counter + 1;
- end
- formatSpec = 'X is %.3f, Y is %.3f, Count is %.f \n';
- fprintf(formatSpec, argument, min, counter-1)
- end
- function Dichotomia(a, b, delta)
- introduction = sprintf('\t\t\t\t Метод дихотомии\n');
- fprintf(introduction)
- global func_call
- counter = 0;
- func_call = 0;
- while b-a > 2*r_d((a+b/2))
- Delta_b = delta*(b-a);
- c = (a+b)/2 - Delta_b;
- d = (a+b)/2 + Delta_b;
- if y(c) <= y(d)
- counter = counter + 1;
- b = d;
- argument = c;
- min = y(c);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- else
- counter = counter + 1;
- a = c;
- argument = d;
- min = y(d);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- while b-a > 6*Delta_b
- Delta_b = delta*(b-a);
- c = (a+b)/2 - Delta_b;
- d = (a+b)/2 + Delta_b;
- if y(c) <= y(d)
- counter = counter + 1;
- b = d;
- argument = c;
- min = y(c);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- else
- counter = counter + 1;
- a = c;
- argument = d;
- min = y(d);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- end
- function Gold_Section(a, b)
- introduction = sprintf('\t\t\t\t Метод золотого сечения\n');
- fprintf(introduction)
- counter = 0;
- global func_call
- func_call = 0;
- c = (3-sqrt(5))/2*(b-a) + a;
- d = (sqrt(5)-1)/2*(b-a) + a;
- y_c = y(c);
- y_d = y(d);
- while b-a > 2*r_d((a+b)/2)
- if y_c <= y_d
- b = d;
- d = c;
- y_d = y_c;
- c = (3-sqrt(5))/2*(b-a) + a;
- y_c = y(c);
- argument = (a+b)/2;
- min = y(argument);
- counter = counter + 1;
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- else
- a = c;
- c = d;
- y_c = y_d;
- d = (sqrt(5)-1)/2*(b-a) + a;
- y_d = y(d);
- argument = (a+b)/2;
- min = y(argument);
- counter = counter + 1;
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- if y(c) < min
- counter = counter + 1;
- argument = c;
- min = y(c);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- elseif y(d) < min
- counter = counter + 1;
- argument = d;
- min = y(d);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- function Fibonacci(a, b)
- introduction = sprintf('\t\t\t\t Метод Фибоначчи\n');
- fprintf(introduction)
- global func_call
- func_call = 0;
- counter = 0;
- fib_array = [1 1 2 3 5];
- index = 2;
- f1 = 1;
- f2 = 1;
- f3 = 2;
- while (b-a)/r_d((a+b)/2) > f3
- index = index + 1;
- f1 = f2;
- f2 = f3;
- f3 = f1 + f2;
- end
- i = index;
- c = a + (b-a)*f1/f3;
- d = a + (b-a)*f2/f3;
- y_c = y(c);
- y_d = y(d);
- while b-a > 2*r_d((a+b/2))
- if y_c <= y_d
- b = d;
- d = c;
- y_d = y_c;
- c = a + (b-a)*f1/f3;
- y_c = y(c);
- argument = (a+b)/2;
- min = y(argument);
- counter = counter + 1;
- i = i - 1;
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- else
- a = c;
- c = d;
- y_c = y_d;
- d = a + (b-a)*f2/f3;
- y_d = y(d);
- argument = (a+b)/2;
- min = y(argument);
- counter = counter + 1;
- i = i - 1;
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- if y(c) < min
- counter = counter + 1;
- argument = c;
- min = y(c);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- elseif y(d) < min
- counter = counter + 1;
- argument = d;
- min = y(d);
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, argument, min, counter, func_call, a, b)
- end
- end
- function Newton(a, b)
- introduction = sprintf('\t\t\t\t Метод касательных(Ньютона)\n');
- fprintf(introduction)
- dddy = @(x) 24+432*x+1260*x.^2+3120*x.^3-169*13*cos(13*x)-216*cos(6*x)-27*cos(3*x);
- if dy(a)*dddy(a) > 0
- x0 = a;
- x1 = b;
- else
- x0 = b;
- x1 = a;
- end
- global func_call
- func_call = 2;
- counter = 0;
- current_epsilon = realmax;
- while current_epsilon > r_d((a+b/2))
- counter = counter + 1;
- x1 = x0 - dy(x0)/ddy(x0);
- current_epsilon = abs(x1 - x0);
- minumum = y(x1);
- tmp = [x0, x1];
- formatSpec = 'X is %.4f, Y is %.3f, Count is %.f, Number of function calls %.f, Segment is [%.4f, %.4f] \n';
- fprintf(formatSpec, x1, minumum, counter, func_call, min(tmp), max(tmp))
- x0 = x1;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement