是否有可能在for循环中使用符号变量?

时间:2013-05-24 13:02:42

标签: matlab variables

我在for循环中有一些符号变量,然后出现错误

??? The following error occurred converting from sym to double:
  Error using ==> mupadmex
  Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
    If the input expression contains a symbolic variable, use the VPA function instead.

问题是我应该在计算结束时插入符号变量的值,因为它们是积分变量......我该如何解决这个问题?

function [product,Mi]=test(ii)
variables % this is just a numerical value for m, M and L
syms x y q B alp kk

product=zeros(3,1);
for i=1:3
for ni=1:9

n=new(ni); % All possible "n"s for each "ni" here new(ni) a function which gives different size matrix each time in the for loop 

n_vector=zeros(1,3);

for jj=1:size(n,1)

    n_vector(:)=n(jj,:);




p_vector=((2*pi/L)*(n_vector));
q_vector=((L/(2*pi))*[1,0,0]);
A=zeros(1,3);

    if ii==1
        Mi=sqrt(x.^2*M^2+(1-x).*m^2);
        A=y.*p_vector;
    elseif ii==2
        Mi=sqrt(m^2-(x.*(1-x)).*q^2);
        A=x.*q_vector;
    elseif ii==3
        Mi=sqrt((y.^2).*(M^2-(x.*(1-x)).*q^2)+(1-y).*m^2);        
        A=y.*(p_vector-q_vector*x);
    elseif ii==4
        Mi=sqrt((1-y).^2*M^2+y.^2*m^2-(x.*(1+x)).*y^2*q^2);    
        A=(1-y).*p_vector-(x.*y).*q_vector;
    end

        if i==1
           product(i,1)=A(1,1)*n_vector(1,1)    
        else
           product(i,1)=A(i,1)*n_vector(1,i)
        end

end
end
end
end

这里只是我功能的一部分...我知道“产品”看起来很奇怪,我的意思是我可以写一个没有for循环的表达式,但实际上在我的函数中我需要一个for循环这里每个产品都是这样的......

1 个答案:

答案 0 :(得分:1)

您无法将A(1,1)*n_vector(1,1)分配给product(i,1)的原因是因为product已被定义为双数组。

无效:

product = zeros(3, 1)
product(1,1) = sym('A')

  The following error occurred converting from sym to double:
  Error using mupadmex
  Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
  If the input expression contains a symbolic variable, use the VPA function instead.

会工作:

product=cell(3,1);
product(i,1) = {sym('A')}
product(2,1) = {sym('B')}
product(3,1) = {sym('C')}