필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

can not plot the graph and there is samething wrong on my script, can anyone checkit

조회 수: 1 (최근 30일)
clear all
clc
t = [-5:50]
for n =1:length(t)
if (n>=-5&&n<=0);
y1=zeros;
elseif (n>=0&&n<=8) ;
y2=10*n.^2-5*n;
elseif (n>=8&&n<=16);
y3=624-5*n;
elseif (n>=16&&n<=26);
y4=36*n+12*(n-16)^2;
else (n>=26&&n<=50);
y5=2136*exp(-0.1*(n-26));
end
end
figure(1)
plot(n,y1,n,y2,n,y3,n,y4,n,y5)
xlabel('time');ylabel('v(t)')

답변 (2개)

Rik
Rik 2019년 3월 9일
Your code doesn't do what you think it does. You could see that by looking at your variables: they have become scalars, instead of vectors.
What you should do is take advantage of the array processing you can do with Matlab (with logical indexing), or use indexing in your loop.
t = [-5:50]
y = NaN(size(t))
L= t>=-5 & t<=0;
y(L)=0;
L=t>=0 & n<=8;
y(L)=10*t(L).^2-5*t(L);
L=t>=8 & t<=16;
y(L)=624-5*t(L);
L=t>=16 & t<=26;
y(L)=36*t(L)+12*(t(L)-16)^2;
L=t>=26 & t<=50;
y(L)=2136*exp(-0.1*(t(L)-26));
figure(1)
plot(t,y)
xlabel('time');ylabel('v(t)')
  댓글 수: 3
arham zakki edelo
arham zakki edelo 2019년 3월 9일
and if i want to make 'for if' function to my problems , can you help me to solve that base on my problem
Star Strider
Star Strider 2019년 3월 9일
@arham zakki edelo — Rik’s code uses ‘logical indexing’, the most efficient way to do this problem.
If you want to continue with your for loop and if block, try this:
t = -5:50;
for n =1:length(t)
if (t(n)>=-5&t(n)<=0);
y1(n)=zeros;
t1(n) = t(n);
elseif (t(n)>=0&t(n)<=8);
y2(n)=10*n.^2-5*t(n);
t2(n) = t(n);
elseif (t(n)>=8&t(n)<=16);
y3(n)=624-5*t(n);
t3(n) = t(n);
elseif (t(n)>=16&t(n)<=26);
y4(n)=36*n+12*(t(n)-16)^2;
t4(n) = t(n);
else (t(n)>=26&t(n)<=50);
y5(n)=2136*exp(-0.1*(t(n)-26));
t5(n) = t(n);
end
end
figure(1)
plot(t1,y1, t2(t2>0),y2(t2>0), t3(t3>0),y3(t3>0), t4(t4>0),y4(t4>0), t5(t5>0),y5(t5>0))
xlabel('time')
ylabel('v(t)')
Then go back and un-accept the Comment that you accepted as an Answer, and accept Rik’s Answer that actually provided you with working code.
Experiment to get the result you want.

Praveen Kumar Pakkirisamy
Praveen Kumar Pakkirisamy 2019년 3월 9일
What was your question and what are you trying to do? It is not quite clear from your Code
  댓글 수: 2
arham zakki edelo
arham zakki edelo 2019년 3월 9일
i want to make a plotting picewise graph from these function 1.png which is the interval time is -5 to 50 second, please help me
Rik
Rik 2019년 3월 9일
This is not an answer, but a comment. Please delete this and repost as a comment.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by