How can I show NaN as Error not Exist?

1 次查看(过去 30 天)
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

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by