我在 Octave 中编写了一个二分方法,但它不能使用另一个函数。
我的二分方法代码如下:
function[x,b] = bisection(f,a,b)
t = 10e-8
while abs(b-a) > t;
c = (a+b)/2;
if f(a) * f(b) <= 0
a = a;
b = c;
else
b = b;
a = c
endif
endwhile
x = (a+b)/2
endfunction
Run Code Online (Sandbox Code Playgroud)
我已经有一个文件 f1.m:
function y = f1(x)
y = x^2 - 4;
endfunction
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时[x,v] = bisection[f1,0,5],我得到:
>> [t,v] = bisection(f1,0,5)
error: 'x' undefined near line 2 column 5
error: called from
f1 at line 2 column 3 …Run Code Online (Sandbox Code Playgroud) octave ×1