我已经玩了一段时间,有人可以解释为什么我从Code1和Code2得到不同的答案吗?什么是关于' dsolve()'的实际脚本?这使得2个代码的输出不同?如果我只使用不同的语法(即',;"。'),输出是否可以相同?
%Code1:
syms Qua t Area height
rate_in = 3*Qua*(sin(t))^2;
delta_Vol = dsolve('DAreaY = rate_in - Qua');
delta_Height= dsolve('Dheight = ((rate_in - Qua)/Area)', 'height(0) = 0');
subfnc1 = subs(rate_in, {Qua}, {450});
fnc1 = subs(delta_Height, {'rate_in'}, {subfnc1});
fnc1 = subs(fnc1, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1);
fnc_main(0:10)';
%Code2:
syms Qua t Area height
rate_in = 3*Qua*(sin(t))^2;
delta_Vol = dsolve('DAreaY = 3*Qua*(sin(t))^2 - Qua');
delta_Height= dsolve('Dheight = ((3*Qua*(sin(t))^2 - Qua)/Area)', 'height(0) = 0');
fnc1 = subs(delta_Height, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1);
fnc_main(0:10)';
我不理解的dsolved功能是什么?
答案 0 :(得分:1)
问题可能在于您将字符串传递给dsolve
而不是符号表达式。这意味着在第一种情况下rate_i
可能被解释为常量,而不是t
的函数。
以下是您可能尝试做的事情:将Dheight
定义为sym
,并告诉dsolve
使用sym
做什么} S:
%Code1:
clear Qua t Area height Dheight
syms Qua t Area height(t) Dheight
Dheight = diff(height);
rate_in = 3*Qua*(sin(t))^2;
delta_Height= dsolve(Dheight == ((rate_in - Qua)/Area), height(0) == 0);
subfnc1 = subs(rate_in, {Qua}, {450});
fnc1 = subs(delta_Height, {'rate_in'}, {subfnc1});
fnc1 = subs(fnc1, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1)
%Code2:
clear Qua t Area height Dheight
syms Qua t Area height(t) Dheight
Dheight = diff(height);
rate_in = 3*Qua*(sin(t))^2;
delta_Height= dsolve(Dheight == ((3*Qua*(sin(t))^2 - Qua)/Area), height(0) == 0);
fnc1 = subs(delta_Height, {Area, Qua}, {1250,450});
fnc_main = matlabFunction(fnc1)
您的版本更改:
delta_Vol
,因为它没有被使用,并且包含对(D)AreaY
的不明确引用dsolve
从字符串更改为符号表达式,同时=
必须更改为==
DHeight
定义为diff(Height)
,这意味着必须将height
声明为height(t)
。这也允许我们将初始条件定义为height(0)==0
,否则我们需要将其保留为字符串:'height(0)=0'
。现在两个版本都返回相同的解决方案:
fnc_main =
@(t)t.*(9.0./5.0e1)-sin(t.*2.0).*(2.7e1./1.0e2)
我建议在纸上检查这个解决方案,或者它的象征性前身
delta_Height =
(Qua*(2*t - 3*sin(2*t)))/(4*Area)
确实是你的微分方程的解决方案。