Effacer les filtres
Effacer les filtres

How to avoid dividing by zero in this function?

35 vues (au cours des 30 derniers jours)
Matthew Palermo
Matthew Palermo le 20 Nov 2023
Commenté : Walter Roberson le 30 Nov 2023
I fixed a previous issue of constant values and now the challenge is figuring out how to avoid dividing by zero. I figured that simply advacing the time steps would help, but it did not. Any help is much appreciated.
%```
function [T_til1] = fX10B1T0 (x_til, t_til)
lengthx=length(x_til);
lengtht=length(t_til);
T_til1=zeros(lengthx,lengtht); % Preallocating Arrays for speed
% xd = ones(lengthx);
% td = ones(lengtht);
for ix=1:lengthx % Begin time loop
xd_ix=x_til(ix); % Set current time
for it=1:lengtht % Begin space loop
td_it=t_til(it); % Set current space
if td_it == 0 % For time t=0 condition
T_til1(it+1,ix+1)=0; % Set inital temperature
else
% Solution at any time
T_til1(ix,it)=erfc(xd_ix/sqrt(4*td_it));
end % if td_it
end % for ix
end % for it```
  2 commentaires
Mathieu NOE
Mathieu NOE le 20 Nov 2023
I don't see any issue in the posted code
if td_it is >0 there should be no division by zero in T_til1(ix,it)=erfc(xd_ix/sqrt(4*td_it))
Walter Roberson
Walter Roberson le 30 Nov 2023
You should probably be storing into the same location in both branches.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by