フィルターのクリア

Generating a single pulse in time?

10 ビュー (過去 30 日間)
AA
AA 2013 年 11 月 28 日
コメント済み: Talha Khalid 2020 年 11 月 9 日
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.

採用された回答

David Sanchez
David Sanchez 2013 年 11 月 28 日
Your tspan is
tspan=[0,1000];
it should be:
tspan=0:1000;
You will get this plot:
  2 件のコメント
AA
AA 2013 年 11 月 28 日
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 2013 年 11 月 28 日
I meant the initial and endpoints of integration time.

サインインしてコメントする。

その他の回答 (5 件)

Wayne King
Wayne King 2013 年 11 月 28 日
編集済み: Wayne King 2013 年 11 月 28 日
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 件のコメント
AA
AA 2013 年 11 月 28 日
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 2020 年 11 月 9 日
@Wayne King
I just tested your answer. It doesn't generate the required result. See the attached picture

サインインしてコメントする。


David Sanchez
David Sanchez 2013 年 11 月 28 日
It is easier to do:
t = 0:1000;
s = zeros(length(t));
s(t>400 & t<500) = 2;
plot(t,s)
  1 件のコメント
AA
AA 2013 年 11 月 28 日
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.

サインインしてコメントする。


AA
AA 2013 年 11 月 28 日
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 2013 年 11 月 28 日
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 2013 年 11 月 28 日
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!?

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by