MATLAB How to transform a plot to use a logarithmic scale?

AI Thread Summary
Transforming a plot to a logarithmic scale involves converting the y-values to log scale, which can be done using natural logarithm or logarithm base 10. The discussion highlights that after converting y-values to log scale, the fitting function should also be adjusted accordingly. However, it was noted that fitting in log scale can lead to disproportionate weighting of lower values, potentially affecting the fit quality. An alternative approach is to change the axis scaling directly in MATLAB using properties like YScale, which allows for a logarithmic display without manual conversion of data. This method simplifies the process and maintains equal weighting across data points.
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,764
Physics news on Phys.org
solve for t, it took me two lines, it is easily found.
 
  • Like
Likes 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 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,145
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,062
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

Similar threads

Replies
2
Views
3K
Replies
12
Views
4K
Replies
4
Views
2K
Replies
18
Views
4K
Replies
1
Views
3K
Replies
7
Views
3K
Back
Top