Output argument "x3" (and maybe others) not assigned during call to "threeptb"

1 view (last 30 days)
So I am trying to use three point bracketing function to get values of x and f as shown in the code. The code works for one of my test scripts but not the other and returns the error "Output argument "x3" (and maybe others) not assigned during call to ..". Any idea where i missed something?
This is the function with the "f"
function [f] = obj(x)
%f = 240*(x^-1) + 10^(-4)*x^(1.5) + 0.45;
K = 1.11 + 1.11*(x/10)^(-0.18);
f = K/(10-x);
end
this is the 3 point bracketing function
function [x1,x2,x3,f2,f3] = threeptb(x1,s,tau)
%steps as defined by the three point bracketing algorithm
f1 = obj(x1);
x2 = x1 + s;
f2 = obj(x2);
while f2 <= f1
s = s/tau;
x3 = x2 + s;
f3 = obj(x3);
if f2 > f1
g = f1;
f1 = f2;
f2 = g;
s = -s;
elseif f3 > f2
return
else
x1 = x2;
x2 = x3;
f2 = f3;
end
end
and the error is returned when i call
[q,w,e,r,t] = threeptb(1,0.1,0.618)

Answers (1)

dpb
dpb on 15 Feb 2021
I just worked through at command line --
>> fnK =@(x) (1.11 + 1.11*(x/10)^(-0.18))/(10-x); % the obj() function
>> x1=1; s=0.1; % the given arguments needed
>> f1=fnK(x1)
f1 =
0.3100
>> x2=x1+s;
>> f2=fnK(x2)
f2 =
0.3103
>> f2<=f1
ans =
logical
0
>>
shows that the while condition will not be met and so the function will return without having computed x3, f3
You'll need to set some default values to return or fix the logic if the condition above isn't supposed to occur.
Setting a breakpoint in your function and stepping through to check logic would help in finding such things...

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!