Newtons method for finding minimum of a function.

35 visualizaciones (últimos 30 días)
Sarah Smith
Sarah Smith el 10 de Feb. de 2020
Respondida: Jim Riggs el 10 de Feb. de 2020
I want to mark the solution point (x,f(x)) obtained by the Newton's algorithm on the curve of the function f(x)=(e^(2*sinx))-x to see if it is a local minimum or something else, but I am stuck. I used myfunction in order to obtain the function value and its derivatives Can someone help me with this? I will show you what I have so far.
% Clears all data and screen
clear;
close all;
clc;
f1 = @(x) exp(2*sin(x))-x; %function
x0 = 3; %initial guess
maxIter = 1e6; %max iteration of the function
epsilon = 10e-6; %tolerance value
% Calling the Newton's Method function
[xk,i,error,errorVect] = NewtonsMethod(x0,maxIter,epsilon);
% Printing Newton's Method Results in the console
fprintf('\nThe solution of the function exp(2*sin(x))-x = 0 is %4.5f in %u iteration with %e error.\n', xk,i,error)
xk;
ymin = f1(xk);
errorVect;
% Function for Newton's Method function
%Graphing the error vector
x_axis = 1:1:length(errorVect); %x-axis values
semilogy(x_axis,errorVect,'-mo'); %graph of error vector in logarithmic y-axis
grid on %logarithmic grid (y-axis only)
function[xk,i,error,errorVect] = NewtonsMethod(xk,maxIter,epsilon)
xold = xk;
for i = 1:maxIter
[f1x,df1x,ddf1x]=myfunction(xk);
xk = xk - (df1x/ddf1x);
error = abs(xk-xold);
xold = xk;
errorVect(i) = error;
if (error<epsilon)
break;
end
end
end
% Function for calling the value and the derivative of the function
function [f,g,h] = myfunction(x)
f1 = @(z) exp(2*sin(z))-z; %function
f = f1(x);
syms z
if nargout > 1
g1(z) = diff(f1(z));
g = double(g1(x));
h1(z) = diff(g1(z));
h = double(h1(x));
end
end
  2 comentarios
Jim Riggs
Jim Riggs el 10 de Feb. de 2020
It will be much easier for people to read your code and help you if you use the formatting buttons to format your code.
Or, simply select your code and press <alt> <enter> to format it. Try it.
Jim Riggs
Jim Riggs el 10 de Feb. de 2020
for your myFunction, try the following;
function [f,g,h] = myfunction(x)
f1 = @(z) exp(2*sin(z))-z; %function
ff = f1([x-1, x, x+1]); % this is a 3-element vector
f = ff(2); % the middle value is f1(x)
if nargout > 1 % (not sure why you use this)
gg = diff(ff); % this is a 2-element vector from diff(ff)
g = mean(gg);
h= diff(gg); % single value from diff(gg)
end
end

Iniciar sesión para comentar.

Respuestas (1)

Jim Riggs
Jim Riggs el 10 de Feb. de 2020
Here is another variation;
function [f,g,h] = myfunction(x)
f1 = @(z) exp(2*sin(z))-z; %function
dx = 0.1;
ff = f1([x-dx, x, x+dx]); % this is a 3-element vector
f = ff(2); % the middle value is f1(x)
if nargout > 1 % (not sure why you use this)
gg = diff(ff)./dx; % this is a 2-element vector from diff(ff)
g = gg(1);
h= diff(gg)./dx; % single value from diff(gg)
end
end

Categorías

Más información sobre Animation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by