Hi , I'm new to matlab with fsolve and I get not enough input arguments everytime. Can someone please as what am I missing. Many Thanks

function AAHydro
p = parameter;
Delta_T_adiabat = p.dTad
Zulaufkonzentration = p.c_AA_0
x0 = [-5;-5];
options= optimoptions('fsolve','Display','iter'); % Option to display output
[x,fval] = fsolve(@myfun, x0, options); % Call solver
end
function dx = myfun(x)
% x(1) = Concnetration in the reactor
% x(2) = Temperature of reactor
p= parameter;
dx(1) = (p.c_AA_0-x(1))/p.tau + p.kinf*exp(-p.EA*1000/(p.R*x(2)))*(x(1))^p.n;
dx(2) = (p.Tin - x(2))/p.tau + p.dTad*p.kinf*exp(-p.EA*1000/(p.R*x(2)))*x(1))^p.n;
dx = dx';
end
function p = parameter()
p.R = 8.3145; % [J/molK]
p.Tin = 30 + 273.15; % [K]
p.n = 0.94;
p.rho = 1000; % [kg/m^3]
p.cp = 4.186; % [kJ/kgK]
p.vol = 0.56; % [l]
p.dHr = -55.5; % [kJ/mol]
p.MAA = 102.09; % [g/mol]
p.rho_AA = 1.08; % [g/cm^3]
% p.Vdot_AA = 1.114 ; % [l/h]
p.Vdot_AA = 0.3 ; % [l/h]
p.Vdot_W = 3.6; % [l/h]
p.Vdot_total = p.Vdot_AA + p.Vdot_W; % [l/h]
p.tau = p.vol/p.Vdot_total*60*60; % [s]
p.c_AA_0 = ((p.Vdot_AA*p.rho_AA*1000)/p.MAA)/p.Vdot_total; % [mol/l]
p.dTad = -p.dHr*p.c_AA_0/(1e-03*p.rho*p.cp); % [K]
p.kinf = 139390; % [1/s]
p.EA = 44.35; % [kJ/mol]
end

 Accepted Answer

Perhaps you should pass your parameters as follows:
p = parameter(); % get the parameters into the workspace
[x,fval] = fsolve(@(x)myfun(x,p), x0, options); % Call solver
where you change the function myfun as follows:
function dx = myfun(x,p)
% x(1) = Concnetration in the reactor
% x(2) = Temperature of reactor
dx = [0;0]; % initialize
dx(1) = (p.c_AA_0-x(1))/p.tau + p.kinf*exp(-p.EA*1000/(p.R*x(2)))*(x(1))^p.n;
dx(2) = (p.Tin - x(2))/p.tau + p.dTad*p.kinf*exp(-p.EA*1000/(p.R*x(2)))*x(1))^p.n;
end
Alan Weiss
MATLAB mathematical toolbox documentation

2 Comments

It helped me, thanks alan. I use this fsolve in a function which I will be using it in mz function with fmincon. It gives an error sazing user supplied objective function must return a scalar value. Could you please an insight about it. Thanks so much for the above help. I tried yesterday and I could run the program after few trials but your method result is more accurate.
If you are asking what the fmincon error means, you should know that fmincon requires the objective function to return a scalar value. This means not a vector or matrix, a scalar. You should check what your fmincon objective function returns for your initial value x0.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (1)

optimizationcstr
clear all;
close all;
clc;
%%Optimization
A =[];
b =[];
Aeq =[];
beq =[];
lb = [0 0 813.5 310];
ub = [1.7e-6 9.4e-5 814.5 340];
x0 = [8.6667e-8 6.66667e-7 814 325];
options = optimset('Algorithm','interior-point','Hessian','bfgs',...
'AlwaysHonorConstraints','bounds');
nonlcon=@mycon;
[x]= fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
end
function AAHydro
clc;
close all;
clear all;
p = parameter(); % get the parameters into the workspace
Delta_T_adiabat = p.dTad
Zulaufkonzentration = p.c_AA_0
x0 = [5;5];
options= optimoptions('fsolve','Display','iter'); % Option to display output
[x,fval] = fsolve(@(x)myfun(x,p), x0, options); % Call solver
end
function [c,ceq]= mycon(x)
p= parameter();
c= (x(1)*p.rho_A.A)/(p.MAA*(x(1)+x(2)));
ceq= myfun(x);
end
function dx = myfun(x,p)
% x(1) = Concnetration in the reactor
% x(2) = Temperature of reactor
dx = [0;0]; % initialize
dx(1) = (p.c_AA_0-x(1))/p.tau + p.kinf*exp(-p.EA*1000/(p.R*x(2)))*(x(1))^p.n;
dx(2) = (p.Tin - x(2))/p.tau + p.dTad*p.kinf*exp(-p.EA*1000/(p.R*x(2)))*(x(1))^p.n;
end
function p = parameter()
% Parameter
p.kinf= 936589 ; %[1/s]
p.EA= 49900; %[J/mol]
p.Rg= 8.314 ; %[J/mol*K]
p.V= 0.00026533 ; %[m3]
p.F_H2o=6.6667e-7; % m3/s Flowrate of H2o
p.F_A.A= 8.6667e-8; % m3/s Flowrate of A.A
p.F_Total= 7.53333e-7 ;% m3/s Total Flowrate
p.c_AA_0= 814 ; %[mol/m3] initial concentration
p.Delta_HR= -56.6 ;%[kJ/mol]
p.rho_cp= 4184 ;%[kJ/K*m3]
p.n=0.94 ; % reaction order
p.theTaw = 25; %0C mean temperature
p.theTao = 20; %0C initial temp in the reactor
p.Taw=p.theTaw+273.15;
p.Tao=p.theTao+273.15;
p.MAA=0.10209; % kg/mol
p.rho_A.A= 1080; % kg/m3
p.tau= p.V/p.F_Total;
p.dTad= -p.Delta_HR*p.c_AA_0/(p.rho_cp);
end

4 Comments

@ raviteja peri: is this an answer to your own question? The Answer fields are only supposed to be for actually answering a question.
Do you have a question?
@ Stephan -I'm sorry as I had a question relating to the same topic and thought I can ask in comment section. The above fsolve part was solved and I use this fsolve function (hydro) in my main function where I use fmincon.
Sure, but this is an Answer. We can see that it is an answer because the text-field title was "Answer this question", where you posted the code. It helps us when you stick to using the comments for commenting and adding more information on your original question. The comment links are titled "Comment on this Answer" or "Comment on this Question".
You still have not asked a question, so it is not clear why you have shown us this code. IS the code doing something that you do not expect? How can we help you with it? It would be nice to get an explanation from you, as our mindreading abilities are not very well developed.
The user posted the code in a new question which has received an answer.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!