Search code examples
arraysmatlabdoublecell

Matlab- how can I change an array since it is seen as double


I need to iterate Newton-Raphson.The problem is:

For mmm=1:

1) If m=1 take c1=c1b and c2=1-c1 and do the loop for u1,2(i) and p1,2(i)

2)If m=2 take c1=c1+dc and c2=1-c1, and this time do the loop with new c1 and c2 for u1,2(i) and p1,2(i)

3) If m=3 take c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2)) and do the loop for new c1 and c2.

Then increase the iteration number: mmm=2 ;

mmm keeps count of the number of N-R iterations. The first iteration has mmm=1, the second mmm=2, etc. (This particular run only do 2 iterations).

sumint are inside of the integrals. 'c1, c2 are the camber effects, u1 u2 are the velocities, p1 p2 are pressures.

Relevant part of the code:

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 

The error is: ??? Attempted to access u.%cell(2); index out of bounds because numel(u.%cell)=1.

Error in ==> grab3_SmithWilson at 75 p{1}(i)=0.5*(1.-u{1}(i).^2)+q{1}(i);


Solution

  • You need to show us what you want to see. When I run the code you posted now, I find that first ub is printed as you have written, then each cell of ub is overwritten on each loop iteration. What I mean is that you are not putting values into the arrays that are stored in the cells, you are putting values in to the cells themselves. Are you sure that's what you want?

    If you want to store the calculation in the elements of the arrays that are stored in the cells, the following will work:

    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]
    

    This is why I suggested reading about accessing parts of cells. Really though, this is just a guess until you tell us what you want from your script.