//RTD PT1000 Reading function
//(C)BasicAirData 2013
//EN 60751 Method Style
//www.basicairdata.eu

//Callendar Van Dusen parameters initialitation
R0 = 1000  
A=3.9083e-3
B=-5.775e-7
C=-4.183e-12

R=input("Please enter resistance value :");
t0=0;//Initial guess value for Newton method
errtemp=0;//Error value

if R>1000 then
t1=(-R0*A+(R0^2*A^2-4*R0*B*(R0-R))^0.5)/(2*R0*B)
mprintf("Current temperature value %f\n",t1);
else //Newton method to find implicit equation solution
    i=0;
    while (abs(errtemp)>1/100)|i==0    
    t=t0;
    f=(t - 100)*C * t^3+ B * t^2 +A * t  + (1- R/R0);
    fdot=4*C*t^3-300*C*t^2+2*B*t+A;
    t1=t0-f/fdot;
    mprintf("Iteration %i\n",i);
    mprintf("Current temperature value %f\n",t1);
    errtemp=(t1-t0);
    mprintf("Current step innovation %f\n",errtemp);
    t0=t1;
    i=i+1;
end
end


