Plot a function for x<0 and x >0

20 ビュー (過去 30 日間)
RandomChikiBum
RandomChikiBum 2021 年 12 月 25 日
コメント済み: Simon Chan 2021 年 12 月 26 日
The assigned task is to make a triangular function with peak amplitude of 1 over -5 to +5 without any inbuilt functions.
My approach was to use the respective line equation for x>0 and x<0, but I didn't kow how to do it.
Any other approach to this problem is also appreciated.
Edit:- for some clarity, we are not supposed to use functoins like "Saw-tooth" in this case. while functions like ones,zeroes,plot are fine
The following is my code for the approach I have mentioned above, Any othe approaches are also welcomed
And does anyone know why the title,xlabel,ylabel are not being showed in the output?'
Thanks!
x = (0: 1:5);
k = (-5 :1 :0);
grid on;
g = (k/5) + 1;
y =(-x/5) + 1;
title('Task-2 Triangular Signal');
xlabel('Time');
ylabel('Amplitude');
plot(x,y,k,g);
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 26 日
When you call plot() or most other graphics calls, if you have not called
hold on
or equivalent, then the first thing plot will do is locate the axes and cla() the axes.
Move the labels and title to after the plot call.
RandomChikiBum
RandomChikiBum 2021 年 12 月 26 日
Thanks @Walter Roberson will keep this in mind the next time I code!

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

採用された回答

Simon Chan
Simon Chan 2021 年 12 月 26 日
You may modify based on the following code and you should plot first and then set the grid,title,xlabel,ylabel,etc
x = -5:5;
y = 1-abs(x/5);
plot(x,y);
grid on;
title('Task-2 Triangular Signal');
xlabel('Time');
ylabel('Amplitude');
  5 件のコメント
RandomChikiBum
RandomChikiBum 2021 年 12 月 26 日
@Simon Chan can you post another answer with the logical operators , so that I can accept the answer?
Simon Chan
Simon Chan 2021 年 12 月 26 日
You can just simply accept this answer, glad it solve your question.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by