Format long error in function

1 次查看(过去 30 天)
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!

采纳的回答

Geoff Hayes
Geoff Hayes 2020-11-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by