Incorrect dimensions for matrix multiplication

4 次查看(过去 30 天)
clc;clear all;
syms J
% syms x1 x2 x3
N=50;
%MODEL
x = -2*pi:0.1:2*pi;
y = sin(x)./x;
x_t=transpose(x);
t=x_t;
y_t= sin(t)./t;
t=x_t;
%RANDOMIZATION
relation = [x;y];
ind = 1:length(x);
a = ind(randperm(length(ind)));
shuffeled_vector = relation(:,a);
%NORMALIZATION
factor = max(max(abs(shuffeled_vector(1,:))),max(abs(shuffeled_vector(2,:))));
shuffeled_vector = shuffeled_vector./factor;
%SEPERATION
RATIO = 0.7; % Train-Test Ratio
train_data = shuffeled_vector(:,1:fix(length(x)*RATIO));
test_data = shuffeled_vector(:,fix(length(x)*RATIO)+1:end);
%JACOBIAN MATRIX CALCULATIONS
S = 10; % No. of Neurons
j = 1;
loop = 1;
X = randn(1,3*S+1);
xk=-2+4*X;
syms x [1 3*S+1 ]
h=(exp(x1)-exp(-x1))/(exp(x1)+exp(-x1));
for t_i = train_data(1,:)
i=1:size(t_i,1);
while loop
if j <= S
hs=subs(h,x1,X(j)*t_i+X(S+j));
Jo(i,j)= X(j+2*S)*t_i*(1-hs^2);
elseif (j > S) && ( j <= 2*S )
hs=subs(h,x1,X(j-S)*t_i+X(j));
Jo(i,j)= X(j+S)*t_i*(1-hs^2);
elseif (j > 2*S) && ( j <= 3*S )
hs=subs(h,x1,X(j-2*S)*t_i+X(j-S));
Jo(i,j)= [hs^2];
else
Jo(i,j)= 1;
loop = 0;
end
j = j + 1;
end
A2=0;
for j=(2*S+1):3*S
A1=x(j)*subs(h,x1,X(j-2*S)*t(i)+X(j-S));
A2=A1+A2;
end
y_model(i,1)=A2+x(3*S+1);
end
E=vpa(y_t-y_model);
f=vpa(E.'*E);
while 1
xk_old=xk;
Nmax=100;
u=150;
uscale=0.5;
u_min=0.001;
u_max=150;
ep1 = 1e-10;
ep2 = 1e-10;
ep3 = 1e-10;
[xk]= LevenbergMdeneme(f,xk,E,Jo,u,uscale,u_min,u_max,Nmax,ep1,ep2,ep3);
fprintf('Iteration %d of YSA is done\n',k);
norm_E=double(norm(subs(E,x,xk)));
delta_xk=norm(xk-xk_old);
f_MSE=double(subs(f,x,xk));
training_error(k)=log10(norm_E);
if norm_E<10^-5 && delta_xk<10^-5 || f_MSE<10^-5|| training_error(k)<-3 || k>N
fprintf('SISO_ANN Number of Iterations is %d\n',k);
fprintf('SISO_ANN Training Error is %.4f\n',norm_E);
fprintf('SISO_ANN Change in ANN Weights is %.4f\n',delta_xk);
y_model_val=double(subs(y_model,x,xk));
figure(1);
subplot(2,1,1);
plot(t,y_data,'-*');
hold on
plot(t,y_model_val,'-o');
grid on
title('Train Data Graph');
xlabel('Input');
ylabel('Output');
legend('Real Output of Training Data','Model Output of Train Data')
subplot(2,1,2);
plot(1:1:k,training_error,'b');
grid on
title('Train Error');
xlabel('Iteration');
ylabel('log(MSE)');
break;
end
k=k+1;
end
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.

Error in solution>LevenbergMdeneme (line 148)
zk=D*Jx.'*Ex
%---------------------------------------- I called this function
function [xk]=LevenbergMdeneme(f,xk,E,Jo,u,uscale,u_min,u_max,Nmax,ep1,ep2,ep3);
syms x [1 length(xk)];
Nmax=100;
u=150;
uscale=0.5;
u_min=0.001;
u_max=150;
ep1 = 1e-10;
ep2 = 1e-10;
ep3 = 1e-10;
f_grad(x) = gradient(f,x);
x_old=0;
old_f = 0;
z=size(Jo,2);
n = length(xk);
i=1;
while (i < Nmax) && (abs(double(subs(f,x,xk)) - old_f) > ep1) && (norm(xk-x_old) > ep2) && (norm(double(subs(f_grad,x,xk))) > ep3)
fx=double(subs(f,x,xk));
Ex=double(subs(E,x,xk));
Jx=double(subs(Jo,x,xk));
I=eye(n);
while 1
J_xx=Jx.'*Jx+u.*I;
D=-(inv(J_xx));
zk=D*Jx.'*Ex
f_xz=subs(f,x,xk'+zk');
if f_xz<fx
pk=zk;
f_sk=subs(f,x,xk+x1.*pk);
[sk]=GoldensectionMethod(f_sk,10,-10,10^-4);
x_old=xk;
xk=double(xk+sk.*pk);
u=u/uscale;
break
else
u=u*uscale;
if u<umax && u>umin
continue %go back to start of while
else
break; %end the while
end
end
end
f1=subs(f,x,x_old);
f2=subs(f,x,xk);
normgrad=norm(subs(grad_F,x,xk));
error=abs(f2-f1);
delta_x=norm(xk-x_old);
if i>Nmax
fprintf('Max iteration = %d\n',i)
break
elseif error<ep1
fprintf('Condition 2 iteration = %d\n',i)
break
elseif delta_x<ep2
fprintf('Condition 3 iteration = %d\n',i)
break
elseif normgrad<ep3
fprintf('Condition 4 iteration = %d\n',i)
break
elseif u<u_min || u>u_max
fprintf('Condition 5 iteration = %d\n',i)
break;
end
i=i+1;
end
end
  1 个评论
Adam Danz
Adam Danz 2022-12-20
As the error message indicates, the problem occurs in this line
zk=D*Jx.'*Ex;
where D*Jx.' is a 31x1 and Ex is 126x1 and those two vectors cannot be multiplied.
If you trace these variables back, you'll find that the 31 comes from the number of neurons (10) x 3 + 1
X = randn(1,3*S+1); % where S is the number of neurons
while the 126 comes from your definition of x in your mode: x = -2*pi:0.1:2*pi; which has 126 values.
Without knowing what the function is supposed to do and without having the time to reverse-engineer it, I'm affraid I can't say more. If you intended to compute a 31x126 matrix, then you could transpose Ex and compute
zk=D*Jx.'*Ex.';
however, the next line will break for the same reason - incorrect matrix dimensions. I recommend using debug mode to step through the function line-by-line to make sure each line is fulfilling its intention.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by