How to remove this error??

1 view (last 30 days)
Nidhi
Nidhi on 18 Aug 2013
Here is my code
function [] = plot_rho()
h= zeros(80,1);
h(1,1)=1000;
density = zeros(80,1);
k=1;
while k<=80;
density(k,1) = rho(h(k,1));
h(k+1,1)= h(k,1)-10;
k=k+1;
end
plot(density,h);
grid on;
getting an error like this but I have defined density and h of same length 80 so why this error
Error using ==> plot
Vectors must be the same lengths.
Error in ==> plot_rho at 11
plot(density,h);

Answers (2)

kjetil87
kjetil87 on 18 Aug 2013
You are indexing h as
h(k+1,1)
That way when k=80 you are assigning a new value to
h(81)=h(80,1)-10;
And thus the size of h grows to 81.

Image Analyst
Image Analyst on 18 Aug 2013
h is 81 long while density is still only 80 long. This could be easily figured out if you know how to debug programs. See this: http://blogs.mathworks.com/videos/category/gui-or-guide/ Then you will also see that your code won't even run because you didn't define rho. You never would have gotten that error with the code that you posted. So that's how I know that you are not posting the actual code you are running.

Tags

Community Treasure Hunt

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

Start Hunting!