Table of values from output

Hi, I would like help to add a table of output values of x and y values for this code
x0 = [3; -1.5];
maxIter = 50;
tolX = 1e-14;
x = x0;
xold = x0;
for i = 1:maxIter
[f,j] = newtraph(x);
x = x- j\f;
err(:,i) = abs(x-xold);
xold = x;
if (err(:,i)<tolX)
break;
end
end
display(x)
fprintf('Number of iterations: %d \n',i)
function [fval,jac] = newtraph(X)
x = X(1);
y = X(2);
fval(1,1)=x+exp(-x)+y^3;
fval(2,1)=x^2+2*x*y-y^2+tan(x);
jac = [1-exp(-x), 3*y^2;
2*x+2*y+1+(tan(x))^2, 2*x-2*y];
end

 Accepted Answer

Perhaps something like this:
xv = nan(MaxIter,1);
yv = xv;
for i = 1:maxIter
[f,j] = newtraph(x);
x = x- j\f;
err(:,i) = abs(x-xold);
xold = x;
if (err(:,i)<tolX)
break;
end
xv(i) = x(1);
yv(i) = x(2);
end
OutputValues = table(xv(~isnan(xv)), yv(~isnan(yv)), 'VariableNames',{'X','Y'})
.

4 Comments

Thank you. However, I am getting repeated values of x and y. Is that an issue with my original code or because of the one that you sent?
They are actually not repeats.
Using format long before the loop, the result is:
OutputValues =
5×2 table
X Y
________________ _________________
3.71146633181626 -1.55197504848601
3.59769376871789 -1.53630746228676
3.59241988861306 -1.5354453832451
3.59240993069037 -1.53544358383206
3.59240993065508 -1.53544358382529
They only appear to be repeats with format short. They are actually unique entries.
Also, in the loop, change ‘xv’ and ‘yv’ so they automatically become column vectors:
xv(i,:) = x(1);
yv(i,:) = x(2);
This works better with the table call.
It works and I cannot thank you enough for this!!!
As always, my pleasure!
I cannot thank you enough for this!!!
By Accepting my Answer, you already did! Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!