How to use Newton-Ramphson method to find an equation root?

2 次查看(过去 30 天)
Hi, I want to make all of them one M-file by making the second and the third M-file as anonymous functions in the main M-file( f and df are defined as anonymous functions).
THE CURRENT MAIN CODE
function [p0,err,k,y]=newtonlab(f,df,p0,delta,epsilon,max1)
%Input - f is the object function
% - df is the derivative of f
% - p0 is the initial approximation to a zero of f
% - delta is the tolerance for p0
% - epsilon is the tolerance for the function values y
% - max1 is the maximum number of iterations
%Output - p0 is the Newton-Raphson approximation to the zero
% - err is the error estimate for p0
% - k is the number of iterations
% - y is the function value f(p0)
%If f and df are defined as M-file functions use the @ notation
% call [p0,err,k,y]=newton(@f,@df,p0,delta,epsilon,max1).
%If f and df are defined as anonymous functions use the
% call [p0,err,k,y]=newton(f,df,p0,delta,epsilon,max1).
% NUMERICAL METHODS: Matlab Programs
for k=1:max1
p1=p0-f(p0)/df(p0);
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
y=f(p0);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon),break,end
end
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*SECOND M_FILE *_ITALIC TEXT_
function y=f(x)
%%input to function
format short e;
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
M_inv = inv(Params.M); %optimization
zm = Params.zm;
y= zm'*M_inv*inv(M_inv+x.*Q)*M_inv*zm;
THE THIRD M_FILE:
function y1=df(x)
%%%%input to the equation
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
M_inv = inv(Params.M); %optimization
zm = Params.zm;
y1=-zm'*M_inv*inv(M_inv+x.*Q)*Q*M_inv*zm;
  1 个评论
Walter Roberson
Walter Roberson 2011-12-1
Is this question asking something different than your previous question, http://www.mathworks.com/matlabcentral/answers/22711-newton-raphson-method ? If not then the two should be merged together and one of them deleted.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-12-1
λ is not a valid variable name in MATLAB. This code would never have passed m-lint. Please do basic error checking on your code before you post it.
It is fairly unlikely that a program would use both fzero() and Newton Raphson.
[EDIT: Dec 4 2011 17:42 CDT - put in explicit merged code]
function newtonlab_driver
[p0, err, k, y] = newtonlab(@f, @df, 0, 100, 1e-8, 1000);
fprintf(1, 'Best answer was %g at lambda = %g\n', p0, y);
function [p0,err,k,y]=newtonlab(f,df,p0,delta,epsilon,max1)
%Input - f is the object function
% - df is the derivative of f
% - p0 is the initial approximation to a zero of f
% - delta is the tolerance for p0
% - epsilon is the tolerance for the function values y
% - max1 is the maximum number of iterations
%Output - p0 is the Newton-Raphson approximation to the zero
% - err is the error estimate for p0
% - k is the number of iterations
% - y is the function value f(p0)
%If f and df are defined as M-file functions use the @ notation
% call [p0,err,k,y]=newton(@f,@df,p0,delta,epsilon,max1).
%If f and df are defined as anonymous functions use the
% call [p0,err,k,y]=newton(f,df,p0,delta,epsilon,max1).
% NUMERICAL METHODS: Matlab Programs
for k=1:max1
p1=p0-f(p0)/df(p0);
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
y=f(p0);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon),break,end
end
function y=f(x)
%%input to function
format short e;
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
M_inv = inv(Params.M); %optimization
zm = Params.zm;
y= zm'*M_inv*inv(M_inv+x.*Q)*M_inv*zm;
function y1=df(x)
%%%%input to the equation
Params = load('saved_data.mat');
theta = pi/2;
zeta = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';
%T is a matrix(5,5)
Mroot = Params.M.^(1/2); %optimization
T = Mroot*Q*Mroot;
%find the eigen values
E =real( eig(T));
%find the negative eigen values -- %find the smallest negative eigen value
gamma = min(E);
%now solve for lambda
M_inv = inv(Params.M); %optimization
zm = Params.zm;
y1=-zm'*M_inv*inv(M_inv+x.*Q)*Q*M_inv*zm;
  28 个评论
zayed
zayed 2011-12-5
So I should put 'load file and do the next step involving calculating gamma and also (I,Q,M_inv) to get rid of duplication, on the line after the first line (function newtonlab_driver) in the edited code
zayed
zayed 2011-12-6
How can I call the result of the FUNCTION (newtonlab_driver) -in the thread below-into another code.Also I need to change the intial value guess p0 to changed as gamma changed .
http://www.mathworks.com/matlabcentral/answers/22787-how-to-use-newton-ramphson-method-to-find-an-equation-root
Thanks

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by