How can I show NaN as Error not Exist?

1 view (last 30 days)
Janmar Olan
Janmar Olan on 11 Aug 2022
Answered: Fangjun Jiang on 11 Aug 2022
This code Im using is to show the error in Trapezoidal Rule, and upon calculation, error is NaN, and I wanted to display NaN as "Error Does Not Exist". However,even this code show Error Does Not Exist, but if i change the value of (a) and (b) it still shows Error Does not Exist even the answer is not NaN. Please help.
syms x
f = 2*x^3+(3/(x^2))-(1/x); K2 = diff(f,2); der = inline(diff(f,2)); u = der(2); p = der(0);
a = 0; b = 2; n = 4;
if (u > p) K2 = u; else K2= p; end
disp('Relative Error of the Integral is;');
m = ('"ERROR DOES NOT EXIST"');
ErrorT = (K2*(b-a)^3)/(12*n^2);
ErrorT =(m(~isnan(m)));
disp(ErrorT);
  1 Comment
Fangjun Jiang
Fangjun Jiang on 11 Aug 2022
Edited: Fangjun Jiang on 11 Aug 2022
Please double check to see if you have typos. The code doesn't make sense. Variable ErrorT is immediately re-assigned. Variable m is a fixed char array. no point to do m(~isnan(m))
m = ('"ERROR DOES NOT EXIST"');
ErrorT = (K2*(b-a)^3)/(12*n^2);
ErrorT =(m(~isnan(m)));

Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 11 Aug 2022
I guess the true intension is like the code below. If ErrorT is initially NaN, then "ERROR DOES NOT EXIST" will be displayed. If not, it will display empty, which is not as good as my recommendation.
syms x
f = 2*x^3+(3/(x^2))-(1/x); K2 = diff(f,2); der = inline(diff(f,2)); u = der(2); p = der(0);
a = 0; b = 2; n = 4;
if (u > p) K2 = u; else K2= p; end
disp('Relative Error of the Integral is;');
Relative Error of the Integral is;
m = ["ERROR DOES NOT EXIST"];
ErrorT = (K2*(b-a)^3)/(12*n^2);
ErrorT =(m(isnan(ErrorT)));
disp(ErrorT);
ERROR DOES NOT EXIST
My recommendation is below. When ErrorT is some value, it will actually display the value.
syms x
f = 2*x^3+(3/(x^2))-(1/x); K2 = diff(f,2); der = inline(diff(f,2)); u = der(2); p = der(0);
a = 0; b = 2; n = 4;
if (u > p) K2 = u; else K2= p; end
disp('Relative Error of the Integral is;');
Relative Error of the Integral is;
ErrorT = (K2*(b-a)^3)/(12*n^2);
if isnan(ErrorT)
ErrorT="ERROR DOES NOT EXIST";
end
disp(ErrorT);
ERROR DOES NOT EXIST

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by