Effacer les filtres
Effacer les filtres

Need help editing my Newton Raphson Code

1 vue (au cours des 30 derniers jours)
Matthew
Matthew le 18 Nov 2012
function [r,N]= newtonraphe7(fHan,x0,fTol,iterMax)
iterations=0;
max_no_roots=size(fHan,2);
r=0;
N=0;
for no_roots=1:max_no_roots-1
fun_root_new=x0;
flag=1;
coeff_der_function=derivate(fHan);
order_fun=size(fHan,2);
order_der_fun=size(coeff_der_function,2);
while flag==1
fun_root_old=fun_root_new;
fx=0;
dfx=0;
nonzero=1;
while nonzero==1
powers=order_fun-1;
for index=1:order_fun
fx=fx+fHan(index)*fun_root_old^powers;
powers=powers-1;
end
powers=order_der_fun-1;
for index=1:order_der_fun
dfx=dfx+coeff_der_function(index)*fun_root_old^powers;
powers=powers-1;
end
if dfx==0
fun_root_old=fun_root_old+1;
else
nonzero=0;
end
end
iterations = iterations + 1;
fun_root_new = fun_root_old - fx/dfx;
if iterations >= iterMax
flag=0;
elseif abs(fun_root_new-fun_root_old)<=fTol
flag=0;
r(no_roots)=fun_root_new;
N(no_roots)=fx;
end
end
end
I am trying to implement the Newton-Raphson algorithm to solve nonlinear algebraic equations. My function, newtonRaphE7, finds a real root of the nonlinear algebraic equation f(x) = 0, using a Newton-Raphson algorithm.
Input arguments:
  • • fHan: function handle to f(x)
  • • dfHan: function handle to f′(x) = df(x)/dx,
  • • x0: scalar, double, representing an initial guess, x0,
  • • fTol: tolerance for stopping criterion, i.e., f(r) ≤ fTol,
  • • iterMax: maximum number allowed iterations.
Output Arguments
  • • r: the final root estimate obtained by the algorithm,
  • • rHist: a vector containing the sequence of estimates including the initial guess x0,
  • • N: the total number of performed iterations,
  • • fRoot: the absolute value of the function f(x) at r, i.e., fRoot = f(r)
  • If the initial guess, x0 satisfies the stopping criterion, then the root r should be returned as x0, and N should be 0 since no actual iterations were performed.
In general, rHist contains the sequence of estimates x0, x1, x2, . . . , that are calculated by the algorithm. The first element of rHist should be x0, therefore the value returned in N should be equal to length(rHist)-1. In the case N ≥ 1, the function should compute the root, rE, using the MATLAB function fzero. Treat rE as the “exact” root, and produce a plot (using semilogy) of the absolute error (i.e., rHist − rE) as a function of iteration (i.e., 0:N).
I don’t know how to in cooperate the variables “dfHan”and “froot” and “rHist” into my function. Can someone help? Thanks!
  2 commentaires
bym
bym le 18 Nov 2012
At first blush, your code seems more complicated than the ordinary Newton Raphson implementation. I also don't see where you have implemented the function 'fzero'
Matthew
Matthew le 19 Nov 2012
Yeah, I originally just tried the Newton Raphson but my instructor told me that I needed to add the extra variables in there. I don't see HOW to include them in there though ...

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by