Suppress excess output of code
3 views (last 30 days)
Show older comments
When i run newtraph the function outputs all the xrold values of each iteration, the roots are correct, is there a way to surpress this? I am not sure if the problem is in the code for the function, or the script
script:
x = linspace(-1,1);
z = incsearch(@(x) sin(x)-x.^2, -1, 1, 100)
y = @(x)sin(x)-x^2;
dy = @(x) cos(x) - 2*x;
root1 = newtraph(y,dy,-.01)
root2 = newtraph(y,dy,.8600)
function:
function [root, ea, iter]= newtraph(func, dfunc, xr, es, maxit)
if nargin<3, error('at least 3 input arguments required'), end
if nargin<4|isempty(es), es=.0001; end
if nargin<5|isempty(maxit), maxit=50; end
iter=0;
while(1)
xrold=xr;
xr=xr-func(xr)/dfunc(xr);
iter=iter+1;
if xr~=0; ea=abs((xr-xrold)/xr)*100; end
if ea<=es |iter >= maxit,break,end
end
root=xr;
3 Comments
Walter Roberson
on 11 Feb 2016
Please use
which -all newtraph
to check whether possibly you are getting a different version of the code.
Answers (1)
Image Analyst
on 11 Feb 2016
You must have
xr=xr-func(xr)/dfunc(xr)
instead of
xr=xr-func(xr)/dfunc(xr);
If you don't have the semicolon it will output the value of xr to the command window. If you do have the semicolon, it won't.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!