Message Boards Message Boards

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

How can I plot a system's response depending on certain condition

Posted 11 months ago

Hi All,
I am working on a system that would generate output with a particular type of square wave as input as long as an input condition (coming from a subsystem) is above a certain threshold. I tried using If statement but it seems not working. Could you please help? Here is my code. Effectively, what I am looking for is, when the output response ('coupling') of the system ('TF1') with the square wave defined as 'inwave' as the input is >= 0.015, the system output ('v1') should be the 'inwave' itself. It Otherwise it should be 0. It is working fine up to the plotting the 'coupling' signal. But the problem is with the If statement.
Thanks for your kind help.

 TF1 = TransferFunctionModel[
   Unevaluated[{{(697052. + 25694.6 s)/(
     700128. + 25835.5 s + 1. s^2)}}], s, SamplingPeriod ->None, 
   SystemsModelLabels -> None];
PulseWave[duty_, t_, period_, phase_] := 
  UnitBox[Mod[(t/period) + phase, 1]/(2 duty)];
inwave = 110*10^-3 PulseWave[0.25, t, 10^-2, 0] - 70*10^-3;
coupling = OutputResponse[TF1, inwave, {t, 0, 10 10^-2}];
Plot[coupling, {t, 0, 10 10^-2}]
v1 = If[coupling >= 0.015, inwave, 0];
Plot[v1, {t, 0, 10 10^-2}, PlotRange -> All]
4 Replies

Your coupling is a list, you cannot compare it to a number. Try this:

v1 = If[coupling[[1]] >= 0.015, inwave, 0]
POSTED BY: Gianluca Gorni

Dear Gianluca, Thank you so much. As such it worked in a reverse way, i.e., I am getting the response only when I set coupling <= 0.015, although I can see that coupling for certain times is >=0.015. Not sure why this happens. For coupling >=0.015 it always gives flat line at 0. Any help will be much appreciated. Here is the code again with your suggested modification.

PulseWave[duty_, t_, period_, phase_] := 
  UnitBox[Mod[(t/period) + phase, 1]/(2 duty)];
TF1 = TransferFunctionModel[
   Unevaluated[{{(697052. + 25694.6 s)/(
     700128. + 25835.5 s + 1. s^2)}}], s, SamplingPeriod ->None, 
   SystemsModelLabels -> None];
inwave = 
  OutputResponse[TF1, 
   40*10^-3 PulseWave[0.25, t, 10^-3, 0], {t, 0, 4 10^-3}];
Plot[inwave, {t, 0, 4 10^-3}];
coupling = OutputResponse[TF1, inwave, {t, 0, 4 10^-3}];
Plot[coupling, {t, 0, 4 10^-3}, PlotRange -> All]
v1 = If[coupling[[1]][[1]] >= 0.015, inwave, 0];
Plot[v1, {t, 0, 4 10^-3}, PlotRange -> All]

Sorry, I don't know the meaning of your plots, but here a reworking that runs:

Clear[TF1, inwave, coupling, v1];
PulseWave[duty_, t_, period_, phase_] := 
  UnitBox[Mod[(t/period) + phase, 1]/(2 duty)];
TF1 = TransferFunctionModel[{{(697052. + 25694.6 s)/(700128. + 
        25835.5 s + 1. s^2)}}, s, SamplingPeriod -> None, 
   SystemsModelLabels -> None];
inwave[t_] = 
  OutputResponse[TF1, 
    40*10^-3 PulseWave[0.25, t, 10^-3, 0], {t, 0, 4 10^-3}][[1]];
Plot[inwave[t], {t, 0, 4 10^-3}, PlotLegends -> {"inwave"}]
coupling[t_] = OutputResponse[TF1, inwave[t], {t, 0, 4 10^-3}][[1]];
Plot[{coupling[t], inwave[t]}, {t, 0, 4 10^-3}, PlotRange -> All, 
 GridLines -> {{}, {.015}}, PlotLegends -> {"coupling", "inwave"}]
v1[t_] = If[coupling[t] >= 0.02, inwave[t], 0];
Plot[v1[t], {t, 0, 4 10^-3}, PlotRange -> {-.01, .045}, 
 GridLines -> {{}, {.015}}, PlotLegends -> {"v1"}]
POSTED BY: Gianluca Gorni

Thank you so very much! It works perfectly. I am trying to model a biological system of action potential propagation and this is related to that work. Thanks a lot for your kind help.

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