1
$\begingroup$

I have a Log-Linear plot, and i'm unsure about how to fit a line to it. The data represents a I (current) vs V (voltage) curve. The exponential portion of this curve (linear in a Log-Linear plot) is:

data={{0.820667, 0.0123147}, {0.827131, 0.0133158}, {0.838766, 
  0.0155183}, {0.851694, 0.0189221}, {0.852987, 0.0231268}, {0.8685, 
  0.0279321}, {0.876257, 0.0337385}, {0.882721, 0.0400455}, {0.898235,
   0.046853}, {0.903406, 0.0541612}, {0.912455, 0.0623702}, {0.924091,
   0.0714804}, {0.926676, 0.0804904}, {0.937019, 
  0.0900009}, {0.952532, 0.100513}, {0.957703, 0.111625}, {0.968046, 
  0.123338}, {0.977095, 0.135652}, {0.988731, 0.149267}, {0.991316, 
  0.162782}, {1.00166, 0.176597}, {1.01459, 0.191714}}

So a ListLogPlot of this data looks like this:

ListLogPlot[data,AxesLabel->{"Voltage", "Log I"}]

I want a linear fit of a data and a plot of both linear fit and data in a Log linear plot. Ideally like this:

Desire result

Now i´m not interested in the saturated part of a data show in the ideal grahics. I only want a linear fit of a linear part of the Log I vs V curve. I searched extensively on the Internet and the most similar answers appears here:

Logarithmic curve fit in data

Plot least squares curve on Linear Log Plot

Line of best fit on LogLog plot

I tried to adapt the solutions of these answers unsuccessfully to my data :(

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Apr 2 '15 at 1:20
  • $\begingroup$ When you say "linear fit," do you mean a 'true' linear fit, or a linear fit in log-linear space (i.e. an exponential fit)? $\endgroup$ – 2012rcampion Apr 2 '15 at 14:47
  • $\begingroup$ I mean a linear fit in a Log-linear space, an exponential fit in linear space. $\endgroup$ – Siomel savio Apr 2 '15 at 15:35
  • $\begingroup$ @Siomelsavio If I understand your question correctly, you want to fit only the "linear" portion of your dataset (i.e. really the exponential response in the original data), and you don't care for the rest saturation part of the response. Is that right? If so, I provided a possible method in an answer below. Let me know if this is what you were looking for. $\endgroup$ – MarcoB May 1 '15 at 1:30
  • $\begingroup$ Indeed MarcoB. That is the type of processing that i want to implement in the data. You understand the question correctly !! Now i`ll read carefully your answer. $\endgroup$ – Siomel savio May 1 '15 at 22:36
2
$\begingroup$

I understand your question to mean that you want to fit only the "linear" portion of your dataset (i.e. really the exponential response in the original data), and you don't care for the rest saturation part of the response.

On general principles I would suggest that you fit the data to a nonlinear exponential model, rather than to a linearized one. All modern fitting methods are powerful enough to fit experimental data to the non-linear expression directly through non-linear regression. Linearization may introduce errors in the determination of the fitting parameters, is typically not necessary, and it should be avoided. Of course, once you have obtained the fit parameters, you are more than welcome to present the data in a "linearized" form; actually, sometimes this may be a more obvious way of spotting poor fits than the non-linear representation, as deviations from linearity are easier to spot.

Having said that, I would first use NonlinearModelFit on your dataset data with an exponential model function ($ a e^{b x} $) to obtain the best fit parameters:

 expmodel = NonlinearModelFit[ data, a E^(b x), {a, b}, x, MaxIterations -> 200]

 (* 9.8504*10^-7 E^(12.0768 x) *)

You can then plot the fitting function expmodel using Plot, and add your experimental points in an Epilog to your plot as follows:

 Plot[
  expmodel[x], {x, Min[data[[All, 1]]], Max[data[[All, 1]]]},
  PlotStyle -> Red, PlotRange -> Full, AxesOrigin -> {0.815, 0},
  AxesLabel -> {"Voltage", "Current"},
  Epilog -> {PointSize[0.015], Point[data]}
 ]

Simple exponential fit

If you want a logarithmic plot of your data and fitting function, we can use the same strategy with LogPlot.

 LogPlot[
  expmodel[x], {x, Min[data[[All, 1]]], Max[data[[All, 1]]]},
  PlotStyle -> Red, PlotRange -> Full, AxesOrigin -> {0.815, 0.009}, 
  AxesLabel -> {"Voltage", "Current"},
  Epilog -> {PointSize[0.015], Point[data /. {x_, y_} -> {x, Log[y]}]}
 ]

Log plot of the exponential fit

In this case, however, we had to transform your experimental data before plotting, by calculating the logarithm of its y values. I did this by applying a replacement rule on the data:

 data /. {x_, y_} -> {x, Log[y]}

In plain language, the pattern looks inside data for lists of two elements. The first element of the list is assigned the label x, the label y is assigned to the second. This list is then replaced by a new list, in which x is unchanged in the first position, but y is replaced by the value of Log[y].

As a last caveat, you will want to evaluate whether the fit you obtained is "good enough" for your purposes. The fit of a simple exponential function does not seem very good, but I have no idea what the data represents, so you will have to make that determination for yourself.

$\endgroup$
-1
$\begingroup$

If you want a linear fit (for the log of current predicted by voltage) for part of the data and a log fit (for the log of current predicted by the log of voltage) for the rest of the data, you might consider a piecewise fit where one also estimates the join point. An example is already available at this site:

fitting piecewise functions

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.