Unregcognized function or variable 'xotold'

조회 수: 1 (최근 30일)
Gerardo
Gerardo 2024년 2월 18일
댓글: Walter Roberson 2024년 2월 18일
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