How to get rid of "negative data ignored" warning in a loglog plot

43 views (last 30 days)
In fitting a least square line through a set of points on a loglog scale, MATLAB is returning the warning "Negative data ignored". For example,
N = [56 112 224 448 896 1792 3584 7168 14336];
T_max = [0.444793435182132 0.4395279003565365 0.43638330717684004 0.4344924804848505 ...
0.43374154539955007 0.43337438343040974 0.4331928605464643 ...
0.4331026128636092 0.4330576104805632];
T_min = [0.4172265675535062 0.42604231767208034 0.4304025036668112 ...
0.4317013137271798 0.4324741371494442 0.4328174950027687 ...
0.4329158371978752 0.4329426670435078 0.43298763322951783]
T_exact = 5*sqrt(3)*(0.1/2);
T_min_exact = abs(T_min - T_exact);
c2 = polyfit(N,T_min_exact,1);
y2 = polyval(c2,N);
loglog(N,T_min_exact, '*',N,y2,'R')
The above code produces the warning "Negative data ignored" and is producing the following plot:
How to overcome this problem and what am I doing wrong here?

Accepted Answer

Image Analyst
Image Analyst on 20 May 2021
If you want to fit a line on the log-log plot, you'll have to take the log of x and log of y and fit that to a line. Then to display with loglog(), you'll have to "unlog" the line (exponentiate the fitted x and fitted y) to get it in the original space as the original data. You don't want to use loglog() on data that has already been logged!
Or better yet, use nlmfit(). I'm attaching several examples of nlmfit().
  3 Comments
Avishek Mukherjee
Avishek Mukherjee on 20 May 2021
I am not quite sure what you meant by "you'll have to "unlog" the line (exponentiate the fitted x and fitted y) to get it in the original space as the original data".
I tried the following code and got the graph
c1 = polyfit(log(N),log(T_max_exact),1);
y1 = polyval(c1,log(N));
loglog(N, T_max_exact, 'O')
hold on
plot(log(N),exp(y1),'G')
Avishek Mukherjee
Avishek Mukherjee on 20 May 2021
I got the desired results. Thanks a lot!
I made an unintentional mistake of plugging in the log befor N.

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 20 May 2021
You had better select higher order of polynomial to fit or choose a different fit model, e.g., log function
...
c2 = polyfit(N,T_min_exact,8); % Order # 8.
y2 = polyval(c2,N);
loglog(N,T_min_exact, '*',N,y2,'r-')
  1 Comment
Avishek Mukherjee
Avishek Mukherjee on 20 May 2021
I am trying a least square fit here with a straight line, so degree 8 will not work.
Sorry for the confusion. I should have mentioned that in my post. My apologies.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!