Division by zero error

8 ビュー (過去 30 日間)
Elton Sim
Elton Sim 2020 年 9 月 30 日
コメント済み: Elton Sim 2020 年 9 月 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
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 9 月 30 日
MATLAB code?

サインインしてコメントする。

採用された回答

Walter Roberson
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 件のコメント
Elton Sim
Elton Sim 2020 年 9 月 30 日
Hi Walter, thanks for this. It worked!

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
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 件のコメント
Elton Sim
Elton Sim 2020 年 9 月 30 日
Hi Walter, I can't do that because although it resolves the problem, the results will not be the one I want. There must be cases where i=j for my numerical solution.
Ameer Hamza
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by