Clear Filters
Clear Filters

Unregcognized function or variable 'xotold'

1 view (last 30 days)
Gerardo
Gerardo on 18 Feb 2024
Commented: Walter Roberson on 18 Feb 2024
Im trying to get this function to print out the xopt and fopt but i dont know what other inputs I need to put in. How could i fix the error on xoptold?
format short
x = linspace(0,10);
xl = 0; xu = 10;
g = @(x) (2*x) ./ (4+0.8*x + x^2 + 0.2*x^3);
[xopt, fopt] = MinParabolicInterpolation(g, xl, xu)
%%Unregcognized function or variable 'xotold'
%error in MinParabolicInterpolation (line 29)
plot(x,g,fopt,'*g')
xlabel('c(mg/L)');
ylabel('g (d-1)');
title('Yeast Growth Rate versus Antibiotic Concentration');
function [xopt,fopt]=...
MinParabolicInterpolation(func,xlow,xhigh,es,maxit,varargin)
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4||isempty(es), es=0.0001;end
if nargin<5||isempty(maxit), maxit=50;end
iter = 0;
x1 = xlow; x3 = xhigh;
x2 = (x1 + x3) / 2;
f1 = func(x1,varargin{:});
f2 = func(x2,varargin{:});
f3 = func(x3,varargin{:});
if f2<f1 && f2<f3, xoptold = x2; end
while(1)
xopt=x2-0.5*((x2-x1)^2*(f2-f3)-(x2-x3)^2* ...
(f2-f1))/((x2-x1)*(f2-f3)-(x2-x3)*(f2-f1));
fopt = func(xopt,varargin{:});
iter = iter + 1;
if xopt > x2
x1 = x2;
f1 = f2;
else
x3 = x2;
f3 = f2;
end
x2 = xopt; f2 = fopt;
if xopt~=0,ea=abs((xopt - xoptold) / xopt) * 100;end
xoptold = xopt;
if ea<=es || iter>=maxit,break,end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 18 Feb 2024
if f2<f1 && f2<f3,   xoptold = x2; end

You only set xtoptold if that condition is true, but later you use it even if the condition was false

  2 Comments
Gerardo
Gerardo on 18 Feb 2024
how would I fix it so that it would run correctly?
Walter Roberson
Walter Roberson on 18 Feb 2024
Put an else on the
if f2<f1 && f2<f3, xoptold = x2; end
and refuse to run the while loop if the else is encountered.

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!