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

Jan
Jan 2021년 12월 25일
It is impossible to write any useful code in Matlab without using built-in functions. So please explain the problem with more details: what is allowed to use and what is forbidden?
As usual for homework questions: Show, what you have tried so far and ask a specific question.
RandomChikiBum
RandomChikiBum 2021년 12월 26일
편집: RandomChikiBum 2021년 12월 26일
Jan, I have edited the post as asked, Can you help me now?
Thanks!
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일

1 개 추천

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일
I am sorry I forgot to mention we should also not use "abs" function
If I am not wrong both our methods are the same right, Like logic behind the code? ,Do you have any other method in mind?
Accept logical operation like the following?
y = 1 - ((x<0).*-x/5 + (x>0).*x/5);
RandomChikiBum
RandomChikiBum 2021년 12월 26일
yes! Thank you!
(Tho I need to learn what they mean haha)
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개)

카테고리

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

제품

릴리스

R2020b

질문:

2021년 12월 25일

댓글:

2021년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by