0

I would like to implement the following: ive written a script that executes the Newton Raphson algorithm for my specific function. Now I would like to write a script that repeats itself using the previous found zero as my next intial starting point:

x=zeros(1,31);
for i=1:31
    x(i)=(i-1)/10;
end
y0=0;
for i=1:length(x)
    y0=newton(x(i),y0)
end

So, I want this script to execute newton(x,y0). So it will start with newton(0,0), it will find a new value y0 and then I want the script to execute newton(0.1,y0) etc. I want these values to be displayed in a table together with the number of iterates that was needed to find the value y0.

I hope my question is clear. Thanks in regards.

Again: I have a vector x with elements 0, 0.1, 0.2, ...,3 When I implement x(i) with initial value y0 newton(x,y) will give me a value. Then I want the script to execute newton(x,y) again with value x(2) for x and the previous found y0.So I need some kind of loop, but I can't get it done .. :(

EDIT
This is my newton-function:

function nulpunt=newton(x,y0)

tolerantie=1e-8;
iteraties=0;

while (abs(functie1(y0,x))>tolerantie)
    y0=y0-functie1(y0,x)/afgeleide_functie1(y0);
    iteraties=iteraties+1;
end

if iteraties==100;
    fprintf('Maximaal aantal iteraties bereikt')
else
    fprintf('De benadering van het nulpunt y*(%.2f) is %.4f gevonden in %d iteraties.\n',x,y0,iteraties)
end
end
Schorsch
  • 7,761
  • 6
  • 39
  • 65

1 Answers1

0

Your loop should already use the previous y0 every consecutive time it operates.
If you want to display the y0 and the number of iterations in the Matlab command window, you first need to change the newton-function to return not just y0 but also the number of iterations, iteraties:

function [nulpunt, iteraties]=newton(x,y0)

Also, your function should actually return the final y0as nulpunt:

if iteraties==100;
    fprintf('Maximaal aantal iteraties bereikt')
    nulpunt = 0; % in this case, no solution was found, return the baseline value;
else
    fprintf('De benadering van het nulpunt y*(%.2f) is %.4f gevonden in %d iteraties.\n',x,y0,iteraties)
    nulpunt = y0;
end

to display the number of iterations and y0 after each execution of newton, add these two lines after you call the function:

[y0, iteraties] = newton(x(i),y0);
disp(['This point was found: ',num2str(y0)])
disp(['It took ',num2str(iteraties),' iterations.'])  

This may actually be redundant, as your function already outputs which y0 it found and how long that took.

Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • In matlab i get the error that ''nulpunt'' is not assigned during the call. Furthermore, the newton function itself works fine as long as you give it a value for x and y0. The problem is that I want a loop that given a value of x and y0 it uses the found value as the new y0 –  Jan 18 '14 at 17:41
  • Try again. Your function actually needs to assign a value to `nulpunt`- I assume, you want `y0` for that. You should already have on-screen output of your solution. As long as you are inside the loop, the value for `y0` will be overwritten after each call to `newton` hence being used as the starting value for the next run. – Schorsch Jan 18 '14 at 18:40