Engineer Wannabe
Engineer Wannabe

Reputation: 1

MATLAB problems

1st m-file

clear pH Fb
pH = 5:0.1:9;
for i = 1:length(pH)
    Fb(i) = pH2Fb(pH(i));
end 
plot(Fb,pH)

2nd m-file

function Fb=pH2Fb(pH)   
Fb=fsolve(@fun1,1,[],pH);

function f=fun1(Fb,pH)  

 Fa = 2.9; %ml/s
 Ca = 3e-3; %mol/ml
 Cb = 5e-5; %mol/ml
 Ca1 = -3e-3; %mol/ml
 pk1 = 1.27;
 pk2 = 4.266;    
 Xa = (Fa*Ca+Fb*Ca1)/(Fa+Fb);  
 Xb = (Fb*Cb)/(Fa+Fb); 
 f= (Xa + 10^(pH-14)-10^(-pH)+ Xb*(1+2*10^(pH-pk2))/(1+10^(pk1-pH)+10^(pH-pk2)))*1e19;

here's the questions:

1. for Fb=fsolve(@fun1,1,[ ],pH); what it meant by the equation above??

2. for 2nd m-file, why "function f=fun1(Fb,pH)" is stated after "Fb=fsolve(@fun1,1,[ ],pH);"

meanwhile the function "fun1" is called at before it is being solve?? because so far that I know, MATLAB is run is from upper to bottom and supposely the "fun1" is undefined as it being solve later..

Upvotes: 0

Views: 130

Answers (1)

Tom
Tom

Reputation: 1339

In answer to your two questions:

  1. The call to fsolve is using an old MATLAB syntax for passing an argument to the objective function. The call is optimizing for the objective function @(x) fun1(x, pH) starting from the initial value 1 and passing no options ([]). For recent versions of MATLAB this should be written

    fsolve(@(x) fun1(x, pH), 1)

  2. fun1 is a nested function so can be called from within it's parent function pH2Fb.

Upvotes: 1

Related Questions