1

I wanted to plot the load voltage across the resistor in series with a diode using matlab. This is a rather simple example involving piecewise functions, however I ran into an unexpected error.

t=[0:0.001:10]
vs=4*sin(pi * t)
for i =1:length(vs)
    if(vs(i)<=0.7)
        v(i)=0;
    else
        v(i)=vs(i)-0.7;
    end
end
plot(t,v)

t is the time, vs is the source voltage, and v is the load voltage. Now, running this gave me an error saying "error, t and v are of different sizes..". Using length() I found out that while t and vs are of lengths 10001, v is somehow of length 1000001.

This is truly baffling to me. How can v and vs possibly differ in size? every element of vs was mapped to an element of v, and yet the size of v comes out to be about 100 times the size of vs.

Being new to matlab, I still am not very comfortable with not explicitly declaring the array v before using it in the for loop. However, I went through with it, because the example I worked on prior to this, used the same thing and it worked without any problems. We simply had a plot a piecewise function:

x=[-2 : 0.00001 : 20];
for i=1: length(x)
    if(x(i)>=-2 && x(i)<0)
        y(i)=sqrt(x(i)^2+1);
    else if(x(i)>=0 && x(i)<10)
            y(i)=3*x(i)+1;
        else
            y(i)=9*sin(5*x(i)-50);
        end
    end
end
plot(x,y)

This code worked flawlessly, and in my opinion the initial code is fundamentally doing the same thing, so again, I'm clueless as to why it failed.

The original code works if you initialise v to be of the same size as t (and therefore, vs), but still, I want to know why the code involving x,y worked (where y wasn't initialised) and the code involving (t,v) failed.

Also, my friend copy pasted the entire code into the command window of matlab 2016, and it worked. (and I'm using the 2021 version).

4
  • You might have had a variable v defined prior to running this code. It is always best to explicitly declare all your variables. In fact, the MATLAB editor should warn you about increasing the size of v in the loop. The recommendation is to preallocate. Commented Aug 23, 2021 at 18:14
  • I can assure you there wasn't any variable v defined prior to this code. Commented Aug 23, 2021 at 18:15
  • The other option is that the function length is overloaded. Maybe you had a variable called length or a function called length defined. If not, then the problem is a pre-existing variable v. Commented Aug 23, 2021 at 18:27
  • Also, your comments "The original code works if you initialise v to be of the same size as t" and "my friend copy pasted the entire code [...] and it worked" indicate that the problem is not the code, but the status of the workspace where you run the code. Initialize your variables, and write code as functions rather than batch scripts. Commented Aug 23, 2021 at 18:45

1 Answer 1

1

Its good practice to initialize variables before entering a loop. It will help avoid undefinied behaviour when you run the script multiple times. If you run the script with different lengths for t, it would fail the second run. One solution would be:

t=0:0.001:10;
vs=4*sin(pi * t);
v=nan(size(t));
for i =1:length(vs)
    if(vs(i)<=0.7)
        v(i)=0;
    else
        v(i)=vs(i)-0.7;
    end
end
figure;
plot(t,v);

You could also avoid the for loop and use matrix operations instead:

t=0:0.001:10;
vs=4*sin(pi * t);
v=vs-0.7;
v(vs<=0.7)=0;
figure;
plot(t,v);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.