samedi 29 octobre 2016

This if is not working on Octave/Matlab

I have the code divided on three functions:

f.m (This function works)

function y=f(x)
y = x^2;
endfunction

df.m (The function that return the derivate of y=x^2. This function works)

function y=df(i)
  syms x,diff(f(x),x);
  subs(ans,x,i);
  y = ans;
endfunction

newton.m (maxIt = max of tries)

function [x1,nIt] = newton (f,df,x0,tol,maxIt)
   k=0;
   x=x0;
   nIt = 0;
   while (k<maxIt)
     if(df(x)== 0)
       fprintf("La derivada, da resultado 0");
       k=maxIt;
     else
       i = x - (f(x)/df(x))
       x = i;
       nIt = nIt + 1
       k=k+1;
     endif
  end
endfunction

Ok, i execute this comand "newton(@f,@df,0,10,2)", 2/3 of the program works good but if i do df(0), the result of this is 0 but the if dont works. I verify if df(0) return 0 and it´s ok. I´m new in Matlab/Octave. Sorry for the "english".

Aucun commentaire:

Enregistrer un commentaire