How to avoid dividing by zero in this function?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Mathieu NOE
el 20 de Nov. de 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
el 30 de Nov. de 2023
You should probably be storing into the same location in both branches.
Respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!