Solving a system of ODEs where the functions are given discrete (matlab)

北慕城南 提交于 2020-01-06 14:43:26

问题


My question this time is base on an old question I have asked for some months ago (see HERE) If you don't want to go through my first question I can give a short overview about that problem.

In my first question I had two vectors, the first one fx containing function values:

fx = [0.5644 0.6473 0.7258 0.7999 0.8697 0.9353 0.9967 1.0540 1.1072 1.1564 ...
      1.2016 1.2429 1.2803 1.3138 1.3435 1.3695 1.3917 1.4102 1.4250 1.4362 ...
      1.4438 1.4477 1.4482 1.4450 1.4384 1.4283 1.4147 1.3977 1.3773 1.3535 ...
      1.3263 1.2957 1.2618 1.2246 1.1841 1.1403 1.0932 1.0429 0.9893 0.9325 0.8725];

the second one x containing the points where the function was evaluated:

x = 0:0.25:10

This discrete function fx is an ode and I needed to solved it by means of ode45 from matlab. but ode45 doesn't take discrete functions, so the solution was to interpolate these two vectors. Then I could have a function-handle which I could send to ode45, like this:

f = @(xq)interp1(x,fx,xq);
tspan = [0 1];
x0 = 2;
xout = ode45(@(t,x)f(x),tspan,x0);

My problem now:

This time I don't have only one equation representing one ode, I have a system of odes, but, like before the functions are given discrete. This means that I have:

fx1 = [....function values...]
x1 = [...the points where the function fx1 was evaluated...]

fx2 = [....function values...]
x2 = [...the points where the function fx2 was evaluated...]

fx3 = [....function values...]
x3 = [...the points where the function fx3 was evaluated...] 

And I need to be able to solve this system of odes with ode45. But I cannot simply interpolate each equation at the time and send it to ode45, this will be wrong. I need to send the hole system to ode45.

I have tried different things, but my programming skill just don't stretch so long that I can solve this problem, this is the reason because I am asking for help!

My guess-solution

I believe that if I use a for-loop to make only one function handle containing the 3 interpolated equations of the system, I could send this one function-handle to ode45. Does this sounds like a good alternative?

PS: I can gladly post the values of the vectors fx1,fx2,fx3,x1,x2,x3 if needed(!)


回答1:


Define this function and save it in function_helper.m

function result = function_helper(f1, x1, f2, x2, f3, x3, xq)
    result = [
        interp1(x1, f1, xq(1))
        interp1(x2, f2, xq(2))
        interp1(x3, f3, xq(3))
    ];
end

and then integrate like this

f = @(t,xq) function_helper(f1, x1, f2, x2, f3, x3, xq);
tspan = [0 1];
x0 = 1;
xout = ode45(f, tspan, x0);


来源:https://stackoverflow.com/questions/26231703/solving-a-system-of-odes-where-the-functions-are-given-discrete-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!