在Cell数组中绘制数字


天线宝宝
2025-03-22 06:33:11 (17天前)
  1. 我想只绘制存储在Cell Array中的数据,这些数据都是实数。我的单元格数组是1-100 1维,但我很困惑如何实际应用plot()函数与...

2 条回复
  1. 0# 那年 | 2019-08-31 10-32



    您需要停止使用单元格数组进行数字数据,并在数组更简单时使用索引变量名称。



    我在下面编辑了你的代码来绘制

    I1

    阵列。



    为了使它工作,我几乎将所有单元格数组更改为数字数组并简化了一堆索引。注意初始化现在是

    zeros

    代替

    cell

    因此用括号索引

    ()

    不是花括号

    {}




    我没有太多改变结构,因为你的评论表明你正在关注这个布局的一些文献



    对于绘图,你试图在循环中绘制单点 - 为此你没有线(点是不同的),所以需要指定一个像

    plot(x,y,’o’)

    。然而,我所做的只是在循环之后绘制 - 因为你存储了结果

    I1

    无论如何阵列。




    1. % Initalize arrays for storing data
      C = cell(1,100); % Store output vector from floww()
      D = zeros(1,6); % User inputted initial point
      I1 = zeros(1,100);
      I2 = zeros(1,100);
      I3 = zeros(1,100);

    2. %Declare alpha and beta variables detailed in Theorem 1 of paper
      a1 = 0; a2 = 2; a3 = 4; a4 = 6;
      b1 = 2; b2 = 3; b3 = 7; b4 = 10;

    3. % Declare the \lambda_i, i=1,…, 6, variables
      L = zeros(1,6);
      L(1) = abs((b2 - b3)/(a2 - a3));
      L(2) = abs((b1 - b3)/(a1 - a3));
      L(3) = abs((b1 - b2)/(a1 - a2));
      L(4) = abs((b1 - b4)/(a1 - a4));
      L(5) = abs((b2 - b4)/(a2 - a4));
      L(6) = abs((b3 - b4)/(a3 - a4));

    4. for j = 1:6
      D(j) = input(‘Input in1 through in6: ‘);
      end

    5. % Iterate through floww()
      for i = 1:100
      C{i} = floww(D(1), D(2), D(3), D(4), D(5), D(6), L); % Output from floww() is a 6-by-1 vector
      for j = 1:6
      D(j) = C{i}(j,1); % Reassign input values to put back into floww()
      end

    6. % First integrals as described in the paper
      I1(i) = 2(C{i}(1,1)).^2 + 2(C{i}(2,1)).^2 + 2(C{i}(3,1)).^2 + 2(C{i}(4,1)).^2 + 2(C{i}(5,1)).^2 + 2(C{i}(6,1)).^2;
      I2(i) = (-C{i}(3,1))(-C{i}(6,1)) - (C{i}(2,1))(-C{i}(5,1)) + (-C{i}(1,1))(-C{i}(4,1));
      I3(i) = 2
      L(1)(C{i}(1,1)).^2 + 2L(2)(C{i}(2,1)).^2 + 2L(3)(C{i}(3,1)).^2 + 2L(4)(C{i}(4,1)).^2 + 2L(5)(C{i}(5,1)).^2 + 2L(6)*(C{i}(6,1)).^2;
      end
      plot(1:100, I1);

    7. % This function will solve the linear system
      % Bx^(n+1) = x detailed in the research notes
      function [out1] = floww(in1, in2, in3, in4, in5, in6, L)
      % A_ij = (lambda_i - lambda_j)
      % Declare relevant A_ij values
      A32 = L(3) - L(2);
      A65 = L(6) - L(5);
      A13 = L(1) - L(3);
      A46 = L(4) - L(6);
      A21 = L(2) - L(1);
      A54 = L(5) - L(4);
      A35 = L(3) - L(5);
      A62 = L(6) - L(2);
      A43 = L(4) - L(3);
      A16 = L(1) - L(6);
      A24 = L(2) - L(4);
      A51 = L(5) - L(1);

    8. % Declare del(T)
    9. delT = 1;
    10. % Declare the 6-by-6 coefficient matrix B
    11. B = [1, -A32*(delT/2)*in3, -A32*(delT/2)*in2, 0, -A65*(delT/2)*in6, -A65*(delT/2)*in5;
    12.     -A13*(delT/2)*in3, 1, -A13*(delT/2)*in1, -A46*(delT/2)*in6, 0, A46*(delT/2)*in4;
    13.     -A21*(delT/2)*in2, -A21*(delT/2)*in1, 1, -A54*(delT/2)*in5, -A54*(delT/2)*in4, 0;
    14.     0, -A62*(delT/2)*in6, -A35*(delT/2)*in5, 1, -A35*(delT/2)*in3, -A62*(delT/2)*in2;
    15.     -A16*(delT/2)*in6, 0, -A43*(delT/2)*in4, -A43*(delT/2)*in3, 1, -A16*(delT/2)*in1;
    16.     -A51*(delT/2)*in5, -A24*(delT/2)*in4, 0, -A24*(delT/2)*in2, -A51*(delT/2)*in1, 1];
    17. % Declare input vector
    18. N = [in1; in2; in3; in4; in5; in6];
    19. % Solve the system Bx = N for x where x
    20. % denotes the X_i^(n+1) vector in research notes
    21. x = B\N;
    22. % Assign output variables
    23. out1 = x;
    24. end

    25. </code>


    输出用

    in1..6 = 1 .. 6









    注意:您可以简化此代码a

    批量

    如果你在笨重的变量名称上包含数组。下面的脚本实现了完全相同的结果,但更加灵活和可维护:




    看看积分表达式变得多么简单!
    </强>




    1. % Initalize arrays for storing data
      C = cell(1,100); % Store output vector from floww()
      D = zeros(1,6); % User inputted initial point
      I1 = zeros(1,100);
      I2 = zeros(1,100);
      I3 = zeros(1,100);

    2. %Declare alpha and beta variables detailed in Theorem 1 of paper
      a = [0, 2, 4, 6];
      b = [2, 3, 7, 10];

    3. % Declare the \lambda_i, i=1,…, 6, variables
      L = abs( ( b([2 1 1 1 2 3]) - b([3 3 2 4 4 4]) ) ./
      ( a([2 1 1 1 2 3]) - a([3 3 2 4 4 4]) ) );

    4. for j = 1:6
      D(j) = input(‘Input in1 through in6: ‘);
      end

    5. % Iterate through floww()
      k = 1:100;
      for i = k
      C{i} = floww(D(1), D(2), D(3), D(4), D(5), D(6), L); % Output from floww() is a 6-by-1 vector
      D = C{i}; % Reassign input values to put back into floww()

    6. % First integrals as described in the paper
      I1(i) = 2sum(D.^2);
      I2(i) = sum( D(1:3).
      D(4:6) );
      I3(i) = 2sum((L.’).D.^2).^2;
      end
      plot( k, I1 );

    7. </code>


    编辑:



    你可以简化

    floww

    通过使用一些东西来发挥作用






    1. A

      可以很容易地声明为单个矩阵。





    2. 注意

      delT/2

      是几乎所有元素中的一个因素,将其考虑在内!





    3. 唯一的非零元素在哪里

      delT/2

      不是一个因素是对角线的…在使用中添加这个

      eye

      代替。





    4. 输入

      in1..6

      变量作为向量。你打电话时已经有了矢量

      floww

      1. - 毫无意义地打破它。
      2. </p>
      3. </LI>
      4. <LI>
      5. <P>
      6. 使用输入作为向量,我们可以使用实用程序函数
      7. <code>
      8. hankel
      9. </code>
      10. 做一些整洁的索引。这个是初学者的延伸,但我把它作为演示包括在内。
      11. </p>
      12. </LI>

      </醇>


      码:




      1. % In code body, call floww with an array input
        C{i} = floww(D, L);
        %

      2. function [out1] = floww(D, L)
        % A_ij = (lambda_i - lambda_j)
        % Declare A_ij values in a matrix
        A = L.’ - L;

      3. % Declare del(T)
      4. delT = 1;
      5. % Declare the 6-by-6 coefficient matrix B
      6. % Factored out (delt/2) and the D coefficients
      7. B = eye(6,6) - (delT/2) * D( hankel( [4 3 2 1 6 5], [5 4 3 2 1 6] ) ) .*...
      8.     [     0, A(3,2), A(3,2),      0, A(6,5),  A(6,5);
      9.      A(1,3),      0, A(1,3), A(4,6),      0, -A(4,6);
      10.      A(2,1), A(2,1),      0, A(5,4), A(5,4),       0;
      11.        0,    A(6,2), A(3,5),      0, A(3,5),  A(6,2);
      12.      A(1,6),      0, A(4,3), A(4,3),      0,  A(1,6);
      13.      A(5,1), A(2,4),      0, A(2,4), A(5,1),       0];
      14. % Solve the system Bx = N for x where x
      15. % denotes the X_i^(n+1) vector in research notes
      16. out1 = B\D(:);
      17. end

      18. </code>


      你看,当我们简化这样的事情时,代码更容易阅读。例如,它看起来像我(根本不知道文献),就像你有一个标志错误

      B(2,6)

      元素,这是所有其他元素的相反标志……


登录 后才能参与评论