如何在这里正确使用全局变量

时间:2019-02-28 23:07:27

标签: matlab

我是Matlab的新手,我认为需要使用global,以便可以在下面的L1, L2, L3, L4, L5, L6中使用变量for-loop。但是,当我在floww()函数中将这些变量设置为全局变量时,我想要的并没有发生。我遇到以下错误:

  

未定义的函数或变量'L1'。

     

流量错误(第26行)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;

这告诉我Matlab不知道L变量在floww()范围之外。为什么全局无法解决此问题?有没有更好的方法来做我想做的事?

这是我的代码:

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

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

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

% Iterate through floww()

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

%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
global L1;
global L2;
global L3;
global L4;
global L5;
global L6;

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

% A_ij = (lambda_i - lambda_j)
% Declare relevant A_ij values

A32 = L3 - L2;
A65 = L6 - L5;
A13 = L1 - L3;
A46 = L4 - L6;
A21 = L2 - L1;
A54 = L5 - L4;
A35 = L3 - L5;
A62 = L6 - L2;
A43 = L4 - L3;
A16 = L1 - L6;
A24 = L2 - L4;
A51 = L5 - L1;

% 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

0 个答案:

没有答案