How to display the results of each iterative step until convergence is reached?
1 次查看(过去 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
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
回答(1 个)
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 个评论
另请参阅
类别
在 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!