Plot(x,y2) with if condition y1(x)

3 ビュー (過去 30 日間)
Kacper Wybranski
Kacper Wybranski 2020 年 3 月 26 日
コメント済み: Kacper Wybranski 2020 年 3 月 26 日
Hello i've got stuck with some code.
What i want to get is this shape of two functions:
I know y function and how to draw it but i've got problem with y^2 function and its domain.
Here's my code
clc
clear all
close all
dt=0.01;
t=[0:dt:4*pi];
w=1;
A=2;
y1=A.*cos(t).*(sin(2.*w.*t)).^2;
y2=y1.^2
plot(t,y1,'r')
hold on
xlabel('time')
ylabel('Amplitude')
grid on
if y1>0
plot(t,y2,'og')
end
Sadly, i get only visual of y1 not y2 :(
What should I do using if statement or some other loops?
  2 件のコメント
Adam Danz
Adam Danz 2020 年 3 月 26 日
編集済み: Adam Danz 2020 年 3 月 26 日
"I know y function and how to draw it but i've got problem with y^2 function"
What's the problem and what's the goal? Is the green line supposed to be connected with peaks in between?
Kacper Wybranski
Kacper Wybranski 2020 年 3 月 26 日
The y^2 function was supposed to appear only when y function has values over 0, the answer from Fangjun Jiang helped.

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

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 3 月 26 日
You can check the output of y1>0. It's a vector so shouldn't be used in if statement.
what you need for y2 is
index=y1>0
plot(t(index),y2(index),'og')

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by