Borrar filtros
Borrar filtros

Error while writing code for bounding phase algorithm

5 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Prem Kumar Tiwari
Prem Kumar Tiwari el 28 de Sept. de 2018
It would've been great if you could've stated the error that Matlab shows. However, the issue is most probably because in a call to feval, the first argument has to be either a string that is name of the function like 'objF' or handle of the function like '@objF' without quotes of course.
To learn more about feval, check out the documentation here at : feval - Documentation
  1 comentario
Walter Roberson
Walter Roberson el 28 de Sept. de 2018
Right.
These days if you are given a function handle, you should typically use function syntax instead of feval. Like if
F = @objF;
then instead of
F_x0=feval(objF,x0);
you should code
F_x0 = F(x0);
feval() should be reserved for one of two cases:
1) You are doing some wizardry involving the symbolic engine, in which case your command would start feval(symengine, ....)
2) You are specifically trying to design a program that has to be able to accept function names as character vectors instead of the better function handles. Using function names as character vectors has a bunch of limitations, and is not recommended, but some people are given mandates to program for compatibility with MATLAB 4 before function handles existed.

Iniciar sesión para comentar.

Más respuestas (1)

ADITYA SHARMA
ADITYA SHARMA el 24 de Feb. de 2022
%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

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by