New to MATLAB - trying to write bisection method?
Mostrar comentarios más antiguos
Hello, I'm brand new to MATLAB and am trying to understand functions and scripts, and write the bisection method based on an algorithm from our textbook. However, I'm running into problems. Could anyone help me please?
Here is my code:
function [f] = Bisection(a,b,Nmax,TOL)
f = x^3 - x^2 + x;
i=1;
BisectA=f(a);
while i <= Nmax
p=a+(b-a)/2;
BisectP=f(p);
if BisectP == 0 || (b-a)/2 < TOL
disp('p');
end
i=i+1;
if BisectA*BisectP > 0
a=p;
BisectA=BisectP;
else
b=p;
end
end
disp('Method failed after num2str(Nmax) iterations, Nmax=', Nmax);
Thanks.
3 comentarios
Walter Roberson
el 15 de Dic. de 2013
What problems are you running into? Are you getting an error message? If so then at which line, and under what circumstances?
youcef mokrane
el 9 de Nov. de 2020
Editada: Walter Roberson
el 9 de Nov. de 2020
x=4:4.7
f=tan(x)-x
a=4
b=4.7
fa=tan(a)-a
fb=tan(b)-b
n=1
n0=5000
while n<5000
p=(a+b)/2
fp=tan(p)-p
n=n+1
if fa*fp>0
a=p
else
b=p
end
end
Walter Roberson
el 9 de Nov. de 2020
Why are you bothering to do x=4:4.7 ? The default increment for the colon operator is 1, so 4:4.7 is the same as 4:1:4.7 which is just going to be 4 .
Why are you assigning to n0 when you do not use it?
Respuesta aceptada
Más respuestas (1)
Frank Cano
el 15 de Abr. de 2019
2 votos
Here are some Bisection method examples
Categorías
Más información sobre Live Scripts and Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!