How can I show NaN as Error not Exist?

2 ビュー (過去 30 日間)
Janmar Olan
Janmar Olan 2022 年 8 月 11 日
回答済み: Fangjun Jiang 2022 年 8 月 11 日
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 件のコメント
Fangjun Jiang
Fangjun Jiang 2022 年 8 月 11 日
編集済み: Fangjun Jiang 2022 年 8 月 11 日
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)));

サインインしてコメントする。

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 8 月 11 日
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