How can I fix "Error in sym/subsref (line 898) R_tilde = builtin('s​ubsref',L_​tilde,Idx)​; ?

22 views (last 30 days)
Error is also stated as being in line 43;
Error in Assignment2CodeQuadratic2 (line 43)
KgQ(indiciesQ,indiciesQ) = KgQ(indiciesQ,indiciesQ) + subs(KeQ,[x1Q x2Q x3Q], [xQ(i*2-1)
xQ(i*2) xQ(i*2+1)]); % Ke into Kg contribution
% ENME302-22S2, 21 Sep 2022, linear finite elements demonstration
% Derives the element equations using linear shape functions.
clear;close all;clc;
% % Quadratic shape element:
syms xQ x1Q x2Q x3Q u1Q u2Q u3Q a b c
W = 0.01; % Width [m]
tau0 = 1; % Initial Stiffness [Pa]
tau = (1+0.9*sin((sqrt(2)*pi*xQ)/W))*tau0; % Stiffness Equation
f = 100; % Force Density (N/m^3)
uQ = a*xQ^2 + b*xQ + c; % interpolation function (quadratic here) from Equation 8.16
eqn1Q = subs(uQ,xQ,x1Q) == u1Q; % evaluating at left node, Equation 8.18a
eqn2Q = subs(uQ,xQ,x2Q) == u2Q; % evaluating at left node, Equation 8.18b
eqn3Q = subs(uQ,xQ,x3Q) == u3Q; % evaluating at left node, Equation 8.18c
consQ = solve([eqn1Q eqn2Q eqn3Q], [a b c]); % solve simultaneous equations for constants a, b and c, Equation 8.19
uQ = subs(uQ,[a b c],[consQ.a consQ.b consQ.c]); % substitute/plug into interpolation function, Equation 8.20a
[NQ,~] = coeffs(uQ, [u1Q u2Q u3Q]); % collect coefficients for defining shape functions, Equation 8.20b
N1Q=NQ(1); % N1 = 2*xi^2 - 3*xi + 1;
N2Q=NQ(2); % N2 = -4*xi^2 + 4*xi;
N3Q=NQ(3); % N3 = 2*xi^2 - xi;
% Element equations, Ke*Te=Fe (symbolic):
KeQ = sym(zeros(3)); % element stiffness matrix
FeQ = sym(zeros(3,1)); % element rhs forcing vector
for i = 1:3 % 3 dof for quadratic elements
eleqQ = int(tau*diff(uQ,xQ)*diff(NQ(i),xQ),xQ,x1Q,x3Q) - int(f*NQ(i),xQ,x1Q,x3Q);
[coefQ,~] = coeffs(eleqQ, [u1Q u2Q u3Q]); % collect coefficients
KeQ(i,:) = coefQ(1:3); % coefficients of u1 and u2 and u3
FeQ(i) = -coefQ(4); % remaining terms (not a coefficient of u1, u2 or u3 )
end
for kk = 2:4:22
% Global equations, Kg*Tg=Fg (numeric):
L = W; % length (mm)
noe = kk; % number of elements
dof = noe+1; % number of degrees of freedom
x = linspace(0,L,dof); % x-coordinates of dof
KgQ = sym(zeros(dof)); % global stiffness matrix
FgQ = sym(zeros(dof,1)); % global rhs forcing vector
for i=1:noe % assemble each set of element equations
indiciesQ = i*2-1:i*2+1;
KgQ(indiciesQ,indiciesQ) = KgQ(indiciesQ,indiciesQ) + subs(KeQ,[x1Q x2Q x3Q], [xQ(i*2-1) xQ(i*2) xQ(i*2+1)]); % Ke into Kg contribution
FgQ(indiciesQ) = FgQ(indiciesQ) + subs(FeQ,[x1Q x2Q x3Q], [xQ(i*2-1) xQ(i*2) xQ(i*2+1)]); % Fe into Fg contribution
end
% Final equations with boundary conditions, K*Tbc=F (numeric):
Ta = 0; % Dirichlet boundary condition on lhs
FgQ(1) = FgQ(1) - KgQ(1,1)*Ta;
KgQ(1,1) = 1;
FgQ(2) = FgQ(2) - KgQ(2,1)*Ta;
KgQ(2,1) = 0;
KgQ(3,1) = 0;
Tb = 0; % Dirichlet boundary condition on rhs
FgQ(dof) = FgQ(dof) - KgQ(dof,dof)*Tb;
KgQ(dof,dof) = -1;
FgQ(dof-1) = FgQ(dof-1) - KgQ(dof-1,dof)*Tb;
KgQ(dof-1,dof) = 0;
KgQ(dof-2,dof) = 0;
KgQ = double(KgQ);
FgQ = double(FgQ);
% Solve and plot solution:
TbcQ = KgQ\FgQ; % unknown variables, in this case, we have calculated dT/dx|x1 and -dT/dx|x5
TgQ = [Ta;TbcQ(2:dof-1);Tb]; % global vector of displacement values, i.e. replaced for our Dirichlet b.c.s
%Finding Displacement at Middle for each number of elements
loc = find(x==0.005);
displacement(kk) = Tg(loc)*10^3;
end
Index exceeds the number of array elements. Index must not exceed 1.

Error in indexing (line 1079)
R_tilde = builtin('subsref',L_tilde,Idx);
%Displacement Array
centreDisplacement = displacement(2:4:22) % Removes Zeros from Array
%Relative Error
relError(kk) = abs((centreDisplacement(end)-centreDisplacement(kk))/centreDisplacement(kk))*100;
plot(x*10^3,Tg,'k.-'); xlabel('x (mm)'); ylabel('Deflection (m)');
title("Linear Shape Function Displacement")
legend("Linear");

Answers (2)

Torsten
Torsten on 2 Oct 2022
KgQ(indiciesQ,indiciesQ) = KgQ(indiciesQ,indiciesQ) + subs(KeQ,[x1Q x2Q x3Q], [xQ(i*2-1) xQ(i*2) xQ(i*2+1)]); % Ke into Kg contribution
FgQ(indiciesQ) = FgQ(indiciesQ) + subs(FeQ,[x1Q x2Q x3Q], [xQ(i*2-1) xQ(i*2) xQ(i*2+1)]); % Fe into Fg contribution
xQ is a symbolic scalar variable. But you address array elements of xQ (xQ(i*2-1) xQ(i*2) xQ(i*2+1)) which do not exist.
  3 Comments
Torsten
Torsten on 2 Oct 2022
Edited: Torsten on 2 Oct 2022
Then you assume that xQ has dimension at least 2*noe+1 because i runs from 1 to noe. But xQ = linspace(0,L,dof) generates an array with only noe+1 elements.
And if you define xQ = linspace(0,L,dof), you should rename xQ in the list of symbolic variables.

Sign in to comment.


Star Strider
Star Strider on 2 Oct 2022
Perhaps —
syms xQ
xQ = sym('xQ',[1 3])
xQ = 
xQ2 = xQ(2)
xQ2 = 
See the documentation on sym for details.

Categories

Find more on General Applications in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!