Trying to end my loop after err is less than 1e-10.
4 views (last 30 days)
Show older comments
I am trying to end my loop after the err is less than 1e-10.
Here is my code:
clear;
clc;
syms x y;
%Declare Function
f(x,y)=x+2*y-2;
g(x,y)=x^2+4*y^2-4;
%Jacobian Matrixes
f_x(x,y)=diff(f,x);
f_y(x,y)=diff(f,y);
g_x(x,y)=diff(g,x);
g_y(x,y)=diff(g,y);
%Jacobian Matrix
jac1=[f_x f_y; g_x g_y];
%Initial Guess and Max Iterations
x1=1;y1=2;
max=25;
tol=1e-10;
fprintf("=====================================================================================\n");
fprintf("The numbers displayed below are the intermediate points starting from x^1 until x^25.\n");
fprintf("The number displayed below the intermediate points is the error for that point.\n");
fprintf("=====================================================================================\n");
for i=1:max
jac = jac1(x1,y1);
ijac = inv(jac);
xx = double([x1;y1]-(ijac*[f(x1,y1);g(x1,y1)]));
errorpoint=(-ijac)*([f(x1,y1);g(x1,y1)]);
err=norm(xx-[x1,y1]);
x1=double(xx(1));
y1=double(xx(2));
fprintf("\n");
fprintf("Intermediate Point:%.10f\n", xx);
fprintf("\n");
fprintf("Error Point:%.10f\n", errorpoint)
fprintf("\n");
fprintf("=====================================================================================\n")
result(i,1:2)=[x1,y1];
if err<tol
break
end
end
Here is my output:
=====================================================================================
The numbers displayed below are the intermediate points starting from x^1 until x^25.
The number displayed below the intermediate points is the error for that point.
=====================================================================================
Intermediate Point:-0.8333333333
Intermediate Point:1.4166666667
Error Point:-1.8333333333
Error Point:-0.5833333333
=====================================================================================
Intermediate Point:-0.1893939394
Intermediate Point:1.0946969697
Error Point:0.6439393939
Error Point:-0.3219696970
=====================================================================================
Intermediate Point:-0.0150791353
Intermediate Point:1.0075395677
Error Point:0.1743148041
Error Point:-0.0871574020
=====================================================================================
Intermediate Point:-0.0001120013
Intermediate Point:1.0000560006
Error Point:0.0149671340
Error Point:-0.0074835670
=====================================================================================
Intermediate Point:-0.0000000063
Intermediate Point:1.0000000031
Error Point:0.0001119950
Error Point:-0.0000559975
====================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000063
Error Point:-0.0000000031
=====================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
===================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:-0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
===================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
=====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
====================================================================================
Intermediate Point:0.0000000000
Intermediate Point:1.0000000000
Error Point:0.0000000000
Error Point:0.0000000000
====================================================================================
After manually doing the math the code should stop after the 6th iteration. But i cannot figure out how to stop my loop when the err is less than 1e-10.
0 Comments
Answers (1)
Image Analyst
on 11 Dec 2021
I guess it's never less than that. Print out more decimal places:
fprintf("Error Point:%.15f\n", errorpoint)
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!