Function may not have a root
10 views (last 30 days)
Show older comments
Matthew Palermo
on 20 Nov 2023
Commented: Walter Roberson
on 20 Nov 2023
I am getting this eror with this code and associated functions:
Exiting fzero: aborting search for an interval containing a sign change
because no sign change is detected during search.
Function may not have a root.
Here is the code and functions:
close all
clear all
clc
% %FOR A = 3
%
x_til = linspace(0,1,10);
dt = 0.1;
tmax = 1;
t_til = linspace(dt,tmax,10);
nt = length(t_til);
nx = length(x_til);
x0 = [0.0000001 1];
tp1 = ones(nt);
tp2 = zeros(nt);
for A = 3 %finding tp actual
for jx = 1:nx
Tfun1 = @(tp) fX10B1T0(x_til(jx),tp);
Tfun1A = @(tp) (1*(10^(-A))) - Tfun1(tp);
tp1(jx) = fzero(Tfun1A,x0(1));
end
for jx = 1:nx
Tfun2 = @(tp) fX11B10T0(x_til(jx),tp,A);
Tfun2A = @(tp) (1*(10^(-A))) - Tfun2(tp);
tp2(jx) = fzero(Tfun2A,x0(1));
end
end
for ix = 1:nx
for it = 1:nt
TX10B1T0 = fX10B1T0(x_til,t_til);
end
plot (x_til, TX10B1T0(ix,:));
end
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 = zeros(lengthx);
td = zeros(lengtht);
for ix=1:lengthx % Begin time loop
xd_ix=xd(ix); % Set current time
for it=1:lengtht % Begin space loop
td_it=td(it); % Set current space
if td_it == 0 % For time t=0 condition
T_til1(it,ix)=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
end % function
function [T_til2] = fX11B10T0 (t_til, x_til,A)
lengthx=length(x_til);
lengtht=length(t_til);
T_til2=zeros(lengtht,lengthx); % Preallocating Arrays for speed
for it=1:lengtht % Begin time loop
td_it=t_til(it); % Set current time
for ix=1:lengthx % Begin space loop
xd_ix=x_til(ix); % Set current space
td_dev1=(1/(10*A))*(2-xd_ix)^2; % First deviation time
td_dev2=(1/(10*A))*(2+xd_ix)^2; % Second deviation time
if td_it == 0 % For time t=0 condition
T_til2(it,ix)=0; % Set inital temperature
elseif td_it <= td_dev1 % Solution for first small times:
T_til2(it,ix)=erfc(xd_ix/sqrt(4*td_it));
elseif td_it > td_dev1 && td_it <= td_dev2 % Solution for second small times:
T_til2(it,ix)=erfc(xd_ix/sqrt(4*td_it))-erfc((2-xd_ix)/sqrt(4*td_it));
else
m_max=ceil(sqrt(A*log(10)/(td_it*pi^2))); % Set max. No. terms
% Start X11B10T0 case for large times:
T_til2(it,ix)=1-xd_ix; % steady-state temperature solution
for m=1:m_max % Continue X11B10T0 case for large times
% Series solutions:
betam=m*pi; % Define eigenvalues
T_til2(it,ix)=T_til2(it,ix)-2*exp(-betam^2*td_it)*sin(betam*xd_ix)/betam;
end % for m
end % if td_it
if xd_ix == 1 % For location x=1 condition
T_til2(it,ix)=0; % Set x=1 boundary temperature
end % if xd_ix
end % for ix
end % for it
end % function
0 Comments
Accepted Answer
Walter Roberson
on 20 Nov 2023
td = zeros(lengtht);
You initialize td to all zeros
for ix=1:lengthx % Begin time loop
xd_ix=xd(ix); % Set current time
for it=1:lengtht % Begin space loop
td_it=td(it); % Set current space
You extract a value from td. But td is all zero. And you never change td anywhere in the function after you initialize it to zero.
if td_it == 0 % For time t=0 condition
T_til1(it,ix)=0; % Set inital temperature
But it does indeed equal 0 because td remains 0. So you are always setting all of T_til1 to 0.
Your fX10B1T0 function returns constant values. There is no possibility of finding a sign change in a function that is constant.
2 Comments
Walter Roberson
on 20 Nov 2023
In your code, your time is always zero. You set td = zeros(lengtht) and you never change it. So the td_it == 0 is always true, and none of the elseif/else are executed.
You do get
if xd_ix == 1 % For location x=1 condition
T_til2(it,ix)=0; % Set x=1 boundary temperature
end % if xd_ix
executed in some meaningful way... but since at time 0 T_til2 entries are set to 0, setting one or more entries to 0 at this point makes no difference to the outcome.
Therefore, no matter what your inputs are, your T_til2 array is going to be all 0. And a function that is all zero does not have a zero crossing.
More Answers (0)
See Also
Categories
Find more on Particle & Nuclear Physics 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!