Filter löschen
Filter löschen

Generating a single pulse in time?

10 Ansichten (letzte 30 Tage)
AA
AA am 28 Nov. 2013
Kommentiert: Talha Khalid am 9 Nov. 2020
Hi I would like to write a simple code for a single pulse.So for example: if I would like to see pulse in the time period: 400<t<500
Why does If (t>400&&t<500) s=2.0 else s=0.0 end
When I plot this I get: a result where s=0 for all time and no pulse.
but
if (t>400&&t<500) s=2.0; else s=0.01; end
a pulse with a baseline of 0.01 (and maximum value 2.0)?
Thank you for your help.

Akzeptierte Antwort

David Sanchez
David Sanchez am 28 Nov. 2013
Your tspan is
tspan=[0,1000];
it should be:
tspan=0:1000;
You will get this plot:
  2 Kommentare
AA
AA am 28 Nov. 2013
Thanks!
This is a bit worrying though (for me)- as for both 0:1000 and [0 1000] we are specifying the endpoints in the tspan. How can the result be so different?
AA
AA am 28 Nov. 2013
I meant the initial and endpoints of integration time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Wayne King
Wayne King am 28 Nov. 2013
Bearbeitet: Wayne King am 28 Nov. 2013
You did not tell us the sampling frequency so I'll just assume it is 1 Hz in my example.
t = 0:1:1000;
s = 0.01*ones(length(t));
s(t>400 & t<500) = 2;
plot(t,s)
Note that in this case you could just do the simpler (assuming the first sample is t=0):
s = 0.01*ones(10001,1);
s(402:500) = 2;
but the example I have should work when the sampling frequency is not 1.
  2 Kommentare
AA
AA am 28 Nov. 2013
Could you please shed some light on my comment above. I would be very grateful. What is the difference between tspan=0:1000 and [0,1000] Thanks!
Talha Khalid
Talha Khalid am 9 Nov. 2020
@Wayne King
I just tested your answer. It doesn't generate the required result. See the attached picture

Melden Sie sich an, um zu kommentieren.


David Sanchez
David Sanchez am 28 Nov. 2013
It is easier to do:
t = 0:1000;
s = zeros(length(t));
s(t>400 & t<500) = 2;
plot(t,s)
  1 Kommentar
AA
AA am 28 Nov. 2013
Thank you for your answer.
I'm trying to work out why I get the results that I do get? I cannot work it out. Why does having a baseline result in a pulse?
Thanks for your help.

Melden Sie sich an, um zu kommentieren.


AA
AA am 28 Nov. 2013
Thank you for your answer.
I'm trying to work out why I get the results that I do get? I cannot work it out. Why does having a baseline result in a pulse?
Thanks for your help.

Wayne King
Wayne King am 28 Nov. 2013
See my earlier answer for how to do it.
Using your if statement approach, when t is a vector how do you expect the if statement to evaluate (t> 400 && t<500)?
To get that approach to work you'd have to loop through the t-values like this
s = zeros(1000,1);
t = 1:1000;
for nn = 1:length(t)
tval = t(nn);
if (tval > 400 && tval<500)
s(nn) = 2;
else
s(nn) = 0.01;
end
end
plot(t,s)
But that is not an efficient use of MATLAB's inherit array operations. Use the approach I gave you earlier.

AA
AA am 28 Nov. 2013
Thanks both. Heres the thing: I need a zero baseline: SO when I put in either David Sanchez or Wayne Kings' (modify 0.01 to 0 in the latter case) in my code I DO NOT get a pulse- just a flat 0 line. Here is my full code ( I know it seems really long- but its a simplified version of a larger code that I'm using and the setup needs to be preserved. I have included your suggestions):
function dy=testpulse(t,y);
N=100; dy=zeros(N,1); delta=2*pi/N;
s = zeros(length(t));
s(t>400 & t<500) = 2;
for k=1:N
dy(k)=s-y(k);
end
Calling routine: N=100; delta=2*pi/N;
y0=zeros(N,1);
for j=1:N
y0(j)=0.0;
end
x=zeros(N,1);
x=(1:N)*delta-delta;
tspan=[0,1000];
[t,y]=ode15s(@testpulse,tspan,y0);
a=y(:,1:N); mesh(x,t,a); xlabel('\Theta'),ylabel('t'), zlabel('[]'); title('X')
SO my question remains- even when using the codes that you both have suggested why don't I get a pulse when the baseline is 0?
I do know how to get a pulse without using the && (you can use the OR operator or just use a series of if and elseif commands to get it)- but this is really bugging me because it doesn't make sense to me!?

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by