Newbie plot

조회 수: 13 (최근 30일)
Diego Aguilera
Diego Aguilera 2011년 11월 26일
Our teacher has assigned us a project to plot a function through a diode. In order words, the plot should only include positive numbers. I am not sure why my code isn't working. I'm pretty new to Matlab. Thanks.
%Plotting numbers greater than 0 for given expression
clc
in = .1
t = [0:in:10];
size(t);
max(size(t))
VL = 3.*exp(-t/3).*sin(pi*t)
while(i < max(size(t)))
if(VL < 0)
VL(i) = 0;
end
i = i+1;
end
plot(t,VL)

답변 (3개)

Image Analyst
Image Analyst 2011년 11월 26일
Since a diode doesn't let current go in the reverse direction I guess you want to clip your signal to zero when it goes negative. To do that you'd do this:
VL(VL<0) = 0;
Put it right before your call to plot(). VL<0 gives you a logical array where it's 1 (true) where the values are negative. Then it's being used as a logical index to set only those indices to zero and letting the others (where VL >= 0) remain unchanged.
  댓글 수: 3
Daniel Shub
Daniel Shub 2011년 11월 26일
I saw it (and have now voted for it). I just wanted to provide a little more walking through. This extra hand holding would have been inappropriate when you wrote your answer (since Diego hadn't really shown what he had tried).
Image Analyst
Image Analyst 2011년 11월 26일
Thanks. Yeah I had to guess at what he meant since when I replied he hadn't posted any code yet.

댓글을 달려면 로그인하십시오.


Diego Aguilera
Diego Aguilera 2011년 11월 26일
I was up late working on this. I had to break it down part by part. Read one number at a time, then the if statement, and finally calling each number with a while statement. Thanks anyways.
%Plotting numbers greater than 0 for given expression
clc
in = .001;
t = [0:in:10];
max(size(t));
VL = 3.*exp(-t/3).*sin(pi*t);
%VL(13)
i = 13;
while(i < max(size(t)))
if(VL(i) < 0)
VL(i) = 0;
end
i = i+1;
end
%VL(13)
plot(t,VL);
  댓글 수: 1
Image Analyst
Image Analyst 2011년 11월 26일
Instead of staying up late working on it, you should have just looked for a response to your question here. If you had followed my advice I would have saved you, and Daniel, a lot of time.

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2011년 11월 26일
The neat thing about MATALB is you can often replace loops and treat signals as a vector. What your loop is doing is finding all the values of VL that are less than 0. You can write this as:
inegative = VL < 0;
This makes inegative an array the same size as VL with values of 1 when VL is negative and 0 otherwise. You can then set all the values of VL in one go ...
clippedVL = VL;
clippedVL(inegative) = 0;
Putting it all together you can write ...
t = 0:0.1:10;
VL = 3.*exp(-t/3).*sin(pi*t)
VL(VL < 0) = 0;
plot(t, VL);
This should give the same answer as your code above. The differences in speed will be small. Some people would say your answer is easier to understand, others would say mine. The real benefit of my answer is it moves you down the road to thinking about vectors. This will allow you to really benefit from MATLAB.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by