Search code examples
matlabnewtons-method

how to call and use function Recursivity in matlab


I have an exercise with newton way calculate y(x+1)=y(x)-f(x)/f'(x) in this function I need y(x) and for this I use function Recursivity for y(1) & y(2) it's working because y(1) has formula y(1)=R*T/p ,for save y(x) I use zeros()to use when calculate y(x+1) but for x>2 I get the same answer,what am I missing??what can I use instead of zeros() for save and access to newt(x-1)

function y= newt(x)
%define beta,gamma,delta,....there
y(1)=R*T/p;
answ=zeros(1,20);
z=0;
if  x==1
   f=(R*T*y(1)^3)+(beta*y(1)^2)+(gamma*y(1))+delta-(p*y(1)^4);          
   f1=(3*y(1)^2*R*T)+(2*y(1)*beta)+gamma+(4*p*y(1)^3);          
   answ(1) = y(1);
    fprintf('n=1 v=%f\n',y(1));    
else
    y=newt(x-1);
    f=(R*T*y^3)+(beta*y^2)+(gamma*y)+delta-(p*y^4);
    f1=(3*y^2*R*T)+(2*y*delta)+gamma+(4*p*y^3);
    z=y-f/f1
    answ(1,2:x)=z;
end
  answ(1) = y(1);
   answ(1,2:x)=z;

Solution

  • according to @patrik guidance,I find my answer

    f=@(v)p*v^4-R*T*v^3-beta*v^2-gamma*v-delta;
    f1=@(v)4*p*v^3-3*R*T*v^2-2*beta*v-gamma;