Message Boards Message Boards

0
|
5035 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Defining a function inside a For loop

Posted 9 years ago

Hello,

I am stuck in the following code:

For[i = 0, i < 10, i++,

 signal[t_ /; 0 + i <= t <= 0.5/4 + i] = 0;

 signal[t_ /; 0.5/4 + i < t <= 1.5/4 + i] = 1;

 signal[t_ /; 1.5/4 + i < t <= 2.5/4 + i] = 0;

 signal[t_ /; 2.5/4 + i < t <= 3.5/4 + i] = -1;

 signal[t_ /; 3.5/4 + i < t <= 4/4 + i] = 0]

Basically, I want a function which is periodical over the period i. What did I do wrong with defining the function in a loop?

Thanks in advance! Chris

POSTED BY: Chris Smith
4 Replies
 signal[t_] := Piecewise[{{1, 0.5/4 < Mod[t, 1] <= 1.5/4}, {-1, 2.5/4 < Mod[t, 1] <= 3.5/4}}]

 Plot[signal[t], {t, 0, 10}, Exclusions -> None]
POSTED BY: Ilian Gachevski
signal[t_] := Floor[Mod[t + 1/2, 1]/3] - Floor[Mod[t,1]/3]

This should be a more manipulable function, and it works anywhere:

func and plot

I would have found it easier with a more detailed description of your target. A plot like the one pictured helps to clarify the result you're seeking.

POSTED BY: David Gathercole
Posted 9 years ago

Hello David, thank you for your nice solution!

My target is as follows: I want to simulate the behavior of an optical switch, therefor it is important to get the function of my waveform generator. The "weird" zero lines in-between will give me additional information whether the switch behaves the same for e.g. +10V and -10V. The way i get the information out of it, is by comparing the amplitude of the optical output to the zero line output.

POSTED BY: Chris Smith
Posted 9 years ago

Solved it already, however if someone got a better way please let me know.

My solution is as follows:

signal[t_] := 
 For[i = 0, i < 10, i++, 
  If[0 <= t <= 0.5/4 + i, Return[0], 
   If[0.5/4 + i < t <= 1.5/4 + i, Return[1],
    If[1.5/4 + i < t <= 2.5/4 + i, Return[0],
     If[2.5/4 + i < t <= 3.5/4 + i, Return[-1],
      If[3.5/4 + i < t <= 4/4 + i, Return[0]
       ]]]]]]
POSTED BY: Chris Smith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract