【发布时间】:2017-11-27 12:42:46
【问题描述】:
我正在使用 MATLAB R2016b - 学生版 开发一个 m 文件,该文件将符号微分方程 f(t,y) 作为输入,并输出基于斜率场和解曲线的在初始条件下。代码是
prompt={'dy/dt =','tspan','y0 ='};
title='Slope Field Calculator';
answer=inputdlg(prompt,title);
tspan = str2num(answer{2}); %#ok<*ST2NM>
y0 = str2double(answer{3});
syms t y
f = symfun(sym(answer{1}),[t,y]);
[t,y] = ode45(f, tspan, y0);
hold on
dirfield(f,-5:.3:5,-5,.3:5)
plot(t,y,'b','LineWidth',2)
dirfield(f,-5:.3:5,-5:.3:5) 函数将 f 输入为 @ 函数、内联函数或带引号的 m 文件的名称。然后,dirfield 函数使用从 t1 到 t2 的 t 值,间距为 dt,并使用 y1 到 y2 的间距,为形式为 y' = f(t,y) 的一阶 ODE 绘制方向场dy。
根据 MATLAB 帮助,ode45 函数求解微分方程。
[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) 和 TSPAN = [T0 TFINAL] 然后将微分方程 y' = f(t,y) 从时间 T0 到 TFINAL 与初始条件 Y0 积分。输入ODEFUN 是一个函数句柄。对于标量 T 和向量 Y,ODEFUN(T,Y) 必须返回对应于 f(t,y) 的列向量。
当我运行代码时,对话框运行良好并接受我的输入。但是当我点击“确定”时,代码会抛出这个错误:
Warning: Support of character vectors that are not valid variable names or
define a number will be removed in a
future release. To create symbolic expressions, first create symbolic
variables and then use operations on them.
> In sym>convertExpression (line 1559)
In sym>convertChar (line 1464)
In sym>tomupad (line 1216)
In sym (line 179)
In SlopeFieldsSolutionCurves (line 9)
Undefined function 'exist' for input arguments of type 'symfun'.
Error in odearguments (line 59)
if (exist(ode)==2)
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
varargin);
Error in SlopeFieldsSolutionCurves (line 10)
[t,y] = ode45(f, tspan, y0);
我哪里错了?
【问题讨论】:
标签: matlab differential-equations