我想绘制二分法数值解的图形并显示它如何接近真实解。我的代码:
% f=input('please enter the function:');
% xL=input('please enter the limits .from:');
% XR=input('to:');
% eps=input('please enter epsilon:');
f=@(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
subplot(2,1,1);
title('graph 1');
ezplot(f);
hold on ;
% line([0 0],40);
% line(-40,[0 0]);
plot(XR,f(XR),'r*');
plot(xL,f(xL),'r*');
disp( 'the answers is : ');
for df=xL:0.15:XR
if f(xL)*f(df)<= 0
xR=df;
Roots = BisectionM(f,xR,xL,eps);
plot(Roots,f(Roots),'gs');
disp(Roots);
xL=df;
xR=XR;
end
end
subplot(2,1,2);
title('graph 2');
x0=fzero(f,xL);
sol = BisectionM(f,xR,xL,eps);
plot(1:1:100,ones(1,100)*x0);
hold on ;
plot(1:1:100,sol);
功能 :
function[sol,Roots] = BisectionM(f,xR,xL,eps)
while abs(xR - xL) > eps
xM = (xR + xL) / 2;
if (f(xL))*(f(xM)) > 0
xL = xM;
sol=xM;
plot(xL,f(xL),'.');
else
xR = xM;
plot(xR,f(xR),'.');
sol=xM;
end
Roots = xM;
end
end
我不知道如何绘制它,所以它会更接近解决方案(最后的蓝线)。任何人?