簡體   English   中英

在MATLAB中從2D數組創建函數(在ode45中使用)

[英]Creating a function from a 2D array in MATLAB (For use in ode45)

我是MATLAB的第一次用戶。 我在MATLAB中有2個t和f的二維數組。 這個二維數組對應於一個函數,即f(t)。 我正在使用ode45求解一組微分方程,而f(t)是系數之一,即我具有一組x'(t)= f(t)x(t)或其形式的方程。 我該如何進行?

我需要一種將t vs f數組轉換為可以在ode45方法中使用的函數的方法。 據推測,該轉換將進行一些線性插值,並給出最佳擬合曲線的方程式。

或者,如果有另一種方法,我全神貫注!

謝謝!

這是一個簡單的示例,將傳遞函數作為ODE右側的參數。 在同一目錄中創建文件並運行main

的main.m

t = 0:0.2:10; f = sin(t);
fun = @(xc) interp1(t, f, xc);

x0=0.5
% solve diff(x, t)=sin(t)  
% pass function as parameter                           
[tsol, xsol] = ode45(@(t, x) diff_rhs(t, x, fun),[0.0 8.0], 0.5);

% we solve equation dx/dt = sin(x), x(0)=x0
% exact solution is x(t) = - cos(t) + x(0) + 1
plot(tsol, xsol, 'x');
hold on
plot(tsol, -cos(tsol) + x0 + 1, '-');
legend('numerical', 'exact');

diff_rhs.m

function dx = diff_rhs(t, x, fun)
  dx = fun(t);
end

文檔中的參考: interp1匿名函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM