当函数作为离散值给出时求解 ODE -matlab- [英] Solving an ODE when the function is given as discrete values -matlab-

查看:48
本文介绍了当函数作为离散值给出时求解 ODE -matlab-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 ODE:

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

我正在使用 ode45 来解决它.我的解决方案是作为一个dim(k x 1) 向量给出的(通常k = 41,由tspan 给出).

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).

另一方面,我已经制作了一个近似于(1)中的模型的模型,但是为了比较第二个模型的准确度,我想通过ode45.我的问题是这第二个 ode 是离散的:

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 是离散的,而不是像 (1) 中的连续函数.我对 f 的值是:

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

我现在想要的是使用 ode45 解决第二个 ode.希望我能得到一个与(1)中的解决方案非常相似的解决方案.如何解决应用 ode45 的离散 ode?是否可以使用 ode45?否则我可以使用 Runge-Kutta 但我想公平地比较这两种方法,这意味着我必须以相同的方式解决它们.

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.

推荐答案

您可以使用 interp1 创建内插查找表函数:

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);

那么你应该可以正常使用ode45:

Then you should be able to use ode45 as normal:

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

请注意,您没有指定您的函数(此处为 fx)的 x 的值,因此我选择了 0 到 10.当然,您也不希望使用命令窗口中的复制粘贴值,因为它们的精度只有四位小数.另外,请注意,因为 ode45 需要输入 t 然后是 x,所以我使用 f 创建了一个单独的匿名函数,但如果需要,f 可以使用未使用的 t 输入创建.

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.

这篇关于当函数作为离散值给出时求解 ODE -matlab-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆