How do you pass a matrix (parameter) to decic and ode15i?

조회 수: 5 (최근 30일)
Bill Smythe
Bill Smythe 2017년 1월 22일
댓글: Walter Roberson 2017년 1월 23일
Hello,
I am using ode15i, with decic to determine consistent initial conditions for a differential equation that involves a matrix. However, when I call these functions, I want to be able to input my matrix at the same time. That is, everything works perfectly if I have my function fun(t,x,xprime), and solve it using:
[xhat,y]=decic(@fun,0,v,v,w,w), (where v and w are vectors).
[t,x]=ode15i(@fun,[0,1],xhat,y);
But I want to be able to add a matrix input in "fun" (so that it is called with fun(C,t,x,xprime), so I am looking for something like
decic(@fun,C,0,v,v,w,w)
[t,x]=ode15i(@fun,C,[0,1],xhat,y)
where C is some matrix that I have defined earlier in the command window. This syntax does not work though ("not enough input arguments"). How do I input a previously defined matrix into a function within decic/ode15i?
Thanks!

채택된 답변

John Chilleri
John Chilleri 2017년 1월 22일
편집: John Chilleri 2017년 1월 22일
Hello,
Rather than having to include your matrix in the decic/ode15i functions as an input, you could declare your matrix as a global variable, and access it in your function fun.
If you haven't heard of global variables, you can find information here.
Example:
global C
C = some matrix you have;
Call ode15i as normal;
And,
function outputs = fun(t,x)
global C
Use C as needed;
end
Hope this helps!
  댓글 수: 2
Bill Smythe
Bill Smythe 2017년 1월 22일
Very helpful. Worked perfectly. Thank you very much!
Walter Roberson
Walter Roberson 2017년 1월 23일
Ick! Globals are slow, not a good idea for a routine that will have to be called many times. Parameterizing your function is much more efficient.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 1월 23일
Use the techniques described on this documentation page that is linked from the documentation page for ode15i (and the rest of the ODE solvers,)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by