Fzero giving weird answer
1 次查看(过去 30 天)
显示 更早的评论
I'm trying to find a maximum upper bound for an ODE by using ode15s, and fzero to find root of function that will determine the maximum of a variable expressed by the ODE. I have used a while loop to reiterate using absolute error values until it works however it runs forever. On closer inspection its because the first iteration (even before while loop) returns the wrong "solution" through fzero. Am i doing something fundamentally wrong or should I use another way to solve this?
%% at point where Q_generated = Q_removed , func = 0 @ Q_generated - Q_removed
%% initalising guess and coressponding outcomes at that time,
guess = 46 ; %mins
CA0 = (ni_incident(1))/V_incident ;
CB0 = (ni_incident(2))/V_incident ;
T0 = 175+273.15 ;
Y0(1) = CA0 ;
Y0(2) = CB0 ;
Y0(3) = T0 ;
[X,Y] = ode15s(@(t,y)DYDT_incident_C(t,y),[0 guess],Y0) ;
CA_final = Y(end,1) ;
CB_final = Y(end,2) ;
T_final = Y(end,3) ;
func = @(Temp)((V_incident * ((-1*(k0*(exp(((-1*Ea)/(R*Temp))))))/(CA_final*CB_final)) * H_reaction)-(UA * (Temp - T_cooling_water))) ;
solution = fzero(func,T_final) ;
error = solution - T_final ;
%% while loop is created to progressively re-iterate increasing upper bound values of time for ode solver until solution satisfied
while abs(error) > (0.1)
guess = guess + (1/60) ;
[X,Y] = ode15s(@(t,y)DYDT_incident_C(t,y),[45 guess],Y0) ;
CA_final = Y(end,1) ;
CB_final = Y(end,2) ;
T_final = Y(end,3) ;
func = @(Temp)((V_incident * (-1*(k0*(exp(((-1*Ea)/(R*Temp))))))/(CA_final*CB_final) * H_reaction)-(UA * (Temp - T_cooling_water))) ;
solution = fzero(func,T_final);
error = solution - T_final ;
end
function dydt = DYDT_incident_C(t,y)
global V_incident H_reaction sum_niCp k0 Ea R
dydt = zeros(3,1) ;
CA = y(1) ;
CB = y(2) ;
T = y(3) ;
k = k0*(exp(-((Ea)/(R*T)))) ;
%% dCAdt = rA
rA = (-k)*CA*CB ;
%% dCBdt = 2*rA = rB
rB = 2*rA ;
if t > 45
Q_gen = (V_incident * rA * H_reaction) ;
Q_rem = 0 ;
else
Q_gen = 0 ;
Q_rem = 0 ;
end
%% Temp variation over time
dT_dt = ((Q_gen-Q_rem)./(sum_niCp)) ;
dydt(1) = rA ;
dydt(2) = rB ;
dydt(3) = dT_dt ;
end
回答(1 个)
Matt J
2023-2-3
编辑:Matt J
2023-2-3
As you can see from the plot, the root lies quite close to solution, so fzero succeeded. If this violates your expectations, it is likely that you haven't implemented the intended equations.
guess = 46 ; %mins
CA0 = (ni_incident(1))/V_incident ;
CB0 = (ni_incident(2))/V_incident ;
T0 = 175+273.15 ;
Y0(1) = CA0 ;
Y0(2) = CB0 ;
Y0(3) = T0 ;
[X,Y] = ode15s(@(t,y)DYDT_incident_C(t,y),[0 guess],Y0) ;
CA_final = Y(end,1) ;
CB_final = Y(end,2) ;
T_final = Y(end,3) ;
func = @(Temp)((V_incident * ((-1*(k0*(exp(((-1*Ea)/(R*Temp))))))/(CA_final*CB_final)) * H_reaction)-(UA * (Temp - T_cooling_water))) ;
solution = fzero(func,T_final)
fplot(@(x)func(x+solution),[-.1,.1])
4 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!