MATLAB: Subscripted assignment dimension mismatch error in kinematic analysis of a 1 DOF mechanism

errorMATLABsubscripted assignment dimension mismatch

Greetings.
I have been trying to analyze a 1DOF mechanism by Newton Raphson algorythm but I always get the "Subscripted assignment dimension mismatch." error in line 59. How can I solve this? Thank you for your answers. The whole code is below:
clc,
clear,
% Input - defining physical parameters of mechanism
a2=31;a3=64;a4=35;
% Maximum Iteration
Nmax=100;
% Input - defining initial guess values for th3,th4,tau,s
x=[205*pi/180,66*pi/180,76,57];
% Error Tolerance
xe=0.001*abs(x);
% Input - System inputs (th2, w2, al2)
dth=5*pi/180;
th2=0*pi/180:dth:180*pi/180;
w2=10*ones(1,length(th2));
al2=0*ones(1,length(th2));
%--------------
xe=transpose(abs(xe));
kerr=1; %if kerr=1, results won't be converged
for k=1:1:length(th2);
for n=1:Nmax;
% Assign initial guess values
th3(k)=x(1);th4(k)=x(2);tau(k)=x(3);s(k)=x(4);
% Input - Jacobian Matrix
J=zeros(4,4);
J(1,2)=-a4*sin(th4(k));
J(2,2)=a4*cos(th4(k));J(2,3)=-1;
J(3,1)=-a3*sin(th3(k));J(3,4)=1;
J(4,1)=a3*cos(th3(k));
% Input - f vectors
f=zeros(4,1);
f(1,1)=-(a2*cos(th2(k))+a4*cos(th4(k))+4);
f(2,1)=-(a2*sin(th2(k))+a4*sin(th4(k))-s);
f(3,1)=-(a2*cos(th2(k))+a3*cos(th3(k))+tau);
f(4,1)=-(a2*sin(th2(k))+a3*sin(th3(k)));
%-----------------
eps=inv(J)*f;x=x+transpose(eps);
if abs(eps)<xe;
kerr=0;break
end
end
if kerr==1;
'Error nr';
end
th3(k)=x(1);th4(k)=x(2);
tau5(k)=x(3);tau6(k)=x(4);
% Velocity analysis
fv(1,1)=w2(k)*a2*sin(th2(k));
fv(2,1)=-w2(k)*a2*cos(th2(k));
fv(3,1)=w2(k)*a2*sin(th2(k));
fv(4,1)=-w2(k)*a2*cos(th2(k));
vel=inv(J)*fv;
w3(k)=vel(1);w4(k)=vel(2);
Vtau(k)=vel(3);Vs(k)=vel(4);
%Acceleration analysis
fa(1,1)=al2*a2*sin(th2(k))+w2.^2+a2*cos(th2(k))+w4.^2*a4*cos(th4(k));
fa(2,1)=-al2*a2*cos(th2(k))+w2.^2*a2*sin(th2(k))+w4.^2*a4*sin(th4(k));
fa(3,1)=al2*a2*sin(th2(k))+w2.^2*a2*cos(th2(k))+w3.^2*a3*cos(th3(k));
fa(4,1)=-al2*a2*cos(th2(k))+w2.^2*a2*sin(th2(k))+w3.^2*a3*sin(th3(k));
acc=inv(J)*fa;
al3=acc(1);al4=acc(2);atau=acc(3);as=acc(4);
end
% Angle: Radian to degree
th2d=th2*180/pi;
th3d=th3*180/pi;
th4d=th4*180/pi;
% Plots
figure(1),
subplot(4,3,1),plot(th2d,th3d,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\theta_3 (^o)'),xlim([0 180])
subplot(4,3,2),plot(th2d,w3,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\omega_3 (r/s)'),xlim([0 180])
subplot(4,3,3),plot(th2d,al3,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\alpha_3 (r/s^2)'),xlim([0 180])
subplot(4,3,4),plot(th2d,th4d,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\theta_4 (^o)'),xlim([0 180])
subplot(4,3,5),plot(th2d,w4,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\omega_4 (r/s)'),xlim([0 180])
subplot(4,3,6),plot(th2d,al4,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\alpha_4 (r/s^2)'),xlim([0 180])
subplot(4,3,7),plot(th2d,tau,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\tau_5 (cm)'),xlim([0 180])
subplot(4,3,8),plot(th2d,Vtau,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\v tau_5 (cm/s)'),xlim([0 180])
subplot(4,3,9),plot(th2d,atau,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\a tau_5 (cm/s^2)'),xlim([0 180])
subplot(4,3,10),plot(th2d,s,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\tau_6 (cm)'),xlim([0 180])
subplot(4,3,11),plot(th2d,Vs,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\v tau_6 (cm/s)'),xlim([0 180])
subplot(4,3,12),plot(th2d,as,'r','linewidth',2),xlabel('\theta_2 (^o)'),ylabel('\a tau_6 (cm/s^2)'),xlim([0 180])

Best Answer

You could try initializing fa varible with zeros using "zeros function" before assigning it to the above expression in your code. I think that would remove the dimension mismatch error you are getting.
Try debugging your code and find out the size of Right hand side expression. Then initializing variable fa with proper size would solve your error.