Error while writing code for bounding phase algorithm

3 views (last 30 days)
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

Prem Kumar Tiwari
Prem Kumar Tiwari on 28 Sep 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 Comment
Walter Roberson
Walter Roberson on 28 Sep 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!