function[x,y] = MyILIP [fun, a, b, tol, max]
a = a0
b = b0
i = 1
y = feval(fun,x)
x = zeros(max, 1);
y = zeros(max, 1);
a = zeros(max, 1);
b = zeros(max, 1);
while(i<max)
x = b - feval(fun,b)*(b-a)/(feval(fun,b)-feval(fun,a))
if(abs(x-a)<tol or feval(fun,x)=0)
fprintf('x is the zero point')
end
if(feval(fun,a)*feval(fun,x)<0)
b = x
else
a = x
i = i + 1
end
fprintf(' i a b x y\n');
这是我的函数程序
clear;
close all;
clc
a = 0;
b = 4.5;
tol = 1e-3;
max = 20;
fun = @(x)cos(5*x);
[x, y] = MyILIP(fun, a, b, tol, max);
这是我的运行程序
为什么一直报错
clear all;
clc
a = 0;
b = 4.5;
tol = 1e-3;
max = 20;
fun = @(x)cos(5*x);
[x, y] = untitled3(fun,a,b,tol,max);
函数
function[x,y] = untitled3(fun,a,b,tol,max)
i = 1;
while(i<max)
x = b-(feval(fun,b)*(b-a))/(feval(fun,b)-feval(fun,a));
a0=abs(x-a);
x = feval(fun,x);
if a0<tol or (x==0)
fprintf('x is the zero point')
end
if(feval(fun,a)*feval(fun,x)<0)
b = x;
else
a = x;
i = i + 1;
end
y = feval(fun,x);
formatSpec = '%4.4f %4.4f %4.4f %4.4f %4.4f \n';
fprintf(formatSpec,i,a,b,x,y)
end
结果
1.0000 0.0000 0.8496 0.8496 -0.4477
2.0000 -0.9786 0.8496 -0.9786 0.1796
2.0000 -0.9786 -0.6479 -0.6479 -0.9952
3.0000 -0.0721 -0.6479 -0.0721 0.9356
4.0000 -0.1839 -0.6479 -0.1839 0.6064
5.0000 -0.2250 -0.6479 -0.2250 0.4311
6.0000 -0.1922 -0.6479 -0.1922 0.5728
7.0000 -0.2207 -0.6479 -0.2207 0.4507
8.0000 -0.1970 -0.6479 -0.1970 0.5527
9.0000 -0.2176 -0.6479 -0.2176 0.4643
10.0000 -0.2003 -0.6479 -0.2003 0.5392
11.0000 -0.2153 -0.6479 -0.2153 0.4743
12.0000 -0.2025 -0.6479 -0.2025 0.5297
13.0000 -0.2137 -0.6479 -0.2137 0.4816
14.0000 -0.2041 -0.6479 -0.2041 0.5228
15.0000 -0.2124 -0.6479 -0.2124 0.4870
16.0000 -0.2053 -0.6479 -0.2053 0.5178
17.0000 -0.2115 -0.6479 -0.2115 0.4911
18.0000 -0.2062 -0.6479 -0.2062 0.5141
19.0000 -0.2108 -0.6479 -0.2108 0.4942
20.0000 -0.2068 -0.6479 -0.2068 0.5114