Format long error in function
7 次查看(过去 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!
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!