Division by zero error
조회 수: 8 (최근 30일)
이전 댓글 표시
I have the following functions in which I am doing a Ritz Method analysis for a tapered cantilever beam, under clamped free conditions.
I am repeatedly getting a zero division error even though I have checked that there are no zeros in the denominators.
Can I know how do I fix this?
I have attached the file code and pasted the lines for reference.
Thanks in advance
>> %Constants
E = 2*10^12; %Young's Modulus
rho = 300; %Density
L = 29; %Length
syms i j y %Declare i, j, y variables
%Variable Equations
o = y/L; %Position Ratios
A = 4.1*(1.005-o); %Area
I = 0.03*(1.005-0.5*o-0.5*o^2); %Moment of Inertia
%Miscellenous
n = 7; %Iterations
%Basis Functions Cosine
phis_i = cos((i-3)*pi*o);
phis_j = cos((j-3)*pi*o);
%For Mc Matrix
sig_fun = rho*A*phis_i*phis_j; %Function for integral a
sigma = int (sig_fun, y, 0, L); %Integral a for y from 0 to L
%Matrices Cosine Series
Mc = zeros (n);
for si = 1:n
for sj = 1:n
Mc(si,sj) = subs (sigma,{i,j},{si,sj});
end
end
채택된 답변
Walter Roberson
2020년 9월 30일
%Mc(si,sj) = subs (sigma,{i,j},{si,sj});
t1 = limit(sigma, i, si);
t2 = limit(t1, j, sj);
Mc(si, sj) = t2
추가 답변 (1개)
Ameer Hamza
2020년 9월 30일
Division by zero occurs in your equation. Following image show expression for sigma from the live editor

As you can see, there are several conditions when 0 can come in the denominator. One such condition is i==j. Therefore, when the loop starts, you have i=1, j=1, and i-j=0, and MATLAB throws an error.
댓글 수: 4
Ameer Hamza
2020년 9월 30일
It is just one case. You will also get zero in the denominator at j=3, and also at i+j-6=0. I don't think there is an easy way to avoid this other than adding all these conditions in for-loop. For example,
for si = 1:n
for sj = 1:n
if ~((i==j) | (j==3) | ((i+j-6)==0))
Mc(si, sj) = subs(sigma, {i,j}, {si,sj});
end
end
end
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!