二分法:无法使用函数以及如何处理多个函数输入
Bisection Method: Unable to use functions and how to handle multiple function inputs
我有一个有效的二分法,但我需要在一个函数中实现它,该函数将接受三个参数:f 正在使用的函数,xL 是边界的左侧,xR 是边界的右侧。此外,它必须能够根据 x 的值更改函数(例如,如果函数是 f(x))。如果 x 小于或等于零,则 f(x) 将为 x^3 + 3x + 1,如果 x 大于零,则 f(x) 将为 1 + sin(x)。函数需要输出根和执行的迭代次数
如何传递一个函数,因为 f 是函数参数,然后如何找到 x 的值以确定使用哪个函数?
以下是我在控制台输入中遇到的错误:
>> f = inline('x^3 + 3x + 1', 'x')
f =
Inline function:
f(x) = x^3 + 3x + 1
>> bisectionF(f,-2,0.1)
Undefined function or variable 'bisectionF'.
>> bisectionF(f,-2,0.1)
Undefined function or variable 'B'.
Error in bisectionF (line 3)
maxIters = 20;B
>> bisectionF(f,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in bisectionF (line 7)
fM = feval(f,xM);
>> y = bisectionF('f1',-2,0.1)
Error using feval
Undefined function 'f1' for input arguments of type 'double'.
Error in bisectionF (line 9)
fM = feval(f,xM);
>> f1 = inline('x^3 + 3x + 1', 'x');
>> root = bisectionF(f1,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in bisectionF (line 9)
fM = feval(f,xM);
这是我的代码:
function[r, iters] = bisectionF(f,xL,xR)
%f1 = inline('x^3 + 3x + 1', 'x');
%f2 = inline('1+sin(x)','x');
maxIters = 20;
precision = 10^(-5);
for j = 1:maxIters
xM=(xL + xR)/ 2;
fM = feval(f,xM);
fL = feval(f,xL);
fR = feval(f,xR);
if fM * fL < 0
xR = xM
elseif fM * fR < 0
xL = xM;
end
if abs(fM) < precision
disp(['Iterations performed: ', num2str(j)])
disp('Convergence was reached')
elseif(j==20)
disp('The maximum number of iterations were performed')
end
r = xM;
iters = j;
end
您不仅可以将内联函数作为输入传递,还可以将一组内联函数(单元格)作为输入传递。在您的主脚本中(或在命令 Window 中),您可以按如下方式声明这样的元胞数组:
f1 = inline('x^3 + 3*x + 1', 'x');
f2 = inline('1+sin(x)','x');
CandidateFunctions{1}=f1;
CandidateFunctions{2}=f2;
并将 CandidateFunctions
作为 bisectionF
的输入(而不是单个函数 f
)。
现在在bisectionF
里面,在行
之后
xM=(xL + xR)/ 2;
你可以设置一个if/else
开关:
if xM<=0
f=CandidateFunctions{1}
else
f=CandidateFunctions{2}
end
通过这种方式,您可以在每次迭代时 reassign/overwrite f
,具体取决于 xM
是正数、负数还是空值。
我有一个有效的二分法,但我需要在一个函数中实现它,该函数将接受三个参数:f 正在使用的函数,xL 是边界的左侧,xR 是边界的右侧。此外,它必须能够根据 x 的值更改函数(例如,如果函数是 f(x))。如果 x 小于或等于零,则 f(x) 将为 x^3 + 3x + 1,如果 x 大于零,则 f(x) 将为 1 + sin(x)。函数需要输出根和执行的迭代次数
如何传递一个函数,因为 f 是函数参数,然后如何找到 x 的值以确定使用哪个函数?
以下是我在控制台输入中遇到的错误:
>> f = inline('x^3 + 3x + 1', 'x')
f =
Inline function:
f(x) = x^3 + 3x + 1
>> bisectionF(f,-2,0.1)
Undefined function or variable 'bisectionF'.
>> bisectionF(f,-2,0.1)
Undefined function or variable 'B'.
Error in bisectionF (line 3)
maxIters = 20;B
>> bisectionF(f,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in bisectionF (line 7)
fM = feval(f,xM);
>> y = bisectionF('f1',-2,0.1)
Error using feval
Undefined function 'f1' for input arguments of type 'double'.
Error in bisectionF (line 9)
fM = feval(f,xM);
>> f1 = inline('x^3 + 3x + 1', 'x');
>> root = bisectionF(f1,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in bisectionF (line 9)
fM = feval(f,xM);
这是我的代码:
function[r, iters] = bisectionF(f,xL,xR)
%f1 = inline('x^3 + 3x + 1', 'x');
%f2 = inline('1+sin(x)','x');
maxIters = 20;
precision = 10^(-5);
for j = 1:maxIters
xM=(xL + xR)/ 2;
fM = feval(f,xM);
fL = feval(f,xL);
fR = feval(f,xR);
if fM * fL < 0
xR = xM
elseif fM * fR < 0
xL = xM;
end
if abs(fM) < precision
disp(['Iterations performed: ', num2str(j)])
disp('Convergence was reached')
elseif(j==20)
disp('The maximum number of iterations were performed')
end
r = xM;
iters = j;
end
您不仅可以将内联函数作为输入传递,还可以将一组内联函数(单元格)作为输入传递。在您的主脚本中(或在命令 Window 中),您可以按如下方式声明这样的元胞数组:
f1 = inline('x^3 + 3*x + 1', 'x');
f2 = inline('1+sin(x)','x');
CandidateFunctions{1}=f1;
CandidateFunctions{2}=f2;
并将 CandidateFunctions
作为 bisectionF
的输入(而不是单个函数 f
)。
现在在bisectionF
里面,在行
xM=(xL + xR)/ 2;
你可以设置一个if/else
开关:
if xM<=0
f=CandidateFunctions{1}
else
f=CandidateFunctions{2}
end
通过这种方式,您可以在每次迭代时 reassign/overwrite f
,具体取决于 xM
是正数、负数还是空值。