# Matlab fzero function

• Jan 7th 2010, 02:02 AM
MartinKeppler
Matlab fzero function
Hi All,
Below there is some piece of a code for a problem I try to solve. Unfortunately, I am not sure about the red part of the code. Always get an error message in Matlab. Does anybody know where the problem lies? I believe that the "fzero" function in matlab may have problem to handle the f_evaldeltaHV (P,T,x) function which by itself are functions and contatain the iteration variable T (temperature).

V=F*VF; % Vapor stream in mol/s
L=F-V; % Liquid stream in in mol/s
P=1;
Tf=350; %Temperature of feed
T=250; % Guess temperature
% Tchange=1000
% while (abs(Tchange)>1e-5)
% HF=f_evaldeltaHV(P,Tf,z); %Enthalpy of Feed stream in [J/mol]
% HV=f_evaldeltaHV(P,T,y); %Enthalpy of Vapor stream in [J/mol]
% HL=f_evaldeltaHL(P,T,x); %Enthalpy of Liquid stream in [J/mol]

f_energy_balance=F*f_evaldeltaHV(P,Tf,z)+Q V*f_evaldeltaHV(P,T,y)-L*f_evaldeltaHL(P,T,x);
Tnew=fzero(f_energy_balance,T,[],F,Q,V,L,P,T,x,y,z,Tf);

Tchange=T-Tnew;
% if abs(Tchange)<1e-5
% break;
% end;
% T=Tnew
%end;

Cheers and many thanks in advance.
Martin
• Jan 7th 2010, 06:57 AM
CaptainBlack
Quote:

Originally Posted by MartinKeppler
Hi All,
Below there is some piece of a code for a problem I try to solve. Unfortunately, I am not sure about the red part of the code. Always get an error message in Matlab. Does anybody know where the problem lies? I believe that the "fzero" function in matlab may have problem to handle the f_evaldeltaHV (P,T,x) function which by itself are functions and contatain the iteration variable T (temperature).

V=F*VF; % Vapor stream in mol/s
L=F-V; % Liquid stream in in mol/s
P=1;
Tf=350; %Temperature of feed
T=250; % Guess temperature
% Tchange=1000
% while (abs(Tchange)>1e-5)
% HF=f_evaldeltaHV(P,Tf,z); %Enthalpy of Feed stream in [J/mol]
% HV=f_evaldeltaHV(P,T,y); %Enthalpy of Vapor stream in [J/mol]
% HL=f_evaldeltaHL(P,T,x); %Enthalpy of Liquid stream in [J/mol]

f_energy_balance=F*f_evaldeltaHV(P,Tf,z)+Q V*f_evaldeltaHV(P,T,y)-L*f_evaldeltaHL(P,T,x);
Tnew=fzero(f_energy_balance,T,[],F,Q,V,L,P,T,x,y,z,Tf);

Tchange=T-Tnew;
% if abs(Tchange)<1e-5
% break;
% end;
% T=Tnew
%end;

Cheers and many thanks in advance.
Martin

Your f_energy_balance is not a function, it has either to be a function definition in an .m file or an in-line anonymous function.

See the help of fzero: Find root of continuous function of one variable - MATLAB

(it will also tell you what to do with all those extra parameters.)

CB
• Jan 8th 2010, 12:29 AM
MartinKeppler
Many Thanks
Thank you very much. Actually I figured out that by using a handle function you can solve the problem.

example:
f_energy_balance=@ (T) F*f_evaldeltaHV(P,Tf,z)+Q V*f_evaldeltaHV(P,T,y)-L*f_evaldeltaHL(P,T,x);
Tnew=fzero(f_energy_balance, T)

Like this is works. So I have to say that the help of Matlab is not very user friendly at all, and it is rather by luck I understood what a "handle" is....

But many thanks for the hint CB
• Jan 8th 2010, 04:30 AM
CaptainBlack
Quote:

Originally Posted by MartinKeppler
Thank you very much. Actually I figured out that by using a handle function you can solve the problem.

example:
f_energy_balance=@ (T) F*f_evaldeltaHV(P,Tf,z)+Q V*f_evaldeltaHV(P,T,y)-L*f_evaldeltaHL(P,T,x);
Tnew=fzero(f_energy_balance, T)

Like this is works. So I have to say that the help of Matlab is not very user friendly at all, and it is rather by luck I understood what a "handle" is....

But many thanks for the hint CB

That is not a "handle function" but a "function handle" to an anonymous function. Which is one of the things mentioned in my earlier post.

CB