Borrar filtros
Borrar filtros

Unrecognized function or variable for a 'for' loop.

10 visualizaciones (últimos 30 días)
Matthew Palermo
Matthew Palermo el 20 de Nov. de 2023
Comentada: Matthew Palermo el 20 de Nov. de 2023
I am trying to compare the penetratino time between two functions. The code was working fine and plotting until I realized that I had made a syntax mistake in the function X10B1T0. I went in and corrected it (swapping the positions of variables), and now the code is not recognizing the "ix". I have deduced that the problem is somewhere in this section of code, but have no idea what it could be.
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 ix = 1:10
Tfun1 = @(tp) fX10B1T0(x_til(ix),tp);
Tfun1A = @(tp) (1*(10^(-A))) - Tfun1(tp);
tp1(ix) = fzero(Tfun1A,x0);
end
for ix = 1:nx
Tfun2 = @(tp) fX11B10T0(x_til(ix),tp,A);
Tfun2A = @(tp) (1*(10^(-A))) - Tfun2(tp);
tp2(ix) = fzero(Tfun2A,x0(1));
end
end
Error using fzero>localFirstFcnEvalError
FZERO cannot continue because user-supplied function_handle ==> @(tp)(1*(10^(-A)))-Tfun1(tp) failed with the error below.

Unrecognized function or variable 'ix'.

Error in fzero (line 231)
localFirstFcnEvalError(FunFcn,FunFcnIn,ME);
for ix = 1:nx
for it = 1:nt
TX10B1T0 = fX10B1T0(x_til,t_til);
end
plot (x_til, TX10B1T0(ix,:));
end
Also, here are the functions:
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
for it=1:lengtht % Begin time loop
xd_ix=xd(ix); % Set current time
for ix=1:lengthx % 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

Respuestas (1)

Walter Roberson
Walter Roberson el 20 de Nov. de 2023
for A = 3 %finding tp actual
for ix = 1:10
Tfun1 = @(tp) fX10B1T0(x_til(ix),tp);
Tfun1 involves using the current ix value as an index into x_til, but does not involve passing ix to FX10B1T0 -- the element of x_til referenced by ix is passed to that function, but not ix itself.
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
for it=1:lengtht % Begin time loop
xd_ix=xd(ix); % Set current time
inside the function, the current x_til value is received as x_til, and the tp value is received as t_til . You start executing.. and you get to the line xd_ix=xd(ix) which needs ix which has not been passed in to this function.
Context would suggest that xd_ix = xd(it); instead.
  1 comentario
Matthew Palermo
Matthew Palermo el 20 de Nov. de 2023
That worked! I also ended up making sure the for loop variable was correct so that it reads:
for ix = 1:lengthx
xd_ix = xd(ix);
Thanks

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by