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
0 件のコメント
採用された回答
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 件のコメント
Star Strider
2021 年 8 月 30 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Number Theory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!