How to transform a plot to use a logarithmic scale?

Click For Summary

Discussion Overview

The discussion revolves around transforming a plot to use a logarithmic scale in MATLAB, particularly in the context of fitting an exponential decay model to simulated data. Participants explore the implications of using logarithmic scales for both the data and the fitting function, and seek clarity on the correct approach to implement this transformation.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their MATLAB code for generating and fitting data based on an exponential decay model, and asks for guidance on transforming the plot to a logarithmic scale.
  • Several participants suggest using MATLAB functions like semilogy, semilogx, and loglog, but the original poster expresses a desire to understand manual conversion methods.
  • There is a discussion about whether to convert y-values to log-scale before fitting a new logarithmic function, with some participants questioning the validity of directly plotting log(y) versus fitting a logarithmic model.
  • One participant attempts to fit a model after transforming y-values to log(y) and expresses doubt about the quality of the fit, noting that the transition to log-scale may not make sense due to the presence of a constant in the model.
  • Another participant points out that the fitting process in log-scale may disproportionately weight lower values, affecting the outcome of the fit.
  • A suggestion is made to change the axis scaling to logarithmic using MATLAB's axis properties, which would eliminate the need for manual logarithmic transformations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to transforming the plot to a logarithmic scale. There are competing views on whether to transform the data before fitting or to adjust the plot's axis directly.

Contextual Notes

Participants express uncertainty regarding the implications of using logarithmic transformations, particularly in relation to fitting models with constants and the weighting of data points in the fitting process.

Leonid92
Messages
45
Reaction score
2
I wrote the following code in MATLAB:
Code:
t = [0:0.001:0.1];
noise = randn(1,size(t,2));
a = 15*10^9;
b = 15*10^(-3);
c = 7*10^8;
y = a*exp(-t/b)+c+noise*100000000;
fun = @(p,t)p(1)*exp(-t/p(2))+p(3);
p0 = [15.5*10^9, 14*10^(-3), 6*10^8];
p = lsqcurvefit(fun, p0, t, y);
t_fit = [0:0.0001:0.1];
y_fit = fun(p,t_fit);
fig = figure('Visible','on');
plot(t,y,'.');
hold on;
grid on;
plot(t_fit,y_fit,'r','linewidth',2);
disp(sprintf('a=%d; b=%.4f; c=%d',p(1),p(2),p(3)))
Thus I generated data whose behavior is described by equation y = a*exp(-t/b)+c, with adding some noise. Then I fit mentioned function to these data and found values of parameters:
Code:
a=1.550000e+10; b=0.0149; c=6.000000e+08
The plot of simulated data and fit is below:
plot-exp.png

One person told me that it would be good to consider this problem in logarithmic scale. Could you please tell me, what is usually implied when talking about transforming ordinary plot to logarithmic scale? How should I implement this procedure? Should I first convert my simulated y-values into log-scale and after that perform fit with another function? What formulas should I use? Is it reasonable to use logarithm with base 10 or logarithm with base 'e'?
I will be very appreciate for any help or advice.
P.S. Unfortunately, I don't have opportunity to ask that person in more detail regarding this question.
 

Attachments

  • plot-exp.png
    plot-exp.png
    2.7 KB · Views: 1,835
Physics news on Phys.org
solve for t, it took me two lines, it is easily found.
 
  • Like
Likes   Reactions: Leonid92
Dr Transport said:
solve for t, it took me two lines, it is easily found.
You mean
Code:
b*ln( (y-c)/a ) = -t
?
I don't understand. I have simulated data and need to convert y-values to log-scale. Should I just take logarithm(y) ?
 
Have you tried substituting log(y_fun) for y_fun in the plot() line?
 
  • Like
Likes   Reactions: Leonid92
jedishrfu said:
Have you tried substituting log(y_fun) for y_fun in the plot() line?

In my code, I changed two lines:
Code:
plot(t,log(y),'.');
plot(t_fit,log(y_fit),'r','linewidth',2);
And the plot is:
plot-exp1.png

But I have doubt, is it right? I mean, is this what I need? What is the sense in this operation? I thought that firstly I should convert simulated y-values to log-scale, and then perform fitting with new, logarithmic function - fit logarithmic function to transformed y-values, is it right?
 

Attachments

  • plot-exp1.png
    plot-exp1.png
    3 KB · Views: 1,187
I tried the following:
Code:
t = [0:0.001:0.1];
noise = randn(1,size(t,2));
a = 15*10^9;
b = 15*10^(-3);
c = 7*10^8;
y = a*exp(-t/b)+c+noise*100000000;
y = log(y); % here is the change!
fun = @(p,t)log(p(1)*exp(-t/p(2))+p(3)); % here is the change!
p0 = [15.5*10^9, 14*10^(-3), 6*10^8];
p = lsqcurvefit(fun, p0, t, y);
t_fit = [0:0.0001:0.1];
y_fit = fun(p,t_fit);
fig = figure('Visible','on');
plot(t,y,'.');
hold on;
grid on;
plot(t_fit,y_fit,'r','linewidth',2);
disp(sprintf('a=%d; b=%.4f; c=%d',p(1),p(2),p(3)))

The plot is:
plot-exp2.png

I.e. firstly I converted simulated y-values to ln(y), and then performed fitting with function log( a*exp(-t/b)+c ), and it is seen that fit is not good.
Found parameters in this case are the following:
Code:
a=1.550000e+10; b=0.0156; c=6.000001e+08
So for me, transition to log-scale doesn't make sense.
 

Attachments

  • plot-exp2.png
    plot-exp2.png
    3 KB · Views: 1,097
Problem being the constant present, and the weights of the individual points. In the normal fit the points values had equal weights. In the fit of the log the low values get disproportional weights.
 
  • Like
Likes   Reactions: Leonid92

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K