Reputation: 13
so Im trying to use fsolve in my code below, but I keep on getting an error and have no clue on how to fix it, so any help would be greatly appreciated. For reference I use (1/2,1,1,1/2,0) as my input arguments.
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
Also, sorry in advance for the terrible variable labelling.
>> 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);
Upvotes: 1
Views: 159
Reputation: 125864
The first argument to fsolve
has to be a function handle, so you should write f
and g
as 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;
Upvotes: 1