I have a list of data in this form {{x1,y1},{x2,y2},...,{xn,yn}}, which can be plotted simply with

test = Get["~\\test.dat"];
ListLinePlot[test, PlotRange -> {{0, 10}, {0.35, 0.85}}, Frame -> True]

The list can be considered a function $y=y(x)$. Now, I want to plot a curve of $f(x)=y(x)+x\frac{dy}{dx}$.

Because there exists a derivation, I first tried Interpolation to make a function y.

dep = test[[All, 2]]; indep = test[[All, 1]];

y = Interpolation[dep,indep];

Then I construct the function and plot it, but it does not work...

f = dep + indep*D[y,indep]

Plot[f, {indep, 0, 10}, Frame -> True]

My problem should be simple, but I cannot figure out how to fix it. I think the issue may be the Interpolation step because I even cannot plot the function itself using the interpolation.

Plot[y, {indep, 0, 10}, Frame -> True]

1 Answer 1

data = First@ReadList["C:/test.dat"];
minmax = MinMax[data];
y = Interpolation[data];

p1 = ListPlot[data, PlotRange -> {-0.5, 1.0}];
p2 = Plot[y[x], {x, Splice@minmax}, PlotStyle -> Blue, 
   PlotRange -> {-0.5, 1.0}];
p3 = Plot[y'[x], {x, Splice@minmax}, PlotStyle -> Darker@Green, 
   PlotRange -> {-0.5, 1.0}];
p4 = Plot[y[x] + x y'[x], {x, Splice@minmax}, PlotStyle -> Red, 
  PlotRange -> {-0.5, 1.0}]

Show[p1, p2, p3, p4, GridLines -> Automatic]


If you cannot use Splice due to an older version being in use, then use Sequence @@ as shown below:

p4 = Plot[y[x] + x y'[x], {x, Sequence @@ minmax}, PlotStyle -> Red, 
   PlotRange -> {-0.5, 1.0}];

To find the minimum point:

FindMinimum[y[x] + x y'[x], x]

{0.319033, {x -> 1.1225}}


enter image description here

  • $\begingroup$ Thanks @Syed, your method basically works, but Splice function is removed after v.10. Could you modify the answer with another function that is available in later versions? Btw, may I ask a further question: is there a method to show also the min. value point of p4 plot by finding where its derivation is zero? I tried NSolve[2*y'[x]+x y''[x]==0,x] but does not work. Thank you! $\endgroup$
    – user95273
    Sep 3, 2022 at 6:32
  • $\begingroup$ I have updated my answer. $\endgroup$
    – Syed
    Sep 3, 2022 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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