Newton's function matlab
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have started answering a question about newton's method in matlab however am not sure if my coding is correct. The question asks to find the zeros of a function f (not defined) using the prototype function [x , res , xvec , resvec ] = newton (f , df , x0 , maxiter , tol ).(xk) is a sequence generated by the approximations and x* is the actual root of f. Here the function needs to return x, f(x), a vector containing sequence (xk) including initial guess and final approximation, and resvec (same dimension as the previous vector) containing (f(xk)). and the function should also print out a few things which I have written in my code plus a printout of whether convergence has been achieved or not (which I'm not sure how to do). This is what I have so far:
function [x , res , xvec , resvec] = newton( f, df, x0, maxiter, tol )
%
% NEWTON Newton's Method
% Newton's method for finding better approximations to the
% zeroes of a real-valued function.
%
% Input:
% f - input funtion
% df - derived input function
% x0 - initial aproximation
% maxiter - maximum number of iterations allowed
% tol - the required tolerance
%
% Output:
% x - variable containing approximation to x(*)
% res- variable containing the residual |f(x)| evaluated at the approximate solution x
% xvec- vector containing {x(k)}, including initial guess and final approximation
% resvec- vector of the same dimension of xvec, containing {|f(xk)|}.
%
function x(k) = newton( f, df, x0, maxiter, tol )
k(1:maxiter)
f = @x(k)
df = @(x(k))'
x(k)= x0-(f(x0)/df(x0))
x(k+1) = x(k) - (f(x(k))/df(x(k)))
end
%continue iterating until stopping conditions met
%output depends on success of algorithm
while (iter < maxiter)
while (abs(f(n)-(f(n-1)) > tol)
x(1)= x0-(f(x0)/df(x0))
elseif (iter > maxiter)
error('maximum amount of iterations exceeded')
else error('distance between last 2 points is less than stated tolerance')
end
end
fprintf('step(k) x(k) |f(xk)| |x(k+1)-x(k)|')
fprintf('------ ----------- --------- -------------\n')
fprintf('%d %d %.4f %.4f\n', k, x(k), mod(f(xk)), mod(x(k+1)−x(k)) )
How would I then code for matlab to tell me if convergence has been achieved and is this code correct?
Many thanks!
0 Kommentare
Antworten (1)
John D'Errico
am 6 Dez. 2015
Bearbeitet: John D'Errico
am 6 Dez. 2015
So, while I like that I see code with comments at the top, describing the inputs, I also see many spots that will simply fail. I also see unterminated (no semi-colon) lines, so you will get lots of junk displayed at the command line.
Consider this line:
k(1:maxiter)
Um, that does nothing. k is undefined. Are you trying to define k in some way here?
Next, consider this line:
f = @x(k)
While you pass in a function f, then you decide to over-ride your input function. Worse, this call is meaningless in MATLAB. You appear to be trying to define the function hand f, to be a function of something called x, which you are then indexing with k? However, x is undefined, as well as k. The function handle has no arguments, so this will be a complete failure.
Next we see this:
df = @(x(k))'
that prime on the end of the line does NOT differentiate. The ' operator in MATLAB is a matrix (or vector) TRANSPOSE operator.
How about these?
x(k)= x0-(f(x0)/df(x0))
x(k+1) = x(k) - (f(x(k))/df(x(k)))
Again, x is undefined. f was not defined properly as a function. k is undefined.
Interestingly, it seems like you are trying to follow the basic formulas for Newton's method, in somehow defining x(k) up front. But then inside the loop, you create x(1) repeatedly.
I think that while you have made an admirable attempt here, you need to spend some time learning MATLAB syntax.
Do not overwrite f. You passed it in as an argument. You need not explicitly somehow designate it as a function. That should have happened when you created f initially before you passed it in.
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!