2016-01-24 102 views
0

我一直在玩這一段時間,任何人都可以解釋爲什麼我從Code1和Code2得到不同的答案?關於'dsolve()'的實際腳本是什麼使得2個代碼的輸出不同?可以輸出,如果我只是用不同的語法相同(即」;」。「)dsolve生成不一致的輸出

%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功能我不明白

回答

1

這個問題可能?是你傳遞字符串dsolve而不是象徵性的表達這意味着,在第一種情況下rate_i可能被解釋爲一個常數,而不是t功能

這裏就是你可能嘗試做的。:將Dheight定義爲sym,並告知dsolve做什麼用sym S:因爲它不使用,含有不明確的參考(D)AreaY

  • dsolve改變從

    • 我刪除delta_Vol

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

      更改您的版本字符串以符號表達,同時=必須更改爲==

    • 我定義了DHeightdiff(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) 
    

    確實是你的微分方程的解決方案。

  • +1

    謝謝,這是另一種我可以選擇編碼的視角/路線,書籍/教師沒有提供。謝謝你,因爲我認爲dsolve()只接受字符串,但現在我看到我如何插入符號參數。我也從來不知道我可以有一個變量聲明,就像用另一個變量明確定義的高度一樣,如(t)。 – ReadyF4i