how can i print 2 function?s results ?

the answer is only X and not the niter, why ?
function [X,niter] = bisection2(intervallo,tol)
niter = 1;
if intervallo(1) < intervallo(2)
a = intervallo(1);
b = intervallo(2);
elseif intervallo(1) == intervallo(2)
X = intervallo(2);
else a = intervallo(2);
b = intervallo(1);
end
X0 = (a + b)/2;
if f(a) * f(b) < 0
if abs(f(X0)) < tol
end
while abs (f(X0)) > tol
if f(X0) * f(a) < 0
b = X0;
else
a =X0;
end
X0 = (a + b)/2 ;
niter = niter + 1;
end
X = X0 ;
elseif f(a) * f(b) == 0
if f(a) == 0
X = a;
else
X = b;
end
end

 Accepted Answer

Elia, you need to call the function, e.g.,
[X,niter] = bisection2([1,3],1e-6)
Also, I assume the function f is properly defined. Just in case it is not, try adding
f = @(x) x^2 - 2;
right after the function declaration.

2 Comments

thanks Mischa but i can't understand some things. The function f is defined in another step and it is function y = f(x)
y = exp(-x)-sin(x);
end and it's ok,but the bisection2 arguments (intervallo,tol) are not constants but input arguments in the main menu. for example, intervallo=[a,b], tol=0.001. in main menu i write
a = input XX;
b = input XX;
tol = input XX;
bisection2([a,b],tol)
and what e.g. means ? thanks
OK, then you should be fine, simply use (in the main menu)
a = input('Input a: ');
b = input('Input b: ');
tol = input('Input tol: ');
[X,niter] = bisection2([a,b],tol)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Asked:

on 17 May 2014

Edited:

on 18 May 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!