Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why are my lengths not equal?

Subject: Why are my lengths not equal?

From: David

Date: 26 Sep, 2013 23:28:07

Message: 1 of 2

I am trying to develop a plot of four approximations and compare them to each other on the same plot using delta t = 4,2,1,0.5. I am just doing one for dt = 1 to write and then will add the others in once I got it working.
When I run my code, I get an error when it goes to plot that says the length of (t) and the length of (Y) are not equal. I am really not sure how to fix it.

dt = [ 1 ];
tend = 10;
t = 1:dt:tend;
g = 9.8;
Y(1) = 0;
Vy(1) = 100;
for i = 1:length(dt)
    for n = 1:length ((t)-1)

    Y(n+1) = Vy(n) * dt + Y(n)
    Vy(n+1) = Vy(n) - g * dt
    
    end
end
length(Y)
length(t)
plot(t,Y)


I put length in there so I could see exactly how long they were and Y is always 1 more element longer than t no matter what values I use

Subject: Why are my lengths not equal?

From: Nasser M. Abbasi

Date: 26 Sep, 2013 23:57:44

Message: 2 of 2

On 9/26/2013 6:28 PM, David wrote:
>
>t = 1:dt:tend;
..
> for n = 1:length ((t)-1)

may be you need

     1:length(t)-1

length((t)-1) will always be length(t), since all what it does
is subtract one from element in vector t, and then return the
length of the result, which will be the same as length(t).

--Nasser
  

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us