1

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(!)

Community
  • 1
  • 1
Sergio Haram
  • 437
  • 4
  • 17
  • are those x1, x2, x3 the same? or contain at least a lot of similar elements? And why can't you do them seperately? At least it seems as if you had a 1d system which is interpolated for 3 different functions – The Minion Oct 07 '14 at 08:36
  • Hi @TheMinion `x1,x2,x3` can be chosen freely, this will ofcourse affect, as you see, the output of the function-values `fx1,fx2,fx3`. The reason I can not solve them separately is because `fx1,fx2,fx3` represent a (3x3) system of `odes` where the **variables** are dependent on each other. `dx1dt = X1^a*X2^b*X3^c; dx2dt = X1^g*X2^f*X3^h; dx3dt = X1^p*X2^q*X3^r;`. The functions `fx1,fx2,fx3` are the right-hand of the equations of the (3x3) system. – Sergio Haram Oct 07 '14 at 08:57
  • What are the dimensions of `x1`, `x2`, `x3`? If each function `f1`, `f2`, `f3` maps a three-dimensional vector to a point, then it seems as though each `fi` should be `1xN` and each `xi` should be `3xN`. Is that correct? – Chris Taylor Oct 07 '14 at 09:04
  • Maybe I just don't see it, but isn't it completely unrelated how x1,x2,x3 effect each another? You have solutions for your functions fxi at specific points and you just want to interpolate those. How those xi-values are computed shouldn't really matter in my opinion. – The Minion Oct 07 '14 at 09:08
  • @ChrisTaylor the dimension of `x1,x2,x3` is the same as the dimension of `fx1,fx2,fx3` Example: if `x1` is `50x1` (I evaluate the function `fx1` at 50 points) then `fx1` is `50x1` and if I interpolate these two vectors I will have a `50x1` vector. May be is easier to imagine `x1` as points along the x-axis and `fx1` will be the function-values along the y-axis. – Sergio Haram Oct 07 '14 at 09:10
  • @TheMinion You are correct! x1,x2,x3 don't affect each other!!! and How these values are computed don't matter. What it does matter is that after I have 3 interpolated functions I need to solved them as a system of odes. This means that I have to send them all three at the same time to **ode45** – Sergio Haram Oct 07 '14 at 09:13
  • @SergioHaram It seems we have to different points of view and one should be wrong/uncomplete. I am afraid it is mine so maybe you can enlighten me. If I understand you correctly you have 3 functions fxi and each of them depends on ONE argument array the x-values. Yet those 3 x-values are computed by a differential equation which takes all 3 x-values into account. So you have fx1(x1), fx2(x2), fx3(x3) and those xi as you wrote in your first comment. Yet because you have allready computed x1,x2,x3 I just don't understand why you would need to pass ALL xi to the `interp1()`. [to be continued] – The Minion Oct 07 '14 at 09:44
  • after all you want to change those fxi(xi) to be continous and not discrete with more elements. So why you need to take the specific dx1/dt into account when you can just use one existing pair of data sets. – The Minion Oct 07 '14 at 09:45

1 Answers1

2

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);
Sergio Haram
  • 437
  • 4
  • 17
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
  • @CrisTaylor I don't know if you have misunderstood my question completely, because I need something like you have posted! I need to collect my interpolated functions in only one function to be able to send it to ode45. Anyways, I can not create a function inside my editor, when I am trying to do it I get the error: Function definitions are not permitted in this context. – Sergio Haram Oct 07 '14 at 09:32
  • 2
    @SergioHaram You need to create a new file (`edit function_helper.m`) and save the function definition there. You will be then be able to call it with `function_helper(args)` in your command window. – Chris Taylor Oct 07 '14 at 09:52