Format long error in function

2 visualizaciones (últimos 30 días)
ahmed saheed
ahmed saheed el 9 de Nov. de 2020
Comentada: ahmed saheed el 9 de Nov. de 2020
Hi guys,
I am attempting secant method in matlab, my function works perfectly well and outputs results to 4 decimal places. however i want to increase the number of decimal places and i am trying to use format long, however it does not work. Can you kindly take a look at my code
function approx = secant(f,x0,x1,nmax,tol)
format long
i = 2;
p0 = f(x0);
p1 = f(x1);
while i < nmax
x2 = x1 - (x1-x0)*p1/(p1-p0);
if abs(x2-x1)<tol
break
end
i = i + 1;
x0 = x1;
x1 = x2;
p0 = p1;
p1 = f(x2);
disp(x2)
end
approx = x2;
end
Thank you!

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 9 de Nov. de 2020
ahmed - consider using fprintf and specifiying the number of integers to the right of the decimal point that you are interested. For example, change
disp(x2)
to
fprintf('%.8f\n', x2);
where the .8 indicated that you want to show 8 integers to the right of the decimal point.
  1 comentario
ahmed saheed
ahmed saheed el 9 de Nov. de 2020
Hi Geoff
Thank you for the clarity.
It worked :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by