MATLAB: Solve differential equation with anonymous functions

differential equationsode45

Hello, everyone. I am already quite familiar when it comes to the resolution of differential equations(DE’S) in MATLAB with “ode45” function. I have already solved this problem by making a function dFdV that contained the DE’S which has as inputs (V,F), being V the independent and F the dependent variable.
function dFdV=funcion(V,F)
CTo=0.286;
k=0.4;
FT=F(1)+F(2)+F(3); % FT=FA+FB+FC;

CA=(CTo*F(1))/FT;
rA=-k*CA^2;
rB=-rA;
rC=-0.5*rA;
dFdV=zeros(3,1);
dFdV(1)=rA;
dFdV(2)=rB;
dFdV(3)=rC;
end
What I now want is to solve the same problem creating an anonymous function that contains the DE’S and using again “ode45” to solve them.
CTo=0.286;
k=0.4;
FT=@(F)(F(1)+F(2)+F(3)); % FT=FA+FB+FC;
CA=@(F)((CTo*F(1))/FT(F));
rA=-k*(CA(F))^2;
rB=-rA;
rC=-0.5*rA;
dFdV=@(V,F)[rA;rB;rC];
The problem is that when executing It pops up this message (“Undefined function or variable 'F'), which is obvious because now my function dFdV depends on V and F, but F has not been defined (as an input) as the former case.
What should I do?. Thanks

Best Answer

CTo=0.286;
k=0.4;
FT=@(F)(F(1)+F(2)+F(3)); % FT=FA+FB+FC;
CA=@(F)((CTo*F(1))/FT(F));
rA=@(F) -k*(CA(F))^2;
rB=@(F)-rA(F);
rC=@(F)-0.5*rA(F);
dFdV=@(V,F)[rA(F);rB(F);rC(F)];