MATLAB only display numeric values up to 4 decimal places. (MATLAB r2020a)

조회 수: 11 (최근 30일)
I created two functions called 'newton' and 'fjacob' to solve nonlinear equations using Newton's method.
When i type [xsolution,Xk,Fk,Jk,IFLAG,IterationUsed] = newton(@fjacob,[8;5],1e-8,15) in command window,
The results show only 4 decimal places (shown in attached picture), even when I set to format 'long'.
%--newton--%
function [xsolution,Xk,Fk,Jk,IFLAG,IterationUsed] = newton(FunctionName,x0,epsilon,IterationMax)
x = sym('x',[1 2]).';
IFLAG = "Fail to converge";
[f,J] = FunctionName(x);
Xk = x0.';
Jk = [];
IterationUsed = 0;
xsol = x0;
iter = [0];
for i=1:IterationMax
fsub = subs(f,x,xsol);
Jsub = subs(J,x,xsol);
Fk(i,:) = fsub.';
Jk = [Jk;Jsub];
s = -inv(Jsub)*fsub;
x_new = xsol + s;
Xk = [Xk;x_new'];
IterationUsed = IterationUsed + 1;
iter(i+1) = i;
if norm(s,inf) < epsilon
xsolution = x_new;
IFLAG = "Converges to xsolution";
break
end
if i == IterationMax; break , end
xsol = vpa(x_new);
end
end
%--fjacob--%
function [f,J] = fjacob(x)
f = [x(1)^2 + x(2)^2 - 1 ; 5*x(1)^2 - x(2) - 2];
J = jacobian(f,x);
end

채택된 답변

Star Strider
Star Strider 2021년 8월 29일
I cannot run thst.
It appears that you are using the Symbolic Math Toolbox and the vpa funciton. Use the digits function to see what the digits (displayed precision) setting is, and change it if necessary.
.
  댓글 수: 13
Natthaphat G.Mahtani
Natthaphat G.Mahtani 2021년 8월 30일
I think i have fixed it.
I type sympref('default') and call out the function again, the results are same as yours.
Thanks very much for your help.
Star Strider
Star Strider 2021년 8월 30일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by