Plot a rectangular Pulse from -1 to 1

What can be the generalized code for this? I need a generalized code where the output asks me to enter the time axis and range and amplitude
t1=-2:0.01:-1; %time axis
t2=-1:0.01:1; %time axis
t3=1:0.01:2; %time axis
t=[t1 t2 t3];
x1=zeros(size(t1)); %plotting
x2=ones(size(t2));
x3=zeros(size(t3));
x=[x1 x2 x3];
plot(t,x);

댓글 수: 4

SHIVAM KUMAR
SHIVAM KUMAR 2020년 12월 11일
t=input(); %t=[t1(start,end) t2(start,end) t3(start,end)]; eg:[-2 -1 -1 1 1 2]
number=input(); %enter number of points in interval t1,t2,t3
amplitude=input();
x1=zeros(number); %plotting
x2=amplitude*ones(number);
x3=zeros(number);
x=[x1 x2 x3];
tim=[linspace(t1(1),t1(2),number) linspace(t1(3),t1(4),number) linspace(t1(5),t1(6),number) ];
plot(tim,x);
Pratik A
Pratik A 2020년 12월 11일
This is what the error I'm getting after using your code. Kindly Help me.
Image Analyst
Image Analyst 2020년 12월 12일
input() takes a string, not a bunch of numerical variables with no multiplication sign between them. Again, a string, not numbers.
SHIVAM KUMAR
SHIVAM KUMAR 2020년 12월 12일
Ok. I hoped of some conversion from string to array but realized it doesn't work so changed the code to do it myself.

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

답변 (2개)

Mohamad
Mohamad 2020년 12월 12일

1 개 추천

This just an example , you have to repeat according to your code requirements :
prompt = 'Enter the vector t1 ';
t1 = input(prompt);
SHIVAM KUMAR
SHIVAM KUMAR 2020년 12월 12일
편집: SHIVAM KUMAR 2020년 12월 12일

0 개 추천

% the syntax for taking input t eg:[-2 -1 1 2] =>t1=[-2,-1 ], t2=[-1 1], t3=[1 2].
%I was sleepy earlier so didn't run the code.
%code , don't edit anything as its already running well tested myself.
t=input("Enter the limit for axis : ",'s');
t1=str2num(t); %To get the array for that input string
number=input("enter number of points in interval t1,t2,t3 : "); %enter number of points in interval t1,t2,t3
amplitude=input("Enter the amplitude of square wave : ");
x1=zeros(number); %plotting
x2=amplitude*ones(number);
x3=zeros(number);
x=[x1 x2 x3];
tim=[linspace(t1(1),t1(2),number) linspace(t1(2),t1(3),number) linspace(t1(3),t1(4),number) ]; %The time axis
plot(tim,x);
title("Rectangulat plot");
xlabel("time");
ylabel("Amplitude");

카테고리

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

질문:

2020년 12월 11일

댓글:

2020년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by