0

I'm quite new to Mathematica. I have an issue defining cumulatively a function. Let's say I have a waveform f[t] , and I want to create a function with many copies of it at different times. I have the list of arrival times in a vector v. I define my function T as follow

T[t_]:=0
For[i=1,i<=Length[v],i++,T[t]+=f[t-v[[i]]]]

Now, if I copy/paste the output of the last row and define a new function, I can plot it correctly, but if I call directly

Plot[T[t],{t,Min[v],Max[v]}]

My plot is empty. How's that?

  • Regular Plot plots something continuous. You want ListPlot. And also, try to avoid For loops in Mathematica. – Marius Ladegård Meyer Jan 18 '16 at 14:36
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) 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! – Michael E2 Jan 18 '16 at 14:44
  • f[t] is actually continuos. So, Plot is the right one. The discrete nature of the signal lies in the starting times of every copy of the continuous waveform I want to add to my signal. To be more specific, let say I want to add many copy of a Sin[t] translated in time by some given amounts, and then plot the result. Which is the best way to obtain it? Any suggestion on how to avoid the For loop? – Riccardo Buscicchio Jan 18 '16 at 14:46
  • Your plot is not empty, the line is superimposed to the Axes try Plot[T[t], {t, 0, 1}, AxesOrigin -> {0, -1}]. You will see that T is defines as monotonically zero. Your For statement doesn't make much sense. I'm voting to close the question arises from a syntax mistake and is unlikely to help any future visitors – rhermans Jan 18 '16 at 22:35
  • Your question may be put on-hold as it seems to be off-topic, i.e it arises from a simple mistake Don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. – rhermans Jan 18 '16 at 22:36

1 Answers1

1

There are several ways to do what is asked. Below I am going to use Table since that is close to the use of the For loop in the question.

First we define the function f and vector v:

f[t_] := Sin[t]
v = Range[1, 6];

Next we define a function that is the sum of f over the offsets given in v:

T[t_] := Evaluate[Total[Table[f[t - i], {i, v}]]]

This is the function T:

In[24]:= T[t]

Out[24]= -Sin[1 - t] - Sin[2 - t] - Sin[3 - t] - Sin[4 - t] - 
 Sin[5 - t] - Sin[6 - t]

And here is the plot of f and T:

Plot[{f[t], T[t]}, {t, Min[v], Max[v]}, PlotLegends -> "Expressions"]

enter image description here

We can define T to use the function f and the vector of offsets v as arguments.

T[t_, f_, v_] := Total[Table[f[t - i], {i, v}]];

Here is a plot with this new definition of T:

Plot[{f[t], T[t, f, Range[1, 3]], T[t, f, Range[4, 13]]}, {t, Min[v], 
  Max[v]}, PlotLegends -> "Expressions"]

enter image description here

Using Evaluation in the first definition of T is not necessary. Look at Definition[T] to evaluate the differences.

Anton Antonov
  • 37,787
  • 3
  • 100
  • 178