Please help me how to run it :(
Mostrar comentarios más antiguos
% MATLAB script file implementing the method of steepest descent
%Inputs:
% x = starting vector
% xa, xb = x-interval used in contour plot
% ya, yb = y-interval used in contour plot
% tol = tolerance for stopping iteration
% Required m-file
% fp.m is m-file for function f(x)
% grad.m is m-file for gradient of f(x)
x=[-1.2;1.8];
xa=-1.5; xb=2; ya=-1; yb=2; xd=linspace(xa,xb,20);
yd=linspace(ya,yb,20); [X,Y]=meshgrid(xd,yd);
%% Example 1 Rosenbrock’s function
% x=[-1.2, 1.8];
% xa=-1.5;
% xb=2;
% ya=-1;
% yb=2;
% steep;
% Z=100*(X.^2-Y).^2+(X-1).^2;
%% Example 2
% x=[-1, 0.8]; xa=-1; xb=1; ya=-1; yb=1; steep;
% x=[1, 0.8];
% x=[-0.8, -0.5];
% Z=(X-Y).^4+8*X.*Y-X+Y+3;
% Z=10*x^2+y^2;
tol=1.0*10^(-4); xpoints=x(1); ypoints=x(2); df=grad(x(1),x(2));
dfx=df(1); dfy=df(2); df1=[dfx, dfy]; d1=-df1; q=1; counter=1;
while norm(q)>tol
counter=counter+1;
s=fminbnd('fp',0,1,optimset('TolX',0.00001),x(1),x(2),d1(1),d1(2));
q=s*d1;
xx=x+q';
df=grad(xx(1),xx(2));
dfx=df(1); dfy=df(2);
df1=[dfx, dfy];
d1=-df1;
x=xx;
fprintf('\n %i Computed Solution = %e %e',counter,x);
xpoints=[xpoints, x(1)];
ypoints=[ypoints, x(2)];
hold on
contour(X,Y,Z,30,'b')
plot(xpoints,ypoints,'ko-')
hold off
pause
end
error=sqrt((1-x(1))^2+(1-x(2))^2);
fprintf('\n %i Computed Solution = %e %e Error = %e',counter,x,error);
There is error that says me 오류 발생: Steep_descent_method (30번 라인)
tol=1.0*10^(-4); xpoints=x(1); ypoints=x(2); df=grad(x(1),x(2));
How to solve it clearly?
1 comentario
Respuestas (1)
Cris LaPierre
el 30 de Nov. de 2022
0 votos
The error is caused by this code: df=grad(x(1),x(2));
It can't find your m-file function grad.m. From your file comments (line 9)
% grad.m is m-file for gradient of f(x)
Make sure grad.m is in your current folder, or in a folder that has been added to your MATLAB path. See here:
Categorías
Más información sobre Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!