解决生成不一致的输出



我已经玩了一段时间了,谁能解释一下为什么我从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)';

我不明白的解算函数是什么?

问题可能是您将字符串传递给dsolve而不是符号表达式。这意味着在第一种情况下,rate_i可以被解释为常数,而不是t的函数。

以下是您可能正在尝试做的事情:将Dheight也定义为sym,并使用sym告诉dsolve该怎么做:

%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)

确实是微分方程的解。

相关内容

  • 没有找到相关文章

最新更新