how to make a table of iterations

27 次查看(过去 30 天)
i need help to make table of iterations to this code
tic
clear; clc; close all;
syms f(x,y) ph
% f(x,y)
f(x,y)=3*x^2+y^2;
% Initial Guess:
x0=[4;4];
% Gradient Computation:
g(x,y)=[diff(f(x,y),x) ; diff(f(x,y),y)];
fprintf('========================================\n');
g0=g(x0(1),x0(2));
XX=[x0(1)];
YY=[x0(2)];
ff=[f(x0(1),x0(2))];
fprintf('Loop 1\n');
x2(ph)=x0(1)-ph*g0(1);
y2(ph)=x0(2)-ph*g0(2);
G=-g(x2(ph),y2(ph)).*g0;
G=G(1)+G(2);
phsol=solve(G,ph);
X=x0(1)-phsol*g0(1);% New x value
Y=x0(2)-phsol*g0(2);% New y value
m=(Y-x0(2))/(X-x0(1));
%error=10^-20
err=10^-20;
ii=2;
fprintf('alpha=%.3f\n',phsol);
fprintf('x=%.3f y=%.3f\n',X,Y);
fprintf('f(Xnew,Ynew)=%.3f\n',f(X,Y));
fprintf('m=%.3f\n',m);
%relative norm
relnorm=sqrt((X-x0(1))^2/x0(1)^2 + (Y-x0(2))^2/x0(2)^2 );
fprintf('relative norm=%.3f\n',relnorm);
XX=[XX X];
YY=[YY Y];
ff=[ff f(X,Y)];
fprintf('========================================\n');
while(abs(f(X,Y) - f(x0(1),x0(2))) > err)
x0=[X,Y];
g0=g(x0(1),x0(2));
fprintf('Loop%d\n',ii);
x2(ph)=x0(1)-ph*g0(1);
y2(ph)=x0(2)-ph*g0(2);
G=-g(x2(ph),y2(ph)).*g0;
G=G(1)+G(2);
phsol=solve(G,ph);
X=x0(1)-phsol*g0(1);
Y=x0(2)-phsol*g0(2);
ii=ii+1;
% Output results
fprintf('alpha=%.3f\n',phsol);
fprintf('x=%.3f y=%.3f\n',X,Y);
fprintf('f(Xnew,Ynew)=%.3f\n',f(X,Y));
% m
m=(Y-x0(2))/(X-x0(1));
fprintf('m=%.3f\n',m);
% relative norm
relnorm=sqrt((X-x0(1))^2/x0(1)^2 + (Y-x0(2))^2/x0(2)^2 );
fprintf('relative norm=%.3f\n',relnorm);
XX=[XX X];
YY=[YY Y];
ff=[ff f(X,Y)];
fprintf('========================================\n');
end
% Plot Contour
xx=linspace(-5,5);
yy=linspace(-5,5);
[xx,yy] = meshgrid(xx,yy);
ff=3*xx.^2+yy.^2;
[x,y,z] = peaks(100);
v=[0.5,1,1.5,2,2.5];
[C,h] = contour(xx,yy,ff,[-90,-80 -70 -60 -50 -40 30 -20 -10 -5 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 5 10 20 30 40 50 60 70 80 90],'lineWidth',1.5);
clabel(C,h)
hold on
plot(XX,YY,'*-r');
xlabel('X')
ylabel('Y')
hold off
xlabel ('x')
ylabel ('y')
grid
fprintf('initial guess = (4,4)\n');
fprintf('error = 10^-20\n');
fprintf('Number of iteration = %d\n',ii-1);
toc

回答(1 个)

KSSV
KSSV 2023-4-27
The vairable T at the end is the required table. If you want it as an array check the variable iwant.
tic
clear; clc; close all;
syms f(x,y) ph
count = 1 ;
iwant = zeros([],7) ;
% f(x,y)
f(x,y)=3*x^2+y^2;
% Initial Guess:
x0=[4;4];
% Gradient Computation:
g(x,y)=[diff(f(x,y),x) ; diff(f(x,y),y)];
fprintf('========================================\n');
g0=g(x0(1),x0(2));
XX=[x0(1)];
YY=[x0(2)];
ff=[f(x0(1),x0(2))];
fprintf('Loop 1\n');
x2(ph)=x0(1)-ph*g0(1);
y2(ph)=x0(2)-ph*g0(2);
G=-g(x2(ph),y2(ph)).*g0;
G=G(1)+G(2);
phsol=solve(G,ph);
X=x0(1)-phsol*g0(1);% New x value
Y=x0(2)-phsol*g0(2);% New y value
m=(Y-x0(2))/(X-x0(1));
%error=10^-20
err=10^-20;
ii=2;
fprintf('alpha=%.3f\n',phsol);
fprintf('x=%.3f y=%.3f\n',X,Y);
fprintf('f(Xnew,Ynew)=%.3f\n',f(X,Y));
fprintf('m=%.3f\n',m);
%relative norm
relnorm=sqrt((X-x0(1))^2/x0(1)^2 + (Y-x0(2))^2/x0(2)^2 );
fprintf('relative norm=%.3f\n',relnorm);
fprintf('========================================\n');
iwant(count,:) = [count phsol X Y f(X,Y) m relnorm] ;
while(abs(f(X,Y) - f(x0(1),x0(2))) > err)
count = count+1 ;
x0=[X,Y];
g0=g(x0(1),x0(2));
fprintf('Loop%d\n',ii);
x2(ph)=x0(1)-ph*g0(1);
y2(ph)=x0(2)-ph*g0(2);
G=-g(x2(ph),y2(ph)).*g0;
G=G(1)+G(2);
phsol=solve(G,ph);
X=x0(1)-phsol*g0(1);
Y=x0(2)-phsol*g0(2);
ii=ii+1;
% Output results
fprintf('alpha=%.3f\n',phsol);
fprintf('x=%.3f y=%.3f\n',X,Y);
fprintf('f(Xnew,Ynew)=%.3f\n',f(X,Y));
% m
m=(Y-x0(2))/(X-x0(1));
fprintf('m=%.3f\n',m);
% relative norm
relnorm=sqrt((X-x0(1))^2/x0(1)^2 + (Y-x0(2))^2/x0(2)^2 );
fprintf('relative norm=%.3f\n',relnorm);
% XX=[XX X];
% YY=[YY Y];
% ff=[ff f(X,Y)];
fprintf('========================================\n');
iwant(count,:) = [count phsol X Y f(X,Y) m relnorm] ;
end
% Plot Contour
xx=linspace(-5,5);
yy=linspace(-5,5);
[xx,yy] = meshgrid(xx,yy);
ff=3*xx.^2+yy.^2;
[x,y,z] = peaks(100);
v=[0.5,1,1.5,2,2.5];
[C,h] = contour(xx,yy,ff,[-90,-80 -70 -60 -50 -40 30 -20 -10 -5 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 5 10 20 30 40 50 60 70 80 90],'lineWidth',1.5);
clabel(C,h)
hold on
plot(XX,YY,'*-r');
xlabel('X')
ylabel('Y')
hold off
xlabel ('x')
ylabel ('y')
grid
fprintf('initial guess = (4,4)\n');
fprintf('error = 10^-20\n');
fprintf('Number of iteration = %d\n',ii-1);
toc
T = array2table(iwant) ;
T.Properties.VariableNames = {'Loop','alpha','x','y','f(x,y)','m','relative_norm'};
T

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by