# (急)徵求MATLAB高手

PS 可以留信箱給我們嗎?? 我是新手 這樣可以向你請教喔 ^^ 3Q

place.123@yahoo.com.tw 忘了補充 抱歉

### 1 個解答

• 老師
Lv 7
1 0 年前
最佳解答

>> clear   %清除變數>> f=@(x) -12-21*x+18*x.^2-2.75*x.^3;  %匿名函數>> ezplot(f)    %簡單繪圖觀察圖形  與y=0相交於大約是x=[-2,6]這個區域  所以縮小畫圖區域  以便觀察>> ezplot(f,[-2,6])使用ginput指令  去圖上點選大約是y=0的3點後按 return>> ginput  ans =   -0.4240    0.1816    2.2120   -0.2348    4.7742   -1.0675得到 x=-0.4240, x=2.22120, x=4.7742(這點似乎誤差太大) 請自行嘗試二分法  >> x=bisect(f,[-1,0],0.01)x =   -0.4180二分法程式function r = bisect(fun,xb,xtol,ftol,verbose)% bisect  Use bisection to find a root of the scalar equation f(x) = 0%% Synopsis:  r = bisect(fun,xb)%            r = bisect(fun,xb,xtol)%            r = bisect(fun,xb,xtol,ftol)%            r = bisect(fun,xb,xtol,ftol,verbose)%% Input: fun     = (string) name of function for which roots are sought%        xb      = vector of bracket endpoints. xleft = xb(1), xright = xb(2)%        xtol    = (optional) relative x tolerance.    Default:  xtol=5*eps%        ftol    = (optional) relative f(x) tolerance. Default:  ftol=5*eps%        verbose = (optional) print switch. Default: verbose=0, no printing% Output:  r = root (or singularity) of the function in xb(1) <= x <= xb(2)if size(xb,1)>1, warning('Only first row of xb is used as bracket');  endif nargin < 3,  xtol = 5*eps;  endif nargin < 4,  ftol = 5*eps;  endif nargin < 5,  verbose = 0;   endxeps = max(xtol,5*eps);         %  Smallest tolerances are 5*epsfeps = max(ftol,5*eps);a = xb(1,1); b = xb(1,2);       %  Use first row if xb is a matrixxref = abs(b - a);              %  Use initial bracket in convergence testfa = feval(fun,a);   fb = feval(fun,b);fref = max([abs(fa) abs(fb)]);  %  Use max f in convergence testif sign(fa)==sign(fb)           %  Verify sign change in the interval  error(sprintf('Root not bracketed by [%f, %f]',a,b));end

2006-11-05 01:01:43 補充：

if verbose

fprintf('\nBisection iterations for %s.m\n',fun);

fprintf(' k xm fm\n');

end

2006-11-05 01:02:19 補充：

k = 0; maxit = 50; % Current and max number of iterations

while k < maxit

k = k + 1;

dx = b - a;

xm = a + 0.5*dx; % Minimize roundoff in computing the midpoint

2006-11-05 01:02:52 補充：

fm = feval(fun,xm);

if verbose, fprintf('%4d %12.4e %12.4e\n',k,xm,fm); end

if (abs(fm)/fref < feps) | (abs(dx)/xref < xeps) % True when root is found

r = xm; return;

end

2006-11-05 01:03:00 補充：

if sign(fm)==sign(fa)

a = xm; fa = fm; % Root lies in interval [xm,b], replace a and fa

else

b = xm; fb = fm; % Root lies in interval [a,xm], replace b and fb

end

end

warning(sprintf('root not within tolerance after %d iterations\n',k));

2006-11-05 01:05:42 補充：

本來想將二分法程式寄給你

可以你不接受來信

自己都不接受來信

還要求別人留下信箱號碼

這不是有些矛盾嗎

2006-11-05 11:42:54 補充：

試位法請參考

http://tw.knowledge.yahoo.com/question/?qid=110611...

參考資料： 其他的放不下了