# How to display the results of each iterative step until convergence is reached?

10 views (last 30 days)
Hrishikesh Das on 14 Dec 2019
Commented: Hrishikesh Das on 14 Dec 2019
Hi
I am very, very new to matlab. Below is my code. The solution is getting converged in 4th iteration. I wish to display the four values of u1, lam1. Please help.
Thank you.
clc
clear all
close all
%%Stability Problem
%Input
syms u lam phi
H(u,lam)= u^3-8*u^2+18*u-2*lam;
K(u,lam)= diff(H,u);
DK(u) = diff(K,u);
l(phi)= phi-1;
r0 = 2;
tolX = 10^-4;
%Initial Step
u0= 1.17573;
lam0=5.86484;
phi0=1;
%Iterative step (K=0)
for k= 1:10;
H0 = vpa(H(u0,lam0));
K0 = vpa(K(u0,lam0));
del_u_r = vpa(-K0^-1*H0);
del_u_lam = vpa(K0^-1*r0);
h_phi = K0*phi0+DK(u0)*phi0*del_u_r;
del_phi_phi = (-K0^-1)*h_phi;
h_lam =DK(u0)*phi0*del_u_lam;
del_phi_lam = (-K0^-1)*h_lam;
l_com_phi=(phi0)/norm(phi0);
del_lam =( -l_com_phi*del_phi_phi+l(phi0))/(l_com_phi*del_phi_lam);
del_phi = del_phi_phi+del_lam*del_phi_lam;
del_u = del_u_r+del_u_lam*del_lam;
%update
u1 = u0+del_u;
lam1=lam0+del_lam;
phi1=phi0+del_phi;
u0=u1;
lam0=lam1;
phi0=phi1;
if (norm(del_u)/norm(u1))< tolX
break
end
end
msg = ['The solution converged in ', num2str(k),'th iterations.'];
disp(msg)
fprintf('U = %.5f\n',u1);
fprintf('lambda = %.5f\n',lam1);
fprintf('phi = %.0f\n',phi1);
Jesus Sanchez on 14 Dec 2019
Format your code properly please, its very difficult to read. Also, what do you want exactly? You are already showing the values at the end of the loop with fprint
Hrishikesh Das on 14 Dec 2019
Thank you for your comment. As you can see the solution converges in 4th iteration, I wish to display the results u1 and lam1 in every iterative step at the end. Hope I am able make you understand my question. Your help is highly appreciated.

Image Analyst on 14 Dec 2019
Before the last end of your for loop, insert this code:
valueToCheck = norm(del_u)/norm(u1);
fprintf('In iteration %d, norm(del_u)/norm(u1) = %f.\n', k, valueToCheck);
if valueToCheck < tolX
fprintf(' This is more than tolX, so we will continue with the loop now.\n', tolX);
else
fprintf(' This is less than tolX, so we will exit the loop now.\n', tolX);
break
end
Jesus Sanchez on 14 Dec 2019
You are welcome! :)
Hrishikesh Das on 14 Dec 2019
I thank you all for extending your generous support. Thank you to help me learn and code better.