# Thread: mfile code doesnt work, why?

1. ## mfile code doesnt work, why?

hi guys my professor gave us this code to work with but i dont understand why i keep getting errors? plz explain why im wrong

heres the assignment
plot arrow with tail point P(x0,y0) in xy plane and tip at the point (x0+a,y0+b). Writing Pvector=<x0,y0> and Vvector=<a,b>.

heres my code

function y = arrow(P,V,color)

if nargin < 3
color =
'b';
end
x0 = P(1);
y0 = P(2);
a = V(1); b = V(2);

l = max(norm(V), eps);
u = [x0 x0+a]; v = [y0 y0+b];
hchek = ishold;

plot(u,v,color,
'LineWidth',2)
hold
on
h = l - min(.2*l, .2) ; v = min(.2*l/sqrt(3), .2/sqrt(3) );

a1 = (a*h -b*v)/l;
b1 = (b*h +a*v)/l;

plot([x0+a1, x0+a], [y0+b1, y0+b], color,
'LineWidth',2)

a2 = (a*h +b*v)/l;
b2 = (b*h -a*v)/l;

plot([x0+a2, x0+a], [y0+b2, y0+b], color,
'LineWidth',2)
if hchek == 0
hold
off
end

heres my errors
Error in ==> arrow at 7
x0 = P(1);

2. Originally Posted by fearless901
hi guys my professor gave us this code to work with but i dont understand why i keep getting errors? plz explain why im wrong

heres the assignment
plot arrow with tail point P(x0,y0) in xy plane and tip at the point (x0+a,y0+b). Writing Pvector=<x0,y0> and Vvector=<a,b>.

heres my code

function y = arrow(P,V,color)

if nargin < 3
color =
'b';
end
x0 = P(1);
y0 = P(2);
a = V(1); b = V(2);

l = max(norm(V), eps);
u = [x0 x0+a]; v = [y0 y0+b];
hchek = ishold;

plot(u,v,color,
'LineWidth',2)
hold
on
h = l - min(.2*l, .2) ; v = min(.2*l/sqrt(3), .2/sqrt(3) );

a1 = (a*h -b*v)/l;
b1 = (b*h +a*v)/l;

plot([x0+a1, x0+a], [y0+b1, y0+b], color,
'LineWidth',2)

a2 = (a*h +b*v)/l;
b2 = (b*h -a*v)/l;

plot([x0+a2, x0+a], [y0+b2, y0+b], color,
'LineWidth',2)
if hchek == 0
hold
off
end

heres my errors
Error in ==> arrow at 7
x0 = P(1);
What does your calling statement look like and what are the values of the arguments passed.

CB

3. Code:
arrow([0 0],[1 1])
Works for me.