Matlab-如何更改数组,因为它被视为double

时间:2014-01-08 13:04:52

标签: arrays matlab double cell

我需要迭代Newton-Raphson。问题是:

对于mmm = 1:

1)如果m = 1,则取c1 = c1b,c2 = 1-c1,并为u1,2(i)和p1,2(i)进行循环

2)如果m = 2,取c1 = c1 + dc和c2 = 1-c1,这次用新c1和c2为u1,2(i)和p1,2(i)

3)如果m = 3,取c1 =(c1 * st(1) - (c1-dc)* st(2))/(st(1)-st(2))并为新c1做循环和c2。

然后增加迭代次数:mmm = 2;

mmm保持N-R次迭代次数的计数。第一次迭代有mmm = 1,第二次mmm = 2等等(这个特定的运行只进行2次迭代)。

sumint是积分内部。 'c1,c2是外倾效应,u1 u2是速度,p1 p2是压力。

代码的相关部分:

ii=101;
 ub = cell(2, 1);
    ini_cond = [0,0]; 
    for i = 1:2;
           ub{i} = zeros(1,ii);
           ub{i}(:, ii) = ini_cond(i) * rand(1, 1);
    end    

    for i=1:ii;
           x=i*dx;
              fikness = fik*sin(pi.*x);
              ub{1}(i) = (c1b-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
              ub{2}(i) = (c2b+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
    end   
    mmm = 1;
    c1 = c1b;
    m = 1;
    c2=1-c1;
    u = cell(2, 1);
    ini_cond = [0,0];
    for i = 1:2;
       u{i} = zeros(1,ii);
       u{i}(:, ii) = ini_cond(i) * rand(1, 1);
    end    

    for i=1:ii;
       x=(i-1)*dx;
       fikness = fik*sin(pi.*x);
       u{1}(i) = (c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
       u{2}(i)= (c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5)); 
    end

    p = cell(2, 1);
    q = cell(2, 1);

    for i = 1:2;
       p{i} = zeros(1,100);
       q{i} = zeros(1,100);
    end    

    p{1}(1) = 0.5*(1.-u{1}(1).^2);
    q{1}(1) = 0;
    p{2}(1) = 0.5*(1.-u{2}(1).^2);
    q{2}(1) = 0;

    for i = 2:ii;
        q{1}(i) = q{1}(i-1)-dx*(u{1}(i-1)-ub{1}(i-1))./dt;    
        p{1}(i) = 0.5*(1.-u{1}(i).^2)+q{1}(i);    
        q{2}(i) = q{2}(i-1)-dx*(u{2}(i-1)-ub{2}(i-1))./dt;    
        p{2}(i) = 0.5*(1.-u{2}(i).^2)+q{2}(i);    
    end

    st = zeros(2, 1);
    st(1,:) = p{1}(100)-p{2}(100);
   m = m+1;

    if m==3; 
      c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2));
      c2=1-c1;
  end

    sumint = cell(2, 1);
    for i = 1:2    
       sumint{i} = zeros(1,length(x));    
    end 

    sumint{1}(1) = 0.5*(p{2}(1)-p{1}(1));
    sumint{2}(1) = 0.5*(p{2}(1)-p{1}(1)).*(-1/2);

    for i = 2:100;
      x=(i-1)*dx;
      sumint{1}(i) = sumint{1}(i-1)+(p{2}(i)-p{1}(i));    
      sumint{2}(i) = sumint{2}(i-1)+(p{2}(i)-p{1}(i)).*(x-1/2);
    end 

错误是:???试图访问u。%cell(2);索引超出范围因为numel(u。%cell)= 1。

==>中的错误grab3_SmithWilson at 75 p {1}(i)= 0.5 *(1.-u {1}(i)。^ 2)+ q {1}(i);

1 个答案:

答案 0 :(得分:0)

您需要向我们展示您想要看到的内容。当我运行您现在发布的代码时,我发现在您编写的第一个ub打印后,每个循环迭代都会覆盖ub的每个单元格。我的意思是你没有将值放入存储在单元格中的数组中,而是将值放入单元格本身。你确定那是你想要的吗?

如果要将计算存储在单元格中存储的数组元素中,以下内容将起作用:

for i=1:ii;
x=(i-1)*dx;
        fikness=fik*sin(pi.*x);
        ub{1}(i)=(c1b-H1D*(x-0.5)+AD/2*(x-0.5)^2)/(H1-0.5*fikness-A*(x-0.5));
        ub{2}(i)=(c2b+H1D*(x-0.5)-AD/2*(x-0.5)^2)/(1-H1+0.5*fikness+A*(x-0.5));

end

>> ub

ub = 

    [1x101 double]
    [1x101 double]

这就是我建议阅读accessing parts of cells的原因。实际上,这只是一个猜测,直到你告诉我们你的脚本你想要什么。