Index exceeds matrix dimensions.

9 ビュー (過去 30 日間)
Lenilein
Lenilein 2019 年 1 月 16 日
コメント済み: Lenilein 2019 年 1 月 16 日
Dear All,
Can someone enlighten me by explaining why I'm getting following error message in code below?
Index exceeds matrix dimensions. Error in verysimple3 (line 14) [Tp]=ode45(@(l,Tp) myODE(l,Tp,Aap,Acp),Pool(k:k+1),y0);
If I add the parameters Aap,Acp as input parameters of my anonymous function then I get: Not enough input arguments. Error in verysimple3>@(l,Tp,Aap,Acp)myODE(l,Tp,Aap,Acp)
Sorry for this question with a probably very obvious answer! Originally I didn't want to include Aap and Acp as input parameters for myODE function as these are fixed for each interval (see loop) but I'm not sure to have another option in this case...
a =2;
b =1;
Pool = [0,a,b];
y0=30;
for k=1:numel(Pool)
switch k
case rem(k,2)==1
Aap=a; Acp=Aap;
otherwise
Aap=2*b; Acp=0;
end
[l,Tp]=ode45(@(l,Tp) myODE(l,Tp,Aap,Acp),Pool(k:k+1),y0);
y0=y(end,1);
end
plot(l,Tp(:,:));
function dTpdl=myODE(l,Tp,Aap,Acp)
dTpdl=(140-Tp)*Acp+(100-Tp)*Aap-Aap/(Tp+273);
end
  2 件のコメント
Torsten
Torsten 2019 年 1 月 16 日
編集済み: Torsten 2019 年 1 月 16 日
For k=3, you refer to Pool(3:4) which does not exist.
Further, a<b is required which is not the case in your code.
Further, y(end,1) has to be replaced by Tp(end,1).
Further, Tp is always overwritten in the for-loop when k is increased.
Lenilein
Lenilein 2019 年 1 月 16 日
Fantastic, thank you very much!!
I fixed the code and found in another post that I can use the concatenate function to store the results from each run... not exactly sure why that function can be used for this purpose but all in all I get the results that I expected!
a =1;
b =2;
Pool = [0,a,b];
Tp0=30;
lAll=[];
TpAll=[];
for k=1:numel(Pool)-1
switch k
case rem(k,2)==1
Aap=a; Acp=Aap;
otherwise
Aap=2*b; Acp=0;
end
[l,Tp]=ode45(@(l,Tp) myODE(l,Tp,Aap,Acp),Pool(k:k+1),Tp0);
lAll=cat(1,lAll,l);
TpAll=cat(1,TpAll,Tp);
Tp0=Tp(end,1);
end
plot(lAll,TpAll(:,:));
function dTpdl=myODE(l,Tp,Aap,Acp)
dTpdl=(140-Tp)*Acp+(100-Tp)*Aap-Aap/(Tp+273);
end

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 1 月 16 日
編集済み: Jan 2019 年 1 月 16 日
tPool = [0,1,2,4,8];
...
for k = 1:numel(tPool) - 1
% ^^^
Concerning: "If I add the parameters Aap,Acp as input parameters of my anonymous function then I get: Not enough input arguments. Error in verysimple3>@(l,Tp,Aap,Acp)myODE(l,Tp,Aap,Acp)"
The ODE integrator calls the function to be integrated with 2 input arguments. The anonymous function is created to add further inputs. Therefore this works:
[l, Tp]=ode45(@(l,Tp) myODE(l,Tp,Aap,Acp),Pool(k:k+1),y0);
because here ODE45 provides the 2 inputs (l, Tp), the anonymous function appends 2 further inputs. With:
[l, Tp]=ode45(@(l,Tp,Aap,Acp) myODE(l,Tp,Aap,Acp), Pool(k:k+1), y0);
ODE45 is instructed to provide 4 inputs, but it can offer 2 only.
By the way, using "l" (lowercase L) as variable causes troubles freuqently, when it is confused with a "1" (one) or "I" (uppercase i).
  1 件のコメント
Lenilein
Lenilein 2019 年 1 月 16 日
Thank you for your patience, yes this is clear now! I'm understanding more and more, step by step, day by day :)
You're right I'll see that I stop using "l"!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by