Translated by このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

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

arham zakki edelo

arham zakki edelo (view profile)

さんによって質問されました 2019 年 3 月 9 日

Star Strider (view profile)

さんによって コメントされました 2019 年 3 月 9 日
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)')

0 件のコメント

サインイン to comment.

2 件の回答

Rik (view profile)

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 (view profile)

2019 年 3 月 9 日
thanks , can you explain to me about the NaN function
arham zakki edelo

arham zakki edelo (view profile)

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 (view profile)

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.

サインイン to comment.

Praveen Kumar Pakkirisamy (view profile)

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 (view profile)

2019 年 3 月 9 日
i want to make a plotting picewise graph from these function which is the interval time is -5 to 50 second, please help me
Rik

Rik (view profile)

2019 年 3 月 9 日
This is not an answer, but a comment. Please delete this and repost as a comment.

サインイン to comment.