runge-kutta功能
69次观看(最近30天)
显示较旧的评论
得出一个MATLAB函数,该功能从用户接收二阶微分方程以及步长和初始值,并使用用户选择的第4阶runge-kutta或第二阶runge-kutta。
答案(2)
James Tursa
2019年5月29日
编辑:James Tursa
2019年5月29日
这是一些代码来帮助您开始。它接收a string from the user for a derivative function of x and t and turns it into a function handle that can be used in an RK scheme.
s =输入('Input an expression for the derivative of x (it can use t also): ',,,,');
dx =str2func(['@(t,x)'s]);
现在,您拥有一个计算x导数的函数句柄。您将其称为当前时间t和state x:dx(t,x)
例如,运行此代码:
输入an expression for the derivative of x (it can use t also): cos(x) + t*x^2
>> DX
dx =
function_handle具有价值:
@(t,x)cos(x)+t*x^2
However, if your derivative function involves variables from the workspace, then you will need to use eval( ) instead because str2func( ) can't see those local variables when it creates the function handle:
dx = eval([['@(t,x)'s]);
例如。,
>> a = 5;
>> s =输入('Input an expression for the derivative of x (it can use t also): ',,,,');
输入x衍生物的表达式(它也可以使用t):a*cos(x)+t*x^2
>> dx = str2func([['@(t,x)'s]);
>> DX(2,3)
不明确的功能或变量“ A”。< - str2func不起作用
Errorin @(t,x)a*cos(x)+t*x^2
>> DX= eval(['@(t,x)'s]);
>> DX(2,3)
ans =
13.0500 < - eval确实工作
对于RK4和RK2方案,您的教练给了您什么?当然,必须有课堂上讨论的算法。
0评论
Meysam Mahooti
2021年7月28日
//www.tianjin-qmedu.com/matlabcentral/fileexchange/55430-runge-kutta-4th-core?s_tid=srchtitle