Error while writing code for bounding phase algorithm
Show older comments
x0=0;
step_size=input('Enter step size ');
a=x0-abs(step_size);
b=x0+abs(step_size);
F_x0=feval(objF,x0);
F_a=feval(objF,a);
F_b=feval(objF,b);
figure; hold on;
if (F_a>=F_x0 && F_x0>=F_b)
step_size = 1*step_size;
else
step_size = -step_size;
end
k=0;
F_p = feval(objF,k);
F_q = feval(objF,k+1);
for k = 0:10
if F_q<F_p
x(0)=zeros(1, 1000000);
x(k+1)=x(k)+2^k*step_size;
else
xleft=x(k-1);
xright=x(k+1);
end
end
ObjF is
function y=objF(x)
y=x^5-5*x^3-20*x+5;
Now matlab states that there are errors at F_x0=feval(objF,x0); and at y=x^5-5*x^3-20*x+5;
I absolutely cannot fathom the reason for these error messages. Can someone help me?
Accepted Answer
More Answers (1)
ADITYA SHARMA
on 24 Feb 2022
0 votes
%Try this code.
clc;
clear;
global nf;
nf = 0;
k = 0;
syms x;
fx = input('Enter function in x: ');
b = 2; %Enter base for the exponent;
disp('f(x) = '); disp(fx);
while (1)
x0 = input('Enter initial guess value: ');
d = input('Enter a positive value for increment: ');
xi = [x0-d; x0; x0+d];
fx1 = eval(subs(fx,x,xi(1)));
fx2 = eval(subs(fx,x,xi(2)));
fx3 = eval(subs(fx,x,xi(3)));
fx_eval = [fx1; fx2; fx3];
disp('The column vector[x0-d; x0; x0+d] is xi = ');disp(xi);
disp('The column vector[f(x0-d); f(x0); f(x0+d)] is f(x) = '); disp(fx_eval);
if (fx1>=fx2 && fx2>=fx3)
break;
elseif (fx1<=fx2 && fx2<=fx3)
d = -1*d;
break;
else
disp('Enter new set of intial values: ');
end
end
xi(1) = x0-d;
fprintf('Intial value x0 = %.3f\n',x0);
fprintf('Increment d = %.3f\n',d);
fprintf('=====================================================================================================================================================|\n');
fprintf('\t|Iteration number| \t\t\t\t |x_values|\t\t\t\t\t\t\t\t |Function Evaluations at x_values|\t\t\t\t\t\t |Length|\n');
fprintf('=====================================================================================================================================================|\n');
while(1)
xi(3) = xi(2)+d*b^k;
fx1 = eval(subs(fx,x,xi(1)));
fx2 = eval(subs(fx,x,xi(2)));
fx3 = eval(subs(fx,x,xi(3)));
fx_eval = [fx1; fx2; fx3];
%fx3 = eval(subs(fx,x,xi(3)));
fprintf('\tk = %3d\t\t | \t',k);
fprintf('\t%f\t ',xi);
fprintf('|');
fprintf('\t\t%f',fx_eval); fprintf('\t|'); fprintf('\t\t%f',(xi(3)-xi(2)));
fprintf('\n');fprintf('-----------------------------------------------------------------------------------------------------------------------------------------------------|\n');
% disp('xi = ');disp(xi); disp('f(x) = '); disp(fx_eval);
if (fx3<fx2)
k = k+1;
xi(1) = xi(2); xi(2) = xi(3);
fx1 = fx2;fx2 = fx3;
nf = nf+1;
else
if d>0
fprintf('Minimum point lies in the interval (%.3f,%.3f)\n',xi(1),xi(3));
else
fprintf('Minimum point lies in the interval (%.3f,%.3f)\n',xi(3),xi(1));
end
fprintf('No. of iterations = %d \n',k);
fprintf('No. of function evaluations = %d \n',nf);
break;
end
end
Categories
Find more on Image Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!