Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
can not plot the graph and there is samething wrong on my script, can anyone checkit
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Rik
il 9 Mar 2019
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 Commenti
Star Strider
il 9 Mar 2019
@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
il 9 Mar 2019
What was your question and what are you trying to do? It is not quite clear from your Code
2 Commenti
Questa domanda è chiusa.
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!