How to get ode45 to work with my function?

I keep getting these error messages:
Undefined function 'mrdivide' for input arguments of type 'function_handle'.
Error in adiabatic (line 35) k = kt*exp((E/R2)*((1/T0)-(1/f)));
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in KineticsProject (line 14) [X,V] = ode45(@adiabatic,Xspan,V0)
if true
function dXdV = adiabatic(V,X)
syms T
eqn1 = ((CpA+CpB)*(T-T0))+(X*(HRXStand+HvapA+HvapC+(CpC*(T-BPC))+(CpD*(T-T0))-(CpA*(T-BPA))-(CpB*(T-T0)))) == 0;
T = vpasolve(eqn1,T);
f = matlabFunction(T);
k = kt*exp((E/R2)*((1/T0)-(1/f)));
CA = CA0*(1-X);
CB = CA0*(1-X);
rA = k*CA*(CB^0.5); % mol/liter*s
dXdV = rA/FA0;
end
I plugged it into the following code (all variables in eqn1 have been defined):
if true
Xspan = [0,0.5];
V0 = 0;
[X,V] = ode45(@adiabatic,Xspan,V0)
end

 채택된 답변

Star Strider
Star Strider 2018년 4월 28일

0 개 추천

Your code is not easy to follow.
My guess is that this line is throwing the error:
k = kt*exp((E/R2)*((1/T0)-(1/f)));
The most likely solution is to evaluate ‘f’, since that changes it from a function handle to a function evaluation:
k = kt*exp((E/R2)*((1/T0)-(1/f(X))));

댓글 수: 8

Bryan Lee
Bryan Lee 2018년 4월 28일
I'm sorry. The bulk of the code is just defining variables. The important parts of the code are after 'syms T'.
I know that I have to somehow convert the symbolic equation T(X) into a numerical form so that it can be evaluated by ode45, but I can't figure out how.
I thought I told you exactly how in my Answer:
k = kt*exp((E/R2)*((1/T0)-(1/f(X))));
Bryan Lee
Bryan Lee 2018년 4월 28일
I tried it and it did not work
Star Strider
Star Strider 2018년 4월 28일
What does ‘did not work’ mean?
Bryan Lee
Bryan Lee 2018년 4월 28일
Thank you for the prompt responses. These are the errors I get:
Error using symengine>@()1.123e3 Too many input arguments.
Error in adiabatic (line 35) k = kt*exp((E/R2)*((1/T0)-(1/f(X))));
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in KineticsProject (line 14) [X,V] = ode45(@adiabatic,Xspan,V0)
Star Strider
Star Strider 2018년 4월 28일
I would use the Symbolic Math Toolbox once to create any functions you need, extract them as anonymous functions with matlabFunction, then do all the calculations numerically.
Bryan Lee
Bryan Lee 2018년 4월 29일
Thank you for your help Start Strider. Although I fixed it a different way, your guidance pointed me in the right direction. I was able to resolve this problem without using matlabFunction. I used double() to convert my symbolic expression obtained from vpasolve() into double precision and ode45 accepted it. Thank you!
Star Strider
Star Strider 2018년 4월 29일
My pleasure.
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2018년 4월 28일

댓글:

2018년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by