What is the reason for getting multiple lines at v=-1.5 and v=1.5 for t=0:0.1:10*pi in my code

1 view (last 30 days)
Hi all,
At t=0:0.1:10*pi,
I am getting multiple lines at v=1.5 and v=-1.5 when I tried to plot for
voltage versus current when I run my code.
But If I increase the sampling interval, multiple lines occuring at v=1.5 and v=-1.5 are eliminated.
For eg,
For t=0:0.00001:10*pi
I want to reason behind the occurence of multiple lines for t=0:0.1:10*pi.
Can anyone help me.
Thanks.
I attached my code below,
clc;
clear all;
close all;
w=1;
M1=2e6;
M2=2e9;
t=0:0.1:10*pi;
M = M2;
for i=1:length(t)
v(i)=2*sin(w*t(i));
if (v(i)>=1.5)
M = M1;
elseif (v(i)<=-1.5)
M = M2;
end
I(i)= v(i)/M;
end
figure(1)
plot(t, v,'r');
hold on;
plot(t, I*1000000,'b');
legend('voltage','current','Location','northeast');
title('Voltage & Current vs time')
grid on;
figure(2)
plot(v, I);
title('Voltage vs Current');
xlabel('Voltage');
ylabel('Current');

Accepted Answer

the cyclist
the cyclist on 21 Jun 2015
The reason is that your values of v are not sorted. The values of v go back and forth from high ones to low ones, and MATLAB is drawing the lines between each consecutive point in the array (but these are not contiguous on your plot).
Try sorting v (and keeping I sorted along with it), for example like this:
[vSorted,sortIndex] = sort(v);
figure(2)
plot(vSorted, I(sortIndex));
title('Voltage vs Current');
xlabel('Voltage');
ylabel('Current');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!