具有3维矩阵和向量的不合格自变量

时间:2018-08-20 07:09:35

标签: matlab octave

我想在3-d矩阵和向量之间执行以下逐元素加法运算。

for l = 1:dim
    M_star(j,k,:) .+= 15*x(j,:).*x(k,:).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*3*x(l,:).*x_t(l,:).*(sum(x.^2))./(nm.^7) - (l == k)*3*(x(l,:).*x_t(l,:)).*(sum(x.^2))./(nm.^7)
endfor

但是我遇到了以下错误:

error: =: nonconformant arguments (op1 is 101x101x101, op2 is 101x101)

例如,我检查了加法运算是否适用于单个向量

A .+= 15*x(j,:).*x(k,:).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*3*x(l,:).*x_t(l,:).*(sum(x.^2))./(nm.^7) - (l == k)*3*(x(l,:).*x_t(l,:)).*(sum(x.^2))./(nm.^7)

在A初始化为的地方工作

A = zeros(1,N) % N = 101 in this case

x,x_t,nm是1 x 101矢量,而j,k,l是变量。

% initial conditions
x0 = sin(2.5*pi*SpaceSpan);
x = ones(dim, 1)*x0; % initial states
x_t = x
nm = zeros(1, N);
M_star = zeros(N,N,N);

但以下方法也不起作用

M_star(j,k,:) .+= A

非常感谢您。

编辑:完整代码

% Generalized Newtonian with a simple Crank-Nicolson method (implicit method)

clc;
clear;

% initialize
k = 0.01; % space step
h = 0.01; % time step
T = 10;
dim = 10; % number of dimensions
SpaceSpan = (0:k:1);
TimeSteps = round(T/h);
N = length(SpaceSpan);

% initial conditions
x0 = sin(2.5*pi*SpaceSpan);
x = ones(dim, 1)*x0; % initial states
x_t = x
x_tt = x
b = ones(dim, 1)*x0;
gradb = zeros(dim, dim, N);
nm = zeros(1, N);

MaxSteps = 1000000;  % max steps for path evolution
TolErr = 1e-6;    % termination tolerance

TimeSteps = round(T/h);
XSpan = (0:k:1);

% generate important matrices
L2 = (-2*diag(ones(N-4,1)) + diag(ones(N-5,1),-1) + diag(ones(N-5,1),1))/k^2;
L4 = (6*diag(ones(N-4,1)) - 4*diag(ones(N-5,1),-1) - 4*diag(ones(N-5,1),1) + diag(ones(N-6,1),-2) + diag(ones(N-6,1),2))/k^4;
M_star = zeros(N,N,N) % M star matrix

% reparametrization
c = zeros(N,1);
c(end) = 1/k^2;

% evolution
for i = 1:TimeSteps
  x_ = x % old x
  b_ = b % old b
  gradb_ = gradb % old Jacobian matrix b

  for w = 1:N
    nm(w) = sqrt(sum(x(:,w).^2))
    if (w == 1)||(w == N)
      x_t(:,w) = 0
      x_tt(:,w) = 0
    else
      x_t(:,w) = (x(:,w+1)-x(:,w-1))/2/k
      x_tt(:,w) = (x(:,w+1)-2*x(:,w)+x(:,w-1))/k^2
    end
  end

  %%
  for j = 1:dim % assign value to b
    b(j,:) = -x(j,:)./nm.^3
    for k = 1:dim % assign value to Jacobian matrix gradb
      if j == k
        gradb(j,k,:) = (sum(x.^2).-3*x(j,:).^2)./(nm.^5)
        for l = 1:dim
          M_star(j,k,:) .+= (15*x(j,:).^2.-3*sum(x.^2)).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*6*sum(x.^2).*x(l,:).*x_t(l,:)./(nm.^7) % unable to perform compounded addition
        endfor
      else
        gradb(j,k,:) = -3*x(j,:).*x(k,:)./(nm.^5)
        for l = 1:dim
          M_star(j,k,:) .+= 15*x(j,:).*x(k,:).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*3*x(l,:).*x_t(l,:).*(sum(x.^2))./(nm.^7) - (l == k)*3*(x(l,:).*x_t(l,:)).*(sum(x.^2))./(nm.^7)
        endfor
      end
    endfor
  endfor

  % implicit method, note that M* is derived explicitly (backward in time)

  x(3:end-2) = inv(L4 - (gradb.' + gradb)*L2)*(x(3:end-2) + M_star*x_t(3:end-2) - gradb.'*b) % implicit solve
  % plot
  if mod(i,PlotFrequency) == 0
    subplot(1,2,1);
    set(data,'xdata',SpaceSpan,'ydata',u)
    title(['Steps=',num2str(i),' Change=',num2str(change)]);

    subplot(1,2,2);
    plot([1:TimeSteps],integrand);
    title(['Integrand of action (action=',0,')']);
    drawnow;
  end   
endfor

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

假设ABC为101x101,假设M_star为101x101x101

M_star + repmat(ABC,[1,1,101])

以您的示例为例:确实很难阅读,而且我不知道j,k,nm和x是什么。通常:Matlab最多可对2维进行宽大处理,但从3维开始,您必须非常精确。

答案 1 :(得分:0)

我想出了解决方案。因此,事实证明我存在一些尺寸不一致的地方,即我应该转置RHS,因为LHS是列向量,而RHS是行向量。但是,无论如何它仍然无法正常工作,我不得不添加一个临时向量来存储值。解决方法如下:

for j = 1:dim % assign value to b
    b(j,:) = -x(j,:)./nm.^3
    for k = 1:dim % assign value to Jacobian matrix gradb
      if j == k
        gradb(j,k,:) = (sum(x.^2).-3*x(j,:).^2)./(nm.^5)
        for l = 1:dim
          temp .+= ((15*x(j,:).^2.-3*nm.^2).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*6.*nm.^2.*x(l,:).*x_t(l,:)./(nm.^7)).' 
        endfor
        M_star(j,k,:) = temp
      else
        gradb(j,k,:) = -3*x(j,:).*x(k,:)./(nm.^5)
        for l = 1:dim
          temp .+= (15*x(j,:).*x(k,:).*x(l,:).*x_t(l,:)./(nm.^7) - (l == j)*3*x(l,:).*x_t(l,:).*(sum(x.^2))./(nm.^7) - (l == k)*3*(x(l,:).*x_t(l,:)).*(sum(x.^2))./(nm.^7)).'
        endfor
        M_star(j,k,:) = temp
      end
    endfor
  endfor