matlab 二分法a=0; b=5; c=(a+b)/2; while f(c)>=0.00001if f(a)*f(c)>=0 a=c; else b=c; end c=(a+b)/2; end c上面为远程序,求x^3-6*x-1的解
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/11 09:47:42
matlab 二分法a=0; b=5; c=(a+b)/2; while f(c)>=0.00001if f(a)*f(c)>=0 a=c; else b=c; end c=(a+b)/2; end c上面为远程序,求x^3-6*x-1的解
matlab 二分法
a=0;
b=5;
c=(a+b)/2;
while f(c)>=0.00001
if f(a)*f(c)>=0
a=c;
else
b=c;
end
c=(a+b)/2;
end
c
上面为远程序,求x^3-6*x-1的解
matlab 二分法a=0; b=5; c=(a+b)/2; while f(c)>=0.00001if f(a)*f(c)>=0 a=c; else b=c; end c=(a+b)/2; end c上面为远程序,求x^3-6*x-1的解
a=0;
b=5;
c=(a+b)/2;
while abs(f(c))>=0.00001 %加个绝对值
if f(a)*f(c)>=0
a=c;
else
b=c;
end
c=(a+b)/2;
end
c
源程序没有问题,就是计算的时候你需要建立一个M函数
点击file中的new中的M-file,输入
function y=f(x)
y=x^3-6*x-1;
保存后,你再在command window中输入上面的源程序,就可以得到结果,c=2.5
踩下啊
function [zero,res,niter]=...
bisection(fun,a,b,tol,niter_max,varargin)
x=[a, (a+b)*0.5, b];
fx=feval(fun,x,varargin{:});
if fx(1)*fx(3)>0
error(['The sign of the function ...
全部展开
function [zero,res,niter]=...
bisection(fun,a,b,tol,niter_max,varargin)
x=[a, (a+b)*0.5, b];
fx=feval(fun,x,varargin{:});
if fx(1)*fx(3)>0
error(['The sign of the function at the extrema of the interval',...
' must be different.'])
elseif fx(1)==0
zero=a;res=0;niter=0;
return
elseif fx(3)==0
zero=b;res=0;niter=0;
return
end
niter=0;
I=(b-a)*0.5;
while I>tol && niter
if sign(fx(1))*sign(fx(2))<0
x(3)=x(2);
x(2)=x(1)+(x(3)-x(1))*0.5;
fx=feval(fun,x,varargin{:});
I=(x(3)-x(1))*0.5;
elseif sign(fx(2))*sign(fx(3))<0
x(1)=x(2);
x(2)=x(1)+(x(3)-x(1))*0.5;
fx=feval(fun,x,varargin{:});
I=(x(3)-x(1))*0.5;
else
%x(2)=x(find(fx==0));
I=0;
end
end
if I>tol
fprintf(['\n Bin section stopped without converqing desired '...
'tolerance because the max number of iterations was reached.\n'])
end
zero=x(2);x=x(2);res=feval(fun,x);
收起