Error in program (assignment A(I)=B)

1 visualización (últimos 30 días)
Geovanni  Rodriguez Martinez
Geovanni Rodriguez Martinez el 8 de Mayo de 2015
Editada: James Tursa el 8 de Mayo de 2015
This is my code
>clc, clear, close
xmax=4;
m = 0;
for ang=0:0.5:360
m = m + 1;
if ang<=270
s(m)=(xmax/2)*(1-cos((2*pi*ang)/(270)));
else
s(m)=0;
end
end
D = [0:0.5:360];
figure; plot (D,s);
E = [0.5:.5:360];
V=diff(s);
figure; plot (E,V);
F = [1:0.5:360];
A=diff(V);
figure; plot (F,A);
ofs=0.470;
rb=2.0;
rr=0.70;
p=0;
for ang=0:0.5:360
p =p+1;
sref(p)=sqrt(((rb(p)+rr(p))^2)-(ofs(p)^2));
xr(p)=ofs;
yr(p)=sref(p)+s(m);
l=diff(s);
b(p)=atan((xr(p)-l)/(yr(p)));
Xc(p)=xr(p)-rr*sin(b(p));
Yc(p)=yr(p)-rr*cos(b(p));
xc(p)=Xc(p)*cos(ang)+Yc(p)*sin(ang);
yc(p)=-Xc(p)*sin(ang)+Yc(p)*cos(ang);
end
figure; plot(xc,yc);
>Error
>In an assignment A(I) = B, the number of elements in B and I must be the same.
>Error in graphscam (line 34)
> b(p)=atan((xr(p)-l)/(yr(p)));
Can Someone help me? I don't understand. I have MATLAB R2011b
  2 comentarios
Brendan Hamm
Brendan Hamm el 8 de Mayo de 2015
Format your code using the '{} Code' button
Adam
Adam el 8 de Mayo de 2015
Have you debugged the code? Finding the cause of errors such as this is trivial if you put breakpoints in and query:
size( p );
size( atan((xr(p)-l)/(yr(p)));
at the relevant points. At a glance they look as though they should both be the same size, but there's no need to be guessing from a block of code when you can just debug and find the answer easily.

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 8 de Mayo de 2015
Editada: James Tursa el 8 de Mayo de 2015
To debug, at the command line type the following:
dbstop if error
Then run your code as usual. When the error occurs, you will be at the line of code that caused the problem. Examine the sizes of the variables involved to see what the mismatch is. Then trace back through your code to figure out the problem.
If you want to step through your code in the debugger, click on the dash to the right of a line number in the editor (you can set this on any line with a dash ... not just the line with the error). That should put a red dot on that line, which indicates a break point. When you subsequently run your code, the execution will pause at the breakpoints. This allows you to examine variables etc at the command line. You can also use the "Step" etc buttons at the top to step through your code line by line.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by