我正在使用 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
函数绘制形式为y'= f(t,y)的一阶ODE的方向字段,使用t1到t2的t值,间距为dt,并使用y值从y1到y2间距为dy。
根据MATLAB的帮助,ode45
函数解决了微分方程。
[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0)
TSPAN = [T0 TFINAL]
T0
会将TFINAL
与Y0
的微分方程y'= f(t,y)整合为初始条件ODEFUN
。输入ODEFUN(T,Y)
是一个函数句柄。对于标量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);
必须返回对应于f(t,y)的列向量。
当我运行代码时,对话框运行良好并接受我的输入。但是当我单击“确定”时,代码会抛出此错误:
Class 'MdOptgroup' incorrectly implements interface 'Can
Disable'.
Property 'disabled' is missing in type 'MdOptgroup'.
node_modules/@angular/core/index"' has no exported member 'Renderer2'.
Class 'MdTab' incorrectly implements interface 'CanDisable'.Property 'disabled' is missing in type 'MdTab'.
Type '(new (...args: any[]) => CanDisable) & typeofMdTabLabelWrapperBase' is not a constructor function type.
Class 'MdToolbar' incorrectly implements interface 'CanColor'.
Property 'color' is missing in type 'MdToolbar'.
ERROR in Error encountered resolving symbol values statically. Calling function 'InjectionToken', function calls are not supported. Consider replacing the function or lambda with a refe
rence to an exported function, resolving symbol MATERIAL_SANITY_CHECKS in /node_modules/@angular/material/typings/index.d.ts, resolving symbol MdCommonModule in node_modules/@angular/material/typings/index.d.ts, resolving symbol MdCommonModule in /node_modules/@angular/material/typings/index.d.ts
webpack: Failed to compile.
我哪里错了?
答案 0 :(得分:1)
ode45
采用函数句柄,而不是符号函数。请改用matlabFunction:
[t,y] = ode45(matlabFunction(f), tspan, y0);
要摆脱第一个警告,您需要稍微定义f
:
f = evalin( symengine, answer{1} );
f = symfun( f, [t,y] );