Unregcognized function or variable 'xotold'

2 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2024-2-18
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 个评论
Gerardo
Gerardo 2024-2-18
how would I fix it so that it would run correctly?
Walter Roberson
Walter Roberson 2024-2-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by