Oria Gruber
Oria Gruber

Reputation: 1533

"Undefined function or variable "u"." MATLAB essay

I have a very weird problem with a matlab essay I need to hand in. I wrote a function that rotates a 3d vector. I called it rotate.

rotate gets a vector v,an angle theta, and angle alpha, and a scalar r. The function will rotate increase the angle the vector creates with Z axis by theta, rotate it around the Z axis (increase the angle it creates with X axis) by alpha, and then stretch it by r.

for example:

v=(0,0,1)
theta= pi/2
alpha=0
r=1
rotate(v,theta,alpha,r) will return (0,1,0).

My problem is, whenever i call the function rotate, i get an error saying: ??? Undefined function or variable "u".

Error in ==> rotate at 51 x = u(1);

And in the entire code I wrote, there is no function or variable called "u".

Here is my code. Basically I transform the vector i was given to spherical cordinates, and then just add theta and alpha. I think it should work no?

function [output] = rotate(v,theta,alpha,r)
if(isnumeric(v))
    [i,j]=size(v);
    if(i>j)
        for i=1:3
            sum = sum+pow(v(i,1),2);
        end
        sum=sqrt(sum);
        output(1,1)=sum*r;
        output(2,1)=acos(v(3,1)/sum)+theta;
        output(3,1)=atan(v(2,1)/v(1,1))+alpha;
        if((output(2,1)>pi)||(output(2,1)<-1*pi))
            prompt={'Invalid values for second cordinate, more than pi or less than -pi'};
            return
        end
        if((output(3,1)>2*pi)||(output(3,1)<-2*pi))
            prompt={'Invalid values for third cordinate, more than 2 pi or less than -2pi'};
            return
        end
        r=output(1,1);
        angle1=output(2,1);
        angle2=output(3,1);
        output(1,1)=r*sin(angle1)*cos(angle2);
        output(2,1)=r*sin(angle1)*sin(angle2);
        output(3,1)=r*cos(angle1);
    else
        for j=1:3
        sum=sum+pow(v(1,j),2);
        end
        sum=sqrt(sum);
        output(1,1)=sum*r;
        output(1,2)=acos(v(1,3)/sum)+theta;
        output(1,3)=atan(v(1,2/v(1,1))+alpha;
        if((output(1,2)>pi)||(output(1,2)<-1*pi))
             prompt={'Invalid values for second cordinate, more than pi or less than -pi'};
             return;
        end
        if((output(1,3)>2*pi)||(output(1,3)<-2*pi))
            prompt={'Invalid values for third cordinate, more than 2 pi or less than -2pi'};
            return
        end;
        r=output(1,1);
        angle1=output(1,2);
        angle2=output(1,3);
        output(1,1)=r*sin(angle1)*cos(angle2);
        output(1,2)=r*sin(angle1)*cos(angle2);
        output(1,3)=r*cos(angle1);
    end
else
    prompt={'not numeric'};
    return
end
end

Upvotes: 0

Views: 826

Answers (1)

paddy
paddy

Reputation: 63481

You are not in the correct directory, or your path is not set properly. You are running the inbuilt rotate function, which has a problem with the arguments you give it.

If you type edit rotate.m, you will notice that the function is not yours.

Set your path correctly, and/or rename your function.

Upvotes: 1

Related Questions