MATLAB Answers

Not sure why vector changes assignment when using ode45

3 ビュー (過去 30 日間)
3816R 2019 年 11 月 30 日
Commented: Steven Lord 2019 年 12 月 1 日
I am going to simplify the problem out of context as I believe this will be easier to understand, obviously I will be corrected if this is actually unhelpful.
There's 3 m files. Main, secondfun, thirdfun.
In main I have defined an 8 element vector i.e.
vector=[var1 var2 var3 var4 var5 var6 var7 var8]
In main I then call the second function with vector as one of the inputs i.e.
[out1, out2]=secondfun(in1, in2, in3, vector)
Where secondfun m file has secondfun(x,y,z,vector):
and thirdfun, thirdfun(~,vector) has:
out3(1)=(vector(5)-vector(1))/vector(7); *****
So running this I get the error that on the second line in thirdfun: Index exceeds matrix dimensions.
I narrowed this error down to where I'm using ode45 in secondfun. Before that line the vector function is a 8 element row vector as it should be. After that line it's turned into y0 only (3 element column vector), i.e. it's being passed into secondfun fine as far as I understand but I don't understand where or how it's being redefined as that?
Any advice on this would be much appreciated.

  0 件のコメント

サインイン to comment.


Steven Lord
Steven Lord 2019 年 11 月 30 日
This isn't the right way to pass a third input (beyond the two that ode45 passes into the integrand function automatically) into thirdfun, which is what I assume is your intention. Instead vector is being interpreted as the options input to ode45.
There's an example on the ode45 documentation page showing one technique for passing additional parameters into the integrand function and a link (in the description of the odefun input on the documentation page) that describes a few other techniques.

  3 件のコメント

3816R 2019 年 11 月 30 日
Ah! Okay! So I did see the "Pass Extra Parameters to ODE Function" subheading but got confused by their @(t,y) and the fact A and B were constants. But I think this could work.
[t,y] = ode45(@(t,y) odefcn(t,y,A,B), tspan, y0);
So I've tried
[~,out3]=ode45(@(~,out3) thirdfun(vector),t,y0);
But the same issue occurs.
Edit: whilst the same issue occurs, the error is actually now different instead of 'exceeds matrix dimensions' it errors on line 2 of thirdfun saying there is 'not enough input arguments'.
3816R 2019 年 11 月 30 日
I think I've sorted out this issue I now - thank you!!
Steven Lord
Steven Lord 2019 年 12 月 1 日
That call would work if the only thing thirdfun accepted was the vector. In this case, the anonymous function works like an adapter. ode45 requires the function you pass into it as the first input to accept two inputs, the vector of times and the vector of state values. thirdfun requires the function that calls it to pass in (I'm guessing) the vector of state values and the vector of additional parameters. The anonymous function:
@(t, y) thirdfun(y, vector)
could be called by ode45 with two inputs (as it requires) and calls thirdfun with two inputs (as it requires.) It ignores the information ode45 passed in that thirdfun doesn't need and provides extra information to thirdfun above and beyond what ode45 gave it.

サインイン to comment.

More Answers (1)

Jim Riggs
Jim Riggs 2019 年 11 月 30 日
I dont think that the statement
is a valid statement. It may be that out3 is not the size that you intend.
out3 = zeros(3,1);

  3 件のコメント

3816R 2019 年 11 月 30 日
woops! sorry, yes I do have it as zeros in my code I just missed it rewriting it out on here.
darova 2019 年 11 月 30 日
Can you accept the answer?
3816R 2019 年 11 月 30 日
this wasn't the answer - I already had the zeros in my code. The issue is seemingly with ode45.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by