How to transform a plot to use a logarithmic scale?

In summary: YScale-You can change the axis scaling to logarithmic with the XScale/YScale properties of the axes object in the figure:ax = gca;ax.XScale = 'log';ax.YScale = 'log';
  • #1
Leonid92
45
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,644
Physics news on Phys.org
  • #3
  • #4
solve for [itex] t [/itex], it took me two lines, it is easily found.
 
  • Like
Likes Leonid92
  • #5
Dr Transport said:
solve for [itex] t [/itex], 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) ?
 
  • #6
Have you tried substituting log(y_fun) for y_fun in the plot() line?
 
  • Like
Likes Leonid92
  • #7
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,065
  • #8
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: 992
  • #9
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 Leonid92
  • #10
  • Like
Likes Leonid92

1. How do I determine if a logarithmic scale is appropriate for my plot?

A logarithmic scale is typically used when the data being plotted spans a large range of values. It is useful for visualizing data that increases or decreases exponentially rather than linearly. You can determine if a logarithmic scale is appropriate by plotting your data on a linear scale first and examining the pattern of the data. If the data appears to increase or decrease at a faster rate as the x-axis values increase, then a logarithmic scale may be more suitable.

2. How do I change the scale of my plot to logarithmic?

This process can vary depending on the software or programming language you are using to create your plot. However, most programs will have an option to change the scale of the x-axis and/or y-axis. Look for a setting that allows you to switch from a linear to a logarithmic scale. In some cases, you may need to manually input the logarithmic values of your data points.

3. How does a logarithmic scale affect the interpretation of my data?

Using a logarithmic scale can change the way data is perceived, as it compresses the data at lower values and expands it at higher values. This means that small changes at lower values will appear larger on the plot, while larger changes at higher values will appear smaller. It is important to keep this in mind when interpreting the data and to avoid making assumptions based solely on the visual representation of the plot.

4. Can I use a logarithmic scale for all types of data?

A logarithmic scale is most commonly used for data that increases or decreases exponentially. However, it may not be appropriate for data that is inherently linear, such as time series data. It is important to consider the nature of your data and the purpose of your plot before deciding to use a logarithmic scale.

5. Are there any limitations to using a logarithmic scale for my plot?

One potential limitation of using a logarithmic scale is that it can make it difficult to accurately compare data points that are close in value. This is because the spacing between values on a logarithmic scale is not uniform. Additionally, a logarithmic scale may not accurately represent the magnitude of the data if the values are not evenly distributed. In these cases, it may be more appropriate to use a different type of scale or to transform the data in a different way.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
Back
Top