How to avoid dividing by zero in this function?

조회 수: 9 (최근 30일)
Matthew Palermo
Matthew Palermo 2023년 11월 20일
댓글: Walter Roberson 2023년 11월 30일
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
Mathieu NOE
Mathieu NOE 2023년 11월 20일
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 2023년 11월 30일
You should probably be storing into the same location in both branches.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by