MATLAB only display numeric values up to 4 decimal places. (MATLAB r2020a)
11 views (last 30 days)
Show older comments
Natthaphat G.Mahtani
on 29 Aug 2021
Commented: Star Strider
on 30 Aug 2021
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
0 Comments
Accepted Answer
Star Strider
on 29 Aug 2021
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 Comments
Star Strider
on 30 Aug 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
More Answers (0)
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!