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

 Réponse acceptée

Mischa Kim
Mischa Kim le 17 Mai 2014
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 commentaires

Elia
Elia le 17 Mai 2014
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
Mischa Kim
Mischa Kim le 18 Mai 2014
Modifié(e) : Mischa Kim le 18 Mai 2014
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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by