Division by zero error

9 visualizzazioni (ultimi 30 giorni)
Elton Sim
Elton Sim il 30 Set 2020
Commentato: Elton Sim il 30 Set 2020
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

Risposta accettata

Walter Roberson
Walter Roberson il 30 Set 2020
%Mc(si,sj) = subs (sigma,{i,j},{si,sj});
t1 = limit(sigma, i, si);
t2 = limit(t1, j, sj);
Mc(si, sj) = t2
  1 Commento
Elton Sim
Elton Sim il 30 Set 2020
Hi Walter, thanks for this. It worked!

Accedi per commentare.

Più risposte (1)

Ameer Hamza
Ameer Hamza il 30 Set 2020
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 Commenti
Elton Sim
Elton Sim il 30 Set 2020
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 il 30 Set 2020
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

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by