Format long error in function

2 ビュー (過去 30 日間)
ahmed saheed
ahmed saheed 2020 年 11 月 9 日
コメント済み: ahmed saheed 2020 年 11 月 9 日
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.
  1 件のコメント
ahmed saheed
ahmed saheed 2020 年 11 月 9 日
Hi Geoff
Thank you for the clarity.
It worked :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by