3
votes

I am trying to execute a function ,it works as intended until line 83 where it is

disp(['The current root at the ', num2str(k),'-th iterate is ', num2str(p1, 10.5)]);

Here is my actual code:

function newton(p)

Delta=10e-2;
Epsilon=10e-2;
Small=10e-3;
Max=99;

Cond=0;
k=0;

y0=fny(p);

disp(' ');
disp(['Date = ', date]);
disp(['The Newton-Raphson Iteration with Delta=', num2str(Delta), ' , Epsilon=', num2str(Epsilon)...
        ,' and Small=', num2str(Small)]);   
disp(['The maximum number of iterations allowed is ', num2str(Max)]);
disp(' ');



    disp('   k                 p(k)          p(k+1)-p(k)               f(p(k))');
    disp('|---|--------------------|--------------------|---------------------|');

while k<=Max & Cond==0,
       
    [y_, Df]=fny(p);

    if Df==0,
       Cond=1;
       Dp=0;
   else
       Dp=y0/Df;
   end
   
   p1=p-Dp;
   y1=fny(p1);
   
   RelErr=2*abs(Dp)/(abs(p1)+Small);
    
   ds_i=sprintf('%4d %20.10g %20.10g  %20.10g', k, p, abs(p1-p), fny(p));
   disp(ds_i);

   
   if RelErr<Delta & abs(y1)<Epsilon,
       
       if Cond~=1,
           Cond=2;
       end
   end
   
   p=p1;
   y0=y1;
      
k=k+1;

pokis(k)=(p);

end

ds_i=sprintf('%4d %20.10g %20.10g  %20.10g', k, p, p1-p, fny(p));
disp(ds_i);

disp(' ');
disp(['The current root at the ', num2str(k),'-th iterate is ', num2str(p1, 10.5)]);
disp(['Consecutive iterates differ by ', num2str(Dp,10.5)]);
disp(' ');

if Cond==0,
   disp('The maximum number of iteration was exceeded! ');
elseif Cond==1,
    disp('Division by zero was encountered!');
elseif Cond==2,
    disp('The root was found with the disired tolerances!');
end

It is working fine in Matlab but i don't know why it is giving me error on Gnu-Octave. I enter it as newton(1) and the graph output is fine but disp functions are not displaying as intended

2
Please read minimal reproducible example. You post way too much code to illustrate the problem. Also, please clarify why it doesn’t work: error message? (copy-paste the full error message) wrong output? (include actual and expected output). - Cris Luengo

2 Answers

2
votes

without a complete example with inputs and error messages it's tough to be sure what the exact problem is. However it appears you've stumbled into undocumented Matlab behavior that is incompatible with Octave actually following the documented behavior of num2str. According to the Matlab function description precision must be a positive integer specifying the "maximum number of significant digits in the output string". Octave has this same stated behavior as per Octave's function help for num2str. Despite this stated requirement, Matlab appears to accept a non-integer input for n, whereas Octave produces the error your reported. A few quick tests seem to suggest that Matlab applies the round function to the value. E.g., in Matlab 2020a:

>> num2str(123322,2)
ans =
    '1.2e+05'

>> num2str(123322,3)
ans =
    '1.23e+05'

>> num2str(123322,2.6)
ans =
    '1.23e+05'

>> num2str(123322,2.5)
ans =
    '1.23e+05'

>> num2str(123322,2.4999)
ans =
    '1.2e+05'

So I'm not sure what behavior you were hoping to get by specifying precision = 10.5 in num2str, but it appears you were getting the effect of precision = 11 in Matlab, but an error in Octave.

Regarding your comment above about 'fixing' the problem by changing to mat2str, while this will likely now work for you in Octave, I expect you will get errors in Matlab if you wanted this to be compatible code. In Matlab, once again the help documentation for mat2str states that precision is to be "specified as a positive integer". This time, Matlab 2020a throws an error:

>> mat2str(123322,2.4999)
Error using mat2str (line 51)
Precision must be a positive integer scalar.

but while Octave's function help for mat2str also says it expects integer inputs, it apparently allows non-integers. Instead of applying round, however it seems to apply floor, truncating any decimal part. E.g., in Octave 5.2.0:

>> mat2str(123322,2)
ans = 1.2e+05

>> mat2str(123322,2.4999)
ans = 1.2e+05
    
>> mat2str(123322,2.5)
ans = 1.2e+05

>> mat2str(123322,2.6)
ans = 1.2e+05

>> mat2str(123322,3)
ans = 1.23e+05

In short, you've found a workaround that allows your code to run. However, you're going to get a different result in Octave than you had in Matlab. You were specifying precision = 10.5. Matlab's num2str interprets that as 11. Octave's mat2str interprets that as 10.

I would recommend you instead ask for the actual precision you want, 10 or 11, in which case either program or function would work for this purpose.

1
votes

Found the solution:

Replace num2str with mat2str