Make Function Equal Zero After Certain Time

Hi all, I am in need of some help. I am trying to use if and else statements to plot the following function. Here is my current code:
HP = 60/72;
Tas = 0.105;
Elamin = 0.12;
Elamax = 0.28;
Eramin = 0.05;
Eramax = 0.15;
t = [0:0.001:HP];
if t > Tas
aa = 0;
else
aa = sin((pi*t)/Tas);
end
ela = Elamin + (Elamax - Elamin)*aa;
era = Eramin + (Eramax - Eramin)*aa;
plot(t,ela, t,era, t,aa);
Basically, I am trying to say that for 0<t<Tas, the function aa is calculated. Otherwise, the function aa = 0. Attached is the current plot I am getting if I do not include the if/else statements. Its kind of what I want, except the graph must level to around y = 0 after x = Tas (about 0.105s).
Any help is appreciated!

 채택된 답변

Star Strider
Star Strider 2018년 4월 4일

0 개 추천

Try this:
aa = @(t, Tas) sin((pi*t)/Tas) .* ((0 <= t) & (t <= Tas)); % Function, With Conditions As A Logcal Index Vector
t = linspace(-1, 1, 150); % Time Vector
Tas = 0.105;
figure
plot(t, aa(t, Tas))
grid

댓글 수: 6

s.v.
s.v. 2018년 4월 4일
That is more of what I am looking for, but writing aa in that form doesn't allow me to plot ela or era anymore. I get the error:
"Undefined operator '*' for input arguments of type 'function_handle'."
What else must I change?
Thanks!
I forgot to include ‘ela’ and ‘era’.
Try this:
aa_fcn = @(t, Tas) sin((pi*t)/Tas) .* ((0 <= t) & (t <= Tas)); % Function, With Conditions As A Logcal Index Vector
HP = 60/72;
t = linspace(-1, HP, 150); % Time Vector
Tas = 0.105;
aa = aa_fcn(t, Tas);
Elamin = 0.12;
Elamax = 0.28;
Eramin = 0.05;
Eramax = 0.15;
ela = Elamin + (Elamax - Elamin)*aa;
era = Eramin + (Eramax - Eramin)*aa;
figure
plot(t,ela, t,era, t,aa);
grid
legend('ela', 'era', 'aa')
s.v.
s.v. 2018년 4월 4일
That worked. If you'd like to help further -- I'd like to have the same waveform shown in the attached figure happen over 6 seconds. So basically, the attached figure multiple times on 1 graph (graph starts every 0.8 sec).
An image would be easier to work with than a .fig file that requires us to download it and then view it before again deleting the file. It’s been a long day and I choose not to do that.
Try this:
aa_fcn = @(t, Tas) sin((pi*t)/Tas) .* ((0 <= t) & (t <= Tas)); % Function, With Conditions As A Logcal Index Vector
HP = 60/72;
t = linspace(-1, HP, 150); % Time Vector
Tas = 0.105;
aa = aa_fcn(t, Tas);
Elamin = 0.12;
Elamax = 0.28;
Eramin = 0.05;
Eramax = 0.15;
ela = Elamin + (Elamax - Elamin)*aa;
era = Eramin + (Eramax - Eramin)*aa;
figure
plot(t,ela, t,era, t,aa);
grid
legend('ela', 'era', 'aa')
N = 7; % Number Of Repititions
t_ext = linspace(0, HP*N, numel(t)*N); % Extend ‘t’ For ‘N’ Repititions
ela_ext = repmat(ela, 1, N); % Extend ‘ela’ For ‘N’ Repititions
era_ext = repmat(era, 1, N); % Extend ‘era’ For ‘N’ Repititions
aa_ext = repmat(aa, 1, N); % Extend ‘aa’ For ‘N’ Repititions
figure
plot(t_ext,ela_ext, t_ext,era_ext, t_ext,aa_ext);
grid
legend('ela', 'era', 'aa')
The ‘N’ variable will scale the previously-defined vectors to produce the desired number of repetitions.
Experiment to get the result you want.
s.v.
s.v. 2018년 4월 4일
Thank you.
As always, my pleasure.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2018년 4월 4일

댓글:

2018년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by