Clear Filters
Clear Filters

Why do I get "Array indices must be positive integers or logical values"

3 views (last 30 days)
I am trying to solve the following differential equation ode3. Tc_p is a cfit that I change to a symfun so I can susbtitute inside my differential equation and solve the whole thing. I can`t understand why I am getting the error "Array indices must be positive integers or logical values.". Thx in advanace.
clear all
datos_TH;
syms Tfav(t) Tcav(t) Tm(t)
cond1=Tfav(0)==(Tf0-Tm0)/(Tf0-Tm0);
cond2=Tcav(0)==(Tc0-Tm0)/(Tf0-Tm0);
%cond3=Tm(0)==Tm0;
conds=[cond1; cond2];
ode1=diff(Tfav)==-2*Bigf*(Tfav-Tcav)/Rf0 + 100;
ode2=diff(Tcav)==2*K*(Bigc*Rci*(Tfav-Tcav)-Bi(1)*Tcav)/(1-Rci^2);
%ode3=diff(Tm)==(Sc*hi(1)*(Tc-Tm)-2*m*cp*(Tm-Tm0))/(Mc*cp);
odes=[ode1;ode2];
[TcavSol(t), TfavSol(t)]=dsolve(odes, conds);
Tf=[];
Tc=[];
n=1;
t_0=25;
time_ad=t_0*kf/(Rhof*cf*rc0^2);
for i=0:0.01:time_ad
Tf(n)=double(TfavSol(i))*(Tf0-Tm0) + Tm0;
Tc(n)=double(TcavSol(i))*(Tf0-Tm0) + Tm0;
n=n+1;
m=n+1;
end
%Moderator Temperature ODE
save('Tc','Tc');
t=linspace(0,t_0,m-2);
t=transpose(t);
Tc=transpose(Tc);
Tc_p=fit(t,Tc,'poly8');
Tc_p=subs(str2sym(formula(Tc_p)),coeffnames(Tc_p),num2cell(coeffvalues(Tc_p).'));
Tc_p=subs(Tc_p,'t');
cond3=Tm(0)==Tm0;
ode3=diff(Tm,'t')==(Sc*hi(1)*(Tc_p-Tm)-2*m*cp*(Tm-Tm0))/(Mc*cp);
odes=ode3;
TmSol(t)=dsolve(ode3,cond3);
And the complete error is:
Array indices must be positive integers or logical values.
Error in sym/privsubsasgn (line 1311)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in indexing (line 1142)
C = privsubsasgn(L,R,inds{:});
Error in Sim_TH (line 41)
TmSol(t)=dsolve(ode3,cond3);

Accepted Answer

Steven Lord
Steven Lord on 27 Apr 2023
You use Tm in your code but nowhere do you define it. My suspicion is that you've defined it as a numeric vector like you did Tf and Tc. But vector indices cannot be 0.
  3 Comments
Steven Lord
Steven Lord on 27 Apr 2023
t=linspace(0,t_0,m-2);
% snip intervening lines of code
TmSol(t)=dsolve(ode3,cond3);
The first element of t is 0. 0 is not a valid index into an array in MATLAB. Remove the indexing on that last line.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 27 Apr 2023

Tags

Community Treasure Hunt

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

Start Hunting!