2017-07-25 140 views
1

所以我試圖在我的代碼中使用fsolve下面,但我一直在得到一個錯誤,並不知道如何解決它,所以任何幫助將不勝感激。作爲參考,我使用(1/2,1,1,1/2,0)作爲我的輸入參數。fsolve錯誤時解決二次函數

function [ B_A , A_B ] = SecondOrderSimulation(delta,c1,c2,s0,m2A_Current) 
m1A_Current = (-delta -c2*m2A_Current)/c1; 
m1B_Current = 0; 
m2B_Current = 0; 
syms t positive; 
B_A = []; 
A_B = []; 
    for k = 0:5 
     if mod(k,2)==0 || k==0 %if k is even/interval A_n to B_n 
      f = c1*(m1A_Current + (1+s0)*t) +c2*(m2A_Current + m1A_Current*t 
+ ((1+s0)*(t)^2)/2) - delta; 
     solve_even = fsolve(f,1); 
     B_A = [B_A solve_even]; 
     m1B_Next = m1A_Current + (1+s0)*solve_even; 
     m2B_Next = (delta - c1*m1B_Next)/c2; 
     m1B_Current = m1B_Next; 
     m2B_Current = m2B_Next; 
     else %if k is odd/interval B_n to A_n+1 
     g = c1*(m1B_Current - (1-s0)*t) +c2(m2B_Current + m1B_Current*t - ((1-s0)*(t)^2)/2) + delta; 
     solve_odd = fsolve(g,1); 
     A_B = [A_B solve_odd] 
     m1A_Next = m1B_Current - (1-s0)*solve_odd; 
     m2A_Next = -(delta +c1*m1A_Next)/c2; 
     m1A_Current = m1A_Next; 
     m2A_Current = m2A_Next; 
     end 
    end 
end 

此外,對於可怕的變量標籤事先抱歉。

>> SecondOrderSimulation(1/2,1,1,1/2,0) 
Error using lsqfcnchk (line 108) 
If FUN is a MATLAB object, it must have an feval method. 

Error in fsolve (line 210) 
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag); 

Error in SecondOrderSimulation (line 11) 
     solve_even = fsolve(f,1); 

回答

1

的第一個參數fsolve必須是一個function handle,所以你應該寫fganonymous functions:現在

f = @(t) c1*(m1A_Current + (1+s0)*t) +c2*(m2A_Current + m1A_Current*t ... 
     + ((1+s0)*(t)^2)/2) - delta; 

g = @(t) c1*(m1B_Current - (1-s0)*t) +c2(m2B_Current + m1B_Current*t ... 
     - ((1-s0)*(t)^2)/2) + delta; 
+0

你的先生是一個救星:)所有的工作,謝謝! – pepper