Question

In: Computer Science

Give me a working MATLAB code for the Golden section search method . It should be...

Give me a working MATLAB code for the Golden section search method . It should be working

Dont answer if you do not know,

the code must work for the golden section method

Solutions

Expert Solution

clc; clear all; clc;


phi = double( (sqrt(5)-1) / 2 ); % golden ratio
del = .05;% small increment value
epsilon = .001; % function difference precision

max_iter = 100; % maximum number of iterations for Phase I and II.

alpha(1) = 0; % first value of alpha
alpha(2) = del; % second value of alpha is equal to the small increment value


% begin iterations for Phase I
i_iter = 1;
while i_iter <= max_iter
  
if ( f(alpha(i_iter)) > f(alpha(i_iter + 1)) )
alpha(i_iter + 2) = 0;

for j_iter = 1 : i_iter+1
alpha(i_iter + 2) = alpha(i_iter + 2) + del*(1.618)^(j_iter-1);
end
else
break
end
  
i_iter = i_iter + 1; % increment number of iterations by 1
end

% the uncertainity interval
alpha_l = 0; % set lower bound of alpha since it cannot be known how many alphas will be enough, i.e. alpha(i_iter - 2) may be null.
alpha_u = alpha(i_iter + 1);

% plot the line search function of one variable (i.e., alpha - a)
figure; hold on;
syms a;
fplot(@f,[alpha_l alpha_u],'b');
% plot title and axes labels
title( 'Golden section search on f ( alpha )' );
xlabel('alpha');
ylabel('f (alpha)');

% divide uncertainity interval using golden ratio, (i.e., set new interval
% boundaries)
a = alpha_l + (1 - phi)*(alpha_u - alpha_l);  
b = alpha_l + phi*(alpha_u - alpha_l);

% calculate function values at points
f_a = f(a);
f_b = f(b);

% plot new interval boundaries on top of the function plot.
plot(a,f_a,'rd')
plot(b,f_b,'rd')


% begin iterations for Phase II
i_iter = 1;
while ( ( abs(alpha_u-alpha_l) > epsilon ) && (i_iter < max_iter) ) && (i_iter < max_iter)
if(f_a < f_b)
alpha_u = b;
b = a;
a = alpha_l + (1 - phi)*(alpha_u - alpha_l);
  
f_a=f(a);
f_b=f(b);
  
plot(a,f_a,'rd');
else
alpha_l = a;
a = b;
b = alpha_l + phi*(alpha_u - alpha_l);
  
f_a = f(a);
f_b = f(b);
  
plot(b, f_b, 'rd')
end
end

% print alpha value that minimizes the line search function
if(f_a<f_b)
fprintf('\nalpha_min = %f\n', a)
fprintf('f_min = %f\n', f_a)
plot(a,f_a,'kd', 'LineWidth', 1, 'MarkerSize', 10, 'MarkerFaceColor' , 'g')
  
else
fprintf('\nalpha_min = %f\n', b)
fprintf('f_min = %f\n', f_b)
plot(b,f_b,'kd', 'LineWidth', 1, 'MarkerSize', 10, 'MarkerFaceColor' , 'g')
end


Related Solutions

Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine...
Give me a working code in MATLAB for Crout Decomposition. The code must work totally fine and must break into 2 matrix L and U It must be in MATLAB The code used must use continue statement. If no continue statement is there in code it will be downvoted. Answer only if you know or else i will dislike badly
I need a working MATLAB CODE for the Gram Schimdt process Please give the code fast...
I need a working MATLAB CODE for the Gram Schimdt process Please give the code fast Its urgent The code must run on any matrix given It should be a generic code Dont answer any wrong code or else i will badly dislike
Give me a MATLAB code which integrates any function using Romberg integration The code must work...
Give me a MATLAB code which integrates any function using Romberg integration The code must work fine or else I will down vote the answer badly Dont post if you dont know
matlab code that performs overlap add method.
matlab code that performs overlap add method.
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take...
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take the following parameters: • F: A function (assumed to be continuous) whose roots you want to find, • a: A floating-point number giving the left endpoint of the initial interval in which you want to search for a root of F. • b: A floating-point number giving the right endpoint of the initial interval. • delta: A non-negative floating-point number giving the acceptable proximity...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
Could you please also give me a code that does not use the string split method...
Could you please also give me a code that does not use the string split method but instead gives an input and output file that you have to compile using "type output.txt" in java. This is the code: Record.java public class Record { private String stateCode, districCode, districtName; private int totalPopulation, childPopulation, childPovertyPopulation; private String miscStats; public Record(String stateCode, String districCode, String districtName, int totalPopulation, int childPopulation, int childPovertyPopulation, String miscStats) { this.stateCode = stateCode; this.districCode = districCode; this.districtName =...
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
Description: The search method should prompt the user for a search phrase. Given the search phrase...
Description: The search method should prompt the user for a search phrase. Given the search phrase the search method should search each line in the file for the location of the phrase on each line. If the search phrase is not found on a line then just go to the next line. The search method must find every occurrence of the phrase on a line. The search method should print the line number, the line and the location of the...
who can give me the example like this:(also give me the answer,Tell me what formula should...
who can give me the example like this:(also give me the answer,Tell me what formula should use to calculate and how to use those formulas.)thank you Known: risk profolio. expect return and variance of stock A. expect return and variance of stock B. The yield correlation coefficient of two stocks. Risk combination characteristics Question: expect return. variance. Build a complete portfolio according to the investor's requirements. How many should investors invest in risk portfolios and security assets(T-bill). Calculating the sharp...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT