MATLAB only display numeric values up to 4 decimal places. (MATLAB r2020a)
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Natthaphat G.Mahtani
il 29 Ago 2021
Commentato: Star Strider
il 30 Ago 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 Commenti
Risposta accettata
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
Star Strider
il 30 Ago 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Number Theory in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!