How to avoid dividing by zero in this function?
27 views (last 30 days)
Show older comments
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 Comments
Mathieu NOE
on 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
on 30 Nov 2023
You should probably be storing into the same location in both branches.
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!