Error using plot. Vectors must be the same length.

Hello,
When running my code I'm getting the following error :
Error using plot
Vectors must be the same length.
Error in A5Q2 (line 45)
plot(x1,u(1,:),'*',x1,us1,'-');
Please find below my code:
function u = diff1_ex
ngrid = 50;
dx = 1/ngrid;
D=1;
s=0.4;
dt = s*dx^2/D;
tmin=0;
tmax=1;
ntime = ceil((tmax-tmin)/dt);
u = zeros(ngrid+1,ntime);
source=1;
for l=1:ntime
for k=2:ngrid
u(k,l+1)=u(k,l)+s*(u(k+1,l)-2*u(k,l)+u(k-1,l));
if (k>ceil((0.4/dx)-1)) && (k<ceil(0.6/dx))
u(k,1+l)=u(k,1+l)+ dt*source;
end
end
end
subplot(2,1,1);
imagesc(u);
colorbar;
xlabel('x')
ylabel('y')
x1 = 0:dx:1;
us1 = -0.3*x1;
x2 = 0.4:dx:0.6;
us2 = (-x2.^2)/2+(0.1*x2)-0.08;
x3 = 0.6:dx:1;
us3 = 0.5*(1-x3);
subplot(2,1,2);
hold on
plot(x1,u(1,:),'*',x1,us1,'-');
plot(x2,u(2,:),'*',x2,us2,'-');
plot(x3,u(3,:),'*',x3,us3,'-');
hold off
xlabel('x')
ylabel('us')
title('Stationary Solution')
Could you please help me to correct my mistake.
Please find attached the questions and my solution to the first question.
Thanks in advance

5 comentarios

Use the debugger and set a breakpoint after the line
u = zeros(ngrid+1,ntime);
look at
whos u
then at the plot() line try
length(u(1,:))
Vashist Dwarka
Vashist Dwarka el 28 de Oct. de 2018
What do mean by " whos u " ?
When the debugger prompt shows up, type it in at the prompt.
See
doc whos
PS: By "at the plot line" I meant either set another breakpoint at that line and run until there or just step through until you get there (F10)
Vashist Dwarka
Vashist Dwarka el 28 de Oct. de 2018
I'm really lost with the debugging process. Could you please help me going through it ? and figuring out the mistake
dpb
dpb el 28 de Oct. de 2018
Editada: dpb el 28 de Oct. de 2018
Basically you just click on the line where you want a breakpoint in the editor and then press RUN since your function doesn't require any input arguments.
As a hint to your problem, look at how you defined the array u and then how you try to use it when you are trying to plot.
Learning to use the debugger is well worth the effort...

Iniciar sesión para comentar.

Respuestas (2)

madhan ravi
madhan ravi el 28 de Oct. de 2018
plot(x1,u(1,1:numel(x1)),'*',x1,us1,'-');
plot(x2,u(2,1:numel(x2)),'*',x2,us2,'-');
plot(x3,u(3,1:numel(x3)),'*',x3,us3,'-');

4 comentarios

madhan ravi
madhan ravi el 28 de Oct. de 2018
maybe like the above? not sure if its correct
Vashist Dwarka
Vashist Dwarka el 28 de Oct. de 2018
Does not give me the right graph
madhan ravi
madhan ravi el 28 de Oct. de 2018
ok then no idea because your x1, x2, x3 size is 1 by 51 and your u size is 51 by 6251 , how can you plot them?
Vashist Dwarka
Vashist Dwarka el 28 de Oct. de 2018
I've posted more information below. Could you please check if I did something wrong in u , us1, us2, us3, x1, x2 and x3

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Oct. de 2018

Editada:

dpb
el 28 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by