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

3 次查看(过去 30 天)
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);
  2 个评论
Jesus Sanchez
Jesus Sanchez 2019-12-14
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
Hrishikesh Das 2019-12-14
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.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2019-12-14
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
  6 个评论
Hrishikesh Das
Hrishikesh Das 2019-12-14
I thank you all for extending your generous support. Thank you to help me learn and code better.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by