vectorized vs for loop data allocation, they should come out to the same matrix but they do not.
Show older comments
Hello, I am trying to do some numerical methods and while setting up my A matrix, i found something I couldnt figure out. I have the exact same equations leading to the same diagonals (at least they should be) in a vectorized and a for loop method. When I run this code, the two matrices, which should be the same just having different methods of data allocation, differ. I am not sure why as they should have the same values with the exception of the top two and bottom two rows as those are to be used for boundary conditions.Also, the vectorized method should be a sparse matrix due to spdiags fcn. here is the code, the first half is mostly set up, the section " Define Coefficients for Beam Bending" is where the matrix set up begins. so just to be explicitly clear, the matrix 'Mat' and 't' should be the same with the exception of the top and bottom two rows (zeroed out later for boundary conditions) but they differ. Thank you in advance to anyone willing to take their time to look it over.
%% discretize domains
n = 11 ; % Amount of Nodes
% Computational Domain
xi = linspace(0,L,n) ;
dxi = xi(2) - xi(1) ;
% Physical Domain
den = cos((7*pi)/8) - cos(pi/8) ;
term = pi*(((3*xi)/4)+(1/8));
x = (cos(term)-cos(pi/8)) / den ;
% Plotting Domains
% figure(1) ; plot(x,x.*0,'g*-') ; title('Physical Domain)') ;
% xlabel('Length Along Beam (m)') ;
% figure(2) ;plot(xi,xi.*0,'r*-') ; title('Computational Domain') ;
% xlabel('Length Along Beam (m)') ;
%% Define Mapping Function Derivatives
J = ((-3*pi)/4)*(sin(term) / den) ; % First Derivative
J2 = ((-9*pi^2)/16)*(cos(term) / den) ; % Second Derivative
J3 = ((27*pi^3)/64)*(sin(term) / den) ; % Third Derivative
J4 = ((81*pi^4)/256)*(cos(term) / den) ; % Fourth Derivative
%% Define Coefficients for Beam Bending Equation
A = J.^-4 ; % 4th deriv term
B = (-6.*(J.^-5)).*J2 ; % 3rd deriv term
C = ((15.*(J.^-6)).*(J2.^2)) - ((4.*(J.^-5)).*J3) ; % 2nd deriv term
% 1st Derivative Term
D = ((-15.*(J.^-7)).*(J2.^3))+((10.*(J.^-6)).*J2.*J3)-((J.^-5).*J4) ;
%% Set Up Matrix
diags = [ (A.*(1/dxi^4))-(B.*(1/(2*dxi^3)))
(A.*(-4/dxi^4))+(B.*(1/dxi^3))+(C.*(1/dxi^2))-(D*(1/(2*dxi)))
((A).*(6/dxi^4))-(C.*(2/dxi^2))
(A.*(-4/dxi^4))-(B.*(1/dxi^3))+(C.*(1/dxi^2))+(D.*(1/(2*dxi)))
(A.*(1/dxi^4))+(B.*(1/(2*dxi^3))) ]' ;
Mat = spdiags(diags, -2:2, n,n) ;
t = zeros(n,n) ;
for i = 3:n-2
t(i,i-2) = (A(i).*(1/dxi^4))-(B(i).*(1/(2*dxi^3))) ;
t(i,i-1) = (A(i).*(-4/dxi^4))+(B(i).*(1/dxi^3))+(C(i).*(1/dxi^2))-(D(i)*(1/(2*dxi))) ;
t(i,i) = ((A(i)).*(6/dxi^4))-(C(i).*(2/dxi^2)) ;
t(i,i+1) = (A(i).*(-4/dxi^4))-(B(i).*(1/dxi^3))+(C(i).*(1/dxi^2))+(D(i).*(1/(2*dxi))) ;
t(i,i+2) = (A(i).*(1/dxi^4))+(B(i).*(1/(2*dxi^3))) ;
end
1 Comment
Sam Blake
on 14 Mar 2024
Accepted Answer
More Answers (0)
Categories
Find more on Eigenvalue Problems 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!