简体   繁体   English

输入矩阵到ode45的功能文件

[英]Input matrix to function file of ode45

I have a code(which requires a lot input be given by the user) which will give me anxn matrix(say A), which I have to use to solve a system of ODEs X'=AX. 我有一个代码(要求用户提供大量输入),这将给我一个焦虑矩阵(比如A),我必须使用它来求解ODE X'= AX的系统。 How do I include this matrix A in the function file(.m file) of ode45. 如何在ode45的函数文件(.m文件)中包含此矩阵A。 If I include the code in the function file as below: 如果我将代码包含在功能文件中,如下所示:

function xp=g_test(t,x);
k=input('something');
A=some manipulation of inputs;
xp=A*x;
end

Matlab asks for input at each timestep(typically my problem has 30k timesteps). Matlab在每个时间步都要求输入(通常我的问题有30k个时间步)。 So how do I include/ pass the matrix A to the function? 那么,如何将矩阵A包含/传递给函数?

You can create a function that returns a function handle : 您可以创建一个返回函数句柄函数

function odeFcn = makeODE(a,b,c)  
    A = some_function(a, b, c);
    odeFcn = @(t,x) A*x;
end

Now you can call ode45 with input matrices a, b, c : 现在,您可以使用输入矩阵a, b, c调用ode45

outputVector = ode45(makeODE(a,b,c), [t0, t1], x0);

The inspiration is taken from gnovice's answer here . 灵感来自gnovice的回答采取这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM