Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- a = -0.3; 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 = 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);
- func_call = func_call + 1;
- end
- function value = dy(x)
- global func_call
- 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);
- func_call = func_call + 1;
- end
- function value = ddy(x)
- global func_call
- 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));
- 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];
- index = 2;
- while (b-a)/r_d((a+b)/2) > fib_array(index)
- index = index + 1;
- fib_array(index) = fib_array(index-2) + fib_array(index-1);
- end
- i = index;
- c = a + (b-a)*fib_array(i-2)/fib_array(i);
- d = a + (b-a)*fib_array(i-1)/fib_array(i);
- 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)*fib_array(i-2)/fib_array(i);
- 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)*fib_array(i-1)/fib_array(i);
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement