1

I have the following ODE:

x_dot = 3*x.^0.5-2*x.^1.5  % (Equation 1)

I am using ode45 to solve it. My solution is given as a vector of dim(k x 1) (usually k = 41, which is given by the tspan).

On the other hand, I have made a model that approximates the model from (1), but in order to compare how accurate this second model is, I want to solve it (solve the second ODE) by means of ode45. My problem is that this second ode is given discrete:

x_dot = f(x) % (Equation 2)

f is discrete and not a continuous function like in (1). The values I have for f are:

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

What I want now is to solve this second ode using ode45. Hopefully I will get a solution very similar that the one from (1). How can I solve a discrete ode applying ode45? Is it possible to use ode45? Otherwise I can use Runge-Kutta but I want to be fair comparing the two methods, which means that I have to solve them by the same way.

1 Answer 1

4

You can use interp1 to create an interpolated lookup table function:

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];
x = 0:0.25:10
f = @(xq)interp1(x,fx,xq);

Then you should be able to use ode45 as normal:

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

Note that you did not specify what values of of x your function (fx here) is evaluated over so I chose zero to ten. You'll also not want to use the copy-and-pasted values from the command window of course because they only have four decimal places of accuracy. Also, note that because ode45 required the inputs t and then x, I created a separate anonymous function using f, but f can created with an unused t input if desired.

4
  • Hi again horchler. Your suggestion works perfectly! and the solution of my approximation is promising! My supervisor would be very happy. Thank you again! Nov 1, 2013 at 21:08
  • My problem is now the following: For my x value (for the fx function) I have being using the same domain x(t) has, x(t) is the solution (after ode45) of my first equation (1.-). I have now change the tspan and x0 to see how well my model approximates the solutions of this first ODE and for some tspan and x0 I have nice results, but for other i get NAN from the interp1. I know that the domain of x(t), varies accordingly when tspan and x0 varies. I am applying this changes to my value of x, but I am still getting NAN for the interp1. Do you understand my problem @horchler? Thanks in advance! Nov 2, 2013 at 21:03
  • @SergioSarmiento: Please look at the help for interp1. A value of NaN is returned by default for extrapolated values, i.e., your input xp is less than min(x) or greater than max(x). You'll either need to generate new values for fx over a wider range of x or you can look into using the 'extrap' option within interp1 (just be aware that extrapolation won't be very accurate far from the range of x).
    – horchler
    Nov 2, 2013 at 23:40
  • I am sorry to bother you, but I have a question which is based on THIS question from the last year. In your answer you are giving me the solution to how to send a discrete function to ode45. What I have now is a system of (5x5) odes. If you see your answer, this will mean that I have: fx1,fx2,fx3,fx4,fx5 and x1,x2,x3,x4,x5. Which are the function values and the points where the functions where evaluated. My question: How can I solve this system by ode45? I can not interpolate each equation and send to ode45. I have to solve it as a system, but I don't know how to do it... Oct 6, 2014 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.