解决二次函数时的fsolve错误

时间:2017-07-25 17:58:54

标签: matlab

所以我尝试在下面的代码中使用fsolve,但我一直在收到错误并且不知道如何解决它,所以任何帮助都将非常感激。作为参考,我使用(1 / 2,1,1,1 / 2,0)作为我的输入参数。

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 个答案:

答案 0 :(得分:1)

fsolve的第一个参数必须是function handle,因此您应该将fg写为anonymous 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;