What am I doing wrong with this?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to create a bisection method to solve a function, and I thought I knew how to set it up. But now I just don't know!
I created a function file called dopdensity to evaluate a function. Then I created a separate bisection function file called bisect to evaluate that function and provide me with the roots.
Can someone correct my code?
Function File:
function f = dopdensity(N)
T_0 = 300;
T = 1000;
mu_0 = 1360;
q = 1.7e-19;
n_0 = 6.21e-19;
u = mu_0*(T/T_0)^-2.42;
f = 2/(q*u*(N+sqrt(N^2 + 1.54256e20)))-6.5e6;
I get an error telling me that there are not enough input arguments in the last line.
Bisection File:
function [root,fx,ea,iter]=bisect(func,xl,xu)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
test = dopdensity(xl,varargin{:})*dopdensity(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = dopdensity(xl,varargin{:})*dopdensity(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = dopdensity(xr, varargin{:});
I haven't received an error with this.....yet.
I really need to understand where my mistake is
0 Kommentare
Antworten (1)
dpb
am 24 Sep. 2013
I don't know about the first per se since you didn't actually show the error message it's not worthwhile to waste time trying to figure out what it might be...
But,
function [root,fx,ea,iter]=bisect(func,xl,xu)
% bisect: root location zeroes
...
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
test = dopdensity(xl,varargin{:})*dopdensity(xu,varargin{:});
...
Has a serious problem -- varargin is a cell array that must be specified in the argument list of the function that wishes to use the facility of variable number of input arguments. See
doc varargin
for precise rules and example of using it. After that is corrected, the rest of the symptoms will probably change drastically enough to make the original question immaterial.
4 Kommentare
dpb
am 25 Sep. 2013
I will try that. My apologies for not showing the error message, but here it is.
Undefined variable varargin.
Well, yes, that's exactly what I just got through telling you about -- your bisect function doesn't use varargin in the argument list as the last entry therein per
doc varargin
requirements. Ergo, it's undefined (unsurprsingly, it would seem)
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!