Help using fprintf function
Show older comments
I am writing a .m file to later call in my main script that will use the bisection method to solve a problem. However, on my fprintf line I am getting the error, "The format might not agree with the argument count." I am new to matlab and am unsure what I am doing wrong. Any help is greatly appreciated.
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,disp('at least 3 input arguments required'),end
fl = f(xl); fu = f(xu); test = fl*fu;
if test > 0,disp('@@ Bad initial guess, no sign change'),end
if nargin < 4, es = 0.0001;end
if nargin < 5, imax = 50;end
xr = xl;
disp('Iter xl xu xr ea es')
for iter=1:1:imax
xrold = xr;
xr = (xl + xu)/2; fr = f(xr);
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
fprintf('%4d\t%6.4f\t%6.4f\t%6.4f\t%8.6f\t%8.6f\n’,iter,xl,xu,xr,ea,es')
test = fl*fr;
if test < 0
xu = xr;
elseif test > 0
xl = xr; fl = fr;
else
ea = 0;
end
if ea <= es,break,end
end
end
2 Comments
Walter Roberson
on 11 Oct 2019
Why do you use es' for fprintf when you expect es to be a scalar ?
Note that you continue executing in the situation tat nargin < 3
if nargin < 3,disp('at least 3 input arguments required'),end
You should probably error() instead of disp()
Irrespective of the Q?,
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,disp('at least 3 input arguments required'),end
is admirable to do error checking, but your function as is just goes on its merry way after the test, anyways...
function [xr,fr,ea,iter] = bisection(f,xl,xu,es,imax)
if nargin < 3,error('At least 3 input arguments required'),end
will display the message and then abort the function. You can get more involved as you wish, but that's the simplest possible...
But, as you've written it in the function definition line, the function will abort unless all five arguments are supplied...see the doc for varargin to see how to handle a variable number of arguments.
Walter commented while I was still editing before I got to the fprintf() conundrum; not possible to tell unequivocally w/o seeing the input argument definitions and the calling sequence, but the use of the transpose is peculiar and may be what's triggering the error.
Accepted Answer
More Answers (1)
KURRA HARIKRISHNA
on 14 Jun 2025
Edited: Walter Roberson
on 14 Jun 2025
fprintf('%4d'. i*j);
1 Comment
Walter Roberson
on 14 Jun 2025
Period by itself is not a valid binary operator that can go inbetween '%4d' and i*j . You probably wanted comma there,
fprintf('%4d', i*j)
Note that the effect of that code fragment depends on whether it is in LiveScript or not, and whether it is part of a longer line in LiveScript.
If the fragment is executed in LiveScript, then all of the fprintf() on the same (potentially continued) input line output together, and after the last of them there is an implicit newline emitted (unless the last fprintf already ended its output with newline.)
If the fragment is executed outside of LiveScript, then the output will not have an implicit newline added.
Note that there is also no implied space added between outputs. So for example
i = 3; for j = [400,500]; fprintf('%4d', i*j); end
will output
12001500
with no space between them.
Because of this, the fprintf() statement would typically be either
fprintf('%4d\n', i*j)
or else
fprintf(' %4d', i*j)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!