clear;clc;
syms x y
y=1/(2.*x^7+x^3+2)+100;
y2=x+3;
[x0,dy]=solveMono(y,[-100,0])
function [x0,y0]=solveMono(fHd,limVec)
nGrid=1000;%Search Grid;
p=0.5;%march Infactor
err=1e-12;% zero precision
dErr=1e-20;%dy precision
x0=mean(limVec);
syms X_
y(X_)=subs(fHd,X_);
yp(X_)=diff(y,X_);
y0=y(x0);yp0=yp(x0);
% y0 + yp0 + stepSign -
% y0 - yp0 - stepSign -
% y0 + yp0 - stepSign +
% y0 - yp0 + stepSign +
count=0;
stepSign=logical(y0.*yp0<0)-logical(y0.*yp0>0);
lim=limVec((stepSign-(-1))/2+1);
dy=1+err;
figure;
while abs(y0)>err && abs(dy)>err
xVec1=linspace(x0,lim,nGrid);
xVec=xVec1(1:end-1);
yVec=y(xVec);
productY=yVec(1:end-1).*yVec(2:end);
negIndex=find(productY<0);
if isempty(negIndex)
x0Last=xVec(floor(nGrid.*p));y0Last=yVec(floor(nGrid.*p));
x0=xVec(floor(nGrid.*p)+1);y0=yVec(floor(nGrid.*p)+1);
else
x0Last=xVec(negIndex(1));y0Last=yVec(negIndex(1));
x0=xVec(negIndex(1)+1);y0=yVec(negIndex(1)+1);
end
count=count+1;
dy=y0-y0Last;
fprintf('\nsolveMono():%d,%.12f,%.12f',count,x0,y0);
% vpa([count,x0,y0])
if count==1
subplot(2,2,1);hold on;plot(x0Last,count,'o');xlabel('x_0');ylabel('count');drawnow;title('Search Contrary Sign');grid on;
subplot(2,2,3);hold on;plot(count,y0Last,'o');xlabel('count');ylabel('y_0');drawnow;grid on;
end
subplot(2,2,1);hold on;plot(x0,count,'o');xlabel('x_0');ylabel('count');drawnow;title('Search Contrary Sign');grid on;
subplot(2,2,3);hold on;plot(count,y0,'o');xlabel('count');ylabel('y_0');drawnow;grid on;
if y0*y0Last<0
if y0
xLeft=x0;xRight=x0Last;
yLeft=y0;%yRight=y0Last;
elseif y0>=y0Last
xLeft=x0Last;xRight=x0;
yLeft=y0Last;%yRight=y0;
end
while abs(yLeft)>err && abs(dy)>dErr
xVec=linspace(xLeft,xRight,nGrid);
yVec=y(xVec);
productY=yVec(1:end-1).*yVec(2:end);
negIndex=find(productY<0);
yLeftLast=yLeft;
xLeft=xVec(negIndex(1));yLeft=yVec(negIndex(1));
xRight=xVec(negIndex(1)+1);%yRight=yVec(negIndex(1)+1);
dy=yLeft-yLeftLast;
count=count+1;
fprintf('\nsolveMono():%d,%.12f,%.12f',count,xLeft,yLeft);
% vpa([count,xLeft,yLeft])
subplot(2,2,2);hold on;plot(xLeft,count,'*');xlabel('x_0');ylabel('count');drawnow;title('Search Zero Point');grid on;
subplot(2,2,4);hold on;plot(count,yLeft,'*');xlabel('count');ylabel('y_0');drawnow;grid on;
end
x0=xLeft;y0=double(yLeft);
end
end
close(gcf)
end