Demo entry 6746679

二分法

   

Submitted by 李奇 on May 30, 2018 at 16:11
Language: Matlab. Code size: 754 Bytes.

k1=40;
k2=0.04;
m=0.095;
g=9.81;
h=0.43;
f=inline('-0.4*0.04*d.^2.5-0.5*40*d.^2+0.095*9.81*d+0.095*9.81*0.43','d');
xl=0;
xr=0.2;
es=0.5*10^(-5);
xt=0.5*(xl+xr);
flag=0;%³õʼ»¯µü´ú´ÎÊý
while 1
    flag=flag+1;
    xt1=xt;%¼Ì³ÐÉÏÒ»´ÎÖеã
    xt=0.5*(xl+xr);
    ya=feval(f,xl);
    yb=feval(f,xt);
    %Èç¹ûµÃ³öÖе㣬ֱ½Óµ¯³ö
    if(ya==0)
        xt=xl;
        break;
    elseif(yb==0)
        xt=xr;
        break;
    end
    if(ya*yb<0)
        xr=xt;
    elseif(ya*yb>0)
        xl=xt;
    end
    
    if(flag>1)%Ö»ÓÐÔÚµü´ú´ÎÊýΪ2ÒÔÉÏʱ£¬²Å¿ÉÒÔ¼ÆËãÏà¶ÔÎó²î
        ea=abs((xt-xt1)/xt);
    if(ea<es)
        break;
    end
    end
end
fprintf('·½³ÌÔÚ(0,0.2)¼äµÄ¸ùΪ%.5f\n',xt);
fprintf('µü´ú´ÎÊýΪ%d\n',flag);

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).