필터 지우기
필터 지우기

Format long error in function

조회 수: 1 (최근 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 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