You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab绘制二阶微分方程解曲线时fplot报错的解决问询

解决Matlab符号微分方程求解后fplot报错问题

问题场景

求解二阶微分方程:
$$\frac{d2}{dt2}i(t) + 25\frac{d}{dt}i(t) + 100i(t) = 300$$
初始条件为$i(0)=0$,使用Matlab符号计算求解后调用fplot绘制0-5s内的$i(t)$曲线时,触发以下错误:

用户原代码

syms i(t)
eq = diff(i(t),t,2) + 25*diff(i(t),t) + 100*i(t) == 300;
condition = i(0) == 0 ;

sol(t) = dsolve(eq, condition) ; 
fplot(sol, [0,5]);

xlabel('Time (s)'); ylabel('i(t)');

报错信息

Error using fplot>singleFplot (line 240)
Input must be a function or functions of a single variable.

Error in fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
        hObj = cellfun(@(f)
        singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);

Error in fplot>vectorizeFplot (line 200)
        hObj = cellfun(@(f)
        singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);

Error in fplot (line 166)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);

Error in test (line 6)
fplot(sol, [0,5]);

错误原因

sol(t) = dsolve(...)的赋值方式会将sol定义为带自变量索引的符号函数对象,而非单纯的单变量符号函数。fplot无法直接识别这种带索引的对象,因此抛出"输入必须是单变量函数"的错误。

解决方法

提供两种可行修改方案:

方案1:去掉sol的(t)赋值

直接将dsolve的结果赋值给sol,此时sol是标准的单变量符号函数,可直接传入fplot:

syms i(t)
eq = diff(i(t),t,2) + 25*diff(i(t),t) + 100*i(t) == 300;
condition = i(0) == 0 ;

sol = dsolve(eq, condition) ; % 移除(t),直接赋值
fplot(sol, [0,5]);

xlabel('Time (s)'); ylabel('i(t)');

方案2:调用fplot时传入sol(t)

如果需要保留sol(t)的定义方式,调用fplot时明确传入sol(t),让Matlab识别为关于t的单变量函数:

syms i(t)
eq = diff(i(t),t,2) + 25*diff(i(t),t) + 100*i(t) == 300;
condition = i(0) == 0 ;

sol(t) = dsolve(eq, condition) ; 
fplot(sol(t), [0,5]); % 传入sol(t)而非sol

xlabel('Time (s)'); ylabel('i(t)');

补充说明

原方程为二阶微分方程,仅提供了$i(0)=0$一个初始条件,Matlab会自动默认补充$\frac{di}{dt}|_{t=0}=0$的条件。如果实际场景中有不同的导数初始条件,可添加第二个条件参数,例如:

condition2 = diff(i(t),t) == 0; % 假设t=0时导数为0
sol = dsolve(eq, condition, condition2);

内容的提问来源于stack exchange,提问作者이승연

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:40:28