您需要停止使用单元格数组进行数字数据,并在数组更简单时使用索引变量名称。
我在下面编辑了你的代码来绘制
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) = 2L(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);
% 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
</code>
输出用
in1..6 = 1 .. 6
:
注意:您可以简化此代码a
批量
如果你在笨重的变量名称上包含数组。下面的脚本实现了完全相同的结果,但更加灵活和可维护:
的
看看积分表达式变得多么简单!
</强>
% 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) = 2sum(D.^2);
I2(i) = sum( D(1:3).D(4:6) );
I3(i) = 2sum((L.’).D.^2).^2;
end
plot( k, I1 );
</code>
编辑:
你可以简化
floww
通过使用一些东西来发挥作用
A
可以很容易地声明为单个矩阵。
注意
delT/2
是几乎所有元素中的一个因素,将其考虑在内!
唯一的非零元素在哪里
delT/2
不是一个因素是对角线的…在使用中添加这个
eye
代替。
输入
in1..6
变量作为向量。你打电话时已经有了矢量
floww
- 毫无意义地打破它。
</p>
</LI>
<LI>
<P>
使用输入作为向量,我们可以使用实用程序函数
<code>
hankel
</code>
做一些整洁的索引。这个是初学者的延伸,但我把它作为演示包括在内。
</p>
</LI>
</醇>
码:
% 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
</code>
你看,当我们简化这样的事情时,代码更容易阅读。例如,它看起来像我(根本不知道文献),就像你有一个标志错误
B(2,6)
元素,这是所有其他元素的相反标志……