未定义的函数或变量u。“MATLAB的文章

时间:2021-11-22 19:59:01

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.

我有一个很奇怪的问题,我需要交一个matlab的论文。我写了一个旋转三维矢量的函数。我叫它旋转。

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.

向量v,旋转一个角度θ,和角α,和一个标量r。函数将旋转增加向量创建与Z轴角θ,旋转它在Z轴(增加角度创建与X轴),α,然后伸展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".

我的问题是,每当我调用函数旋转时,我都会得到一个错误:???未定义的函数或变量u。

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

==>旋转51 x = u(1);

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

在我写的整个代码中,没有一个函数或变量叫做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

1 个解决方案

#1


1  

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.

如果您键入编辑旋转。m,你会注意到这个函数不是你的。

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

正确设置您的路径,并/或重命名您的函数。

#1


1  

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.

如果您键入编辑旋转。m,你会注意到这个函数不是你的。

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

正确设置您的路径,并/或重命名您的函数。