在单元格数组中绘制数字

时间:2019-03-06 17:33:33

标签: matlab

我只想绘制存储在单元格数组中的所有实数数据。我的单元格数组是1-100一维的,但是我对如何将plot()函数和hold on函数实际应用感到困惑。

这是我的代码:

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


%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;

% Declare the \lambda_i, i=1,..., 6, variables
L = cell(1,6);
L1 = abs((b2 - b3)/(a2 - a3));
L2 = abs((b1 - b3)/(a1 - a3));
L3 = abs((b1 - b2)/(a1 - a2));
L4 = abs((b1 - b4)/(a1 - a4));
L5 = abs((b2 - b4)/(a2 - a4));
L6 = abs((b3 - b4)/(a3 - a4)); 
L{1,1} = L1;
L{1,2} = L2;
L{1,3} = L3;
L{1,4} = L4;
L{1,5} = L5;
L{1,6} = L6;

% Create function handle for floww()
F = @floww;

for j = 1:6
   D{1,j} = input('Input in1 through in6: ');
end

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

   % First integrals as described in the paper
   I1{1,i} = 2*(C{1,i}(1,1)).^2 + 2*(C{1,i}(2,1)).^2 + 2*(C{1,i}(3,1)).^2 + 2*(C{1,i}(4,1)).^2 + 2*(C{1,i}(5,1)).^2 + 2*(C{1,i}(6,1)).^2;
   I2{1,i} = (-C{1,i}(3,1))*(-C{1,i}(6,1)) - (C{1,i}(2,1))*(-C{1,i}(5,1)) + (-C{1,i}(1,1))*(-C{1,i}(4,1));
   I3{1,i} = 2*L1*(C{1,i}(1,1)).^2 + 2*L2*(C{1,i}(2,1)).^2 + 2*L3*(C{1,i}(3,1)).^2 + 2*L4*(C{1,i}(4,1)).^2 + 2*L5*(C{1,i}(5,1)).^2 + 2*L6*(C{1,i}(6,1)).^2;
   plot(k, I1{1,i});
   hold on;
end


% 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{1,3} - L{1,2};
A65 = L{1,6} - L{1,5};
A13 = L{1,1} - L{1,3};
A46 = L{1,4} - L{1,6};
A21 = L{1,2} - L{1,1};
A54 = L{1,5} - L{1,4};
A35 = L{1,3} - L{1,5};
A62 = L{1,6} - L{1,2};
A43 = L{1,4} - L{1,3};
A16 = L{1,1} - L{1,6};
A24 = L{1,2} - L{1,4};
A51 = L{1,5} - L{1,1};

% Declare del(T)
delT = 1;

% Declare the 6-by-6 coefficient matrix B

B = [1, -A32*(delT/2)*in3, -A32*(delT/2)*in2, 0, -A65*(delT/2)*in6, -A65*(delT/2)*in5;
    -A13*(delT/2)*in3, 1, -A13*(delT/2)*in1, -A46*(delT/2)*in6, 0, A46*(delT/2)*in4;
    -A21*(delT/2)*in2, -A21*(delT/2)*in1, 1, -A54*(delT/2)*in5, -A54*(delT/2)*in4, 0;
    0, -A62*(delT/2)*in6, -A35*(delT/2)*in5, 1, -A35*(delT/2)*in3, -A62*(delT/2)*in2;
    -A16*(delT/2)*in6, 0, -A43*(delT/2)*in4, -A43*(delT/2)*in3, 1, -A16*(delT/2)*in1;
    -A51*(delT/2)*in5, -A24*(delT/2)*in4, 0, -A24*(delT/2)*in2, -A51*(delT/2)*in1, 1];

% Declare input vector

N = [in1; in2; in3; in4; in5; in6];

% Solve the system Bx = N for x where x
% denotes the X_i^(n+1) vector in research notes

x = B\N;

% Assign output variables
out1 = x;

%disp(x);
%disp(out1(2,1));
end

使用plot(k, I1{1,i});在for循环中进行绘制。输出的数字不是我期望或想要的:

有人可以向我解释我做错了什么,和/或如何获得我想要的吗?

1 个答案:

答案 0 :(得分:3)

当数组变得更简单时,您需要停止使用单元格数组来存储数字数据和索引变量名称。

我在下面编辑了您的代码,以绘制I1数组。

为使其正常工作,我将几乎所有单元格数组都更改为数字数组,并简化了一堆索引。注意,现在使用zeros而不是cell进行初始化,因此用括号()而不是花括号{}进行索引。

我并没有太大的改变结构,因为您的评论表明您正在遵循这种布局的一些文献

对于绘制,您试图在循环中绘制单个点-要做到没有线(这些点是不同的),因此需要指定一个标记,例如plot(x,y,'o')。但是,我所做的只是在循环后进行绘图-因为无论如何您都存储了结果I1数组。

% 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);

%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;

% 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)); 

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

% 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

   % 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 + 2*L(2)*(C{i}(2,1)).^2 + 2*L(3)*(C{i}(3,1)).^2 + 2*L(4)*(C{i}(4,1)).^2 + 2*L(5)*(C{i}(5,1)).^2 + 2*L(6)*(C{i}(6,1)).^2;
end
plot(1:100, I1);


% 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);

    % Declare del(T)
    delT = 1;
    % Declare the 6-by-6 coefficient matrix B
    B = [1, -A32*(delT/2)*in3, -A32*(delT/2)*in2, 0, -A65*(delT/2)*in6, -A65*(delT/2)*in5;
        -A13*(delT/2)*in3, 1, -A13*(delT/2)*in1, -A46*(delT/2)*in6, 0, A46*(delT/2)*in4;
        -A21*(delT/2)*in2, -A21*(delT/2)*in1, 1, -A54*(delT/2)*in5, -A54*(delT/2)*in4, 0;
        0, -A62*(delT/2)*in6, -A35*(delT/2)*in5, 1, -A35*(delT/2)*in3, -A62*(delT/2)*in2;
        -A16*(delT/2)*in6, 0, -A43*(delT/2)*in4, -A43*(delT/2)*in3, 1, -A16*(delT/2)*in1;
        -A51*(delT/2)*in5, -A24*(delT/2)*in4, 0, -A24*(delT/2)*in2, -A51*(delT/2)*in1, 1];

    % Declare input vector
    N = [in1; in2; in3; in4; in5; in6];
    % Solve the system Bx = N for x where x
    % denotes the X_i^(n+1) vector in research notes
    x = B\N;
    % Assign output variables
    out1 = x;
end

输出为in1..6 = 1 .. 6

plot

注意:如果在笨拙的变量名上包含数组,则可以将代码简化为 lot 。下面的代码可以达到与脚本主体完全相同的结果,但是更加灵活和可维护:

看看您的整数表达式变得简单多了!

% 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);

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

% 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]) ) );  

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

% 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()

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

编辑:

您可以通过使用以下几点来简化floww函数

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

  2. 通知delT/2是几乎每个元素中的一个因素,请将其分解出来!

  3. delT/2不是因数的唯一非零元素是1的对角线...使用eye来添加它。

  4. 输入in1..6变量作为向量。调用floww时,您已经有了引导程序-将其拆分没有任何意义。

  5. 使用输入作为矢量,我们可以使用hankel之类的实用程序函数进行一些整洁的索引编制。对于初学者来说,这是一个延伸,但我将其作为一个演示。

代码:

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

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

    % Declare del(T)
    delT = 1;
    % Declare the 6-by-6 coefficient matrix B
    % Factored out (delt/2) and the D coefficients
    B = eye(6,6) - (delT/2) * D( hankel( [4 3 2 1 6 5], [5 4 3 2 1 6] ) ) .*...
        [     0, A(3,2), A(3,2),      0, A(6,5),  A(6,5);
         A(1,3),      0, A(1,3), A(4,6),      0, -A(4,6);
         A(2,1), A(2,1),      0, A(5,4), A(5,4),       0;
           0,    A(6,2), A(3,5),      0, A(3,5),  A(6,2);
         A(1,6),      0, A(4,3), A(4,3),      0,  A(1,6);
         A(5,1), A(2,4),      0, A(2,4), A(5,1),       0];

    % Solve the system Bx = N for x where x
    % denotes the X_i^(n+1) vector in research notes
    out1 = B\D(:);
end

您看到当我们简化这样的事情时,代码更易于阅读。例如,在我看来(完全不了解文献),就好像您的B(2,6)元素中有符号错误,它与所有其他元素的符号相反...