|
回复:(qiuqia17)有关对 inline的 问题
<P>看一下这样对不对<BR>a=1;<BR>fx=inline('x+aa');<BR>[x,err,xx] = bisct(fx,-2,2,1e-4,50,a);</P>
<P><BR>function [x,err,xx] = bisct(f,a,b,TolX,MaxIter,aa)<BR>%bisct.m to solve f(x) = 0 by using the bisection method.<BR>%input : f = ftn to be given as a string ’f’ if defined in an M-file<BR>% a/b = initial left/right point of the solution interval<BR>% TolX = upperbound of error |x(k) - xo|<BR>% MaxIter = maximum # of iterations<BR>%output: x = point which the algorithm has reached<BR>% err = (b - a)/2(half the last interval width)<BR>% xx = history of x<BR>TolFun=eps; fa = feval(f,aa,a); fb = feval(f,aa,b);<BR>if fa*fb > 0, error('We must have f(a)f(b)<0!'); end<BR>for k = 1: MaxIter<BR>xx(k) = (a + b)/2;<BR>fx = feval(f,aa,xx(k)); err = (b-a)/2;<BR>if abs(fx) < TolFun | abs(err)<TolX, break;<BR>elseif fx*fa > 0, a = xx(k); fa = fx;<BR>else b = xx(k);<BR>end<BR>end<BR>x = xx(k);<BR>if k == MaxIter, fprintf('The best in %d iterations\n',MaxIter), end</P> |
评分
-
1
查看全部评分
-
|