Please do not ever use global variables.
This works:
function r = rxnrate(V,F, k, Keq, f, P)
Fa=F(1);
Fb=F(2);
Fc=F(3);
Fd=F(4);
Pa=(Fa./f)*P;
Pb=(Fb./f)*P;
Pc=(Fc./f)*P;
Pd=(Fd./f)*P;
Fa=Fb;
Fc=Fd;
f=Fa+Fb+Fc+Fd;
r(1)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(2)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(3)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(4)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r=r';
end
P=1;
R=8.314E-3;
k0=6.195E8;
f=33;
Fao=(0.5*f); Fbo=(0.5*f); Fco=0; Fdo=0;
T1=450;
T2=550;
T3=650;
k=k0*exp(-47.53/(R*T1));
Keq=exp((4577.8/T1)-4.33);
V=[0 0.25];
Fo=[Fao Fbo Fco Fdo];
[V,F]=ode45(@(V,F)rxnrate(V,F, k, Keq, f, P),V,Fo);
plot(V,F);
The problem is that you did not declare the variables to be global in both your calling script and ‘rxnrate’. However, since you will never again use global variables, and will always pass necessary variables as additional parameters to your functions instead, there is no need to mention that further.
Best Answer