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

11 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Star Strider
Star Strider il 29 Ago 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 Commenti
Natthaphat G.Mahtani
Natthaphat G.Mahtani il 30 Ago 2021
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 il 30 Ago 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by