Fzero giving weird answer
이전 댓글 표시
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
댓글 수: 5
Walter Roberson
2023년 2월 2일
Please enough for us to be able to execute to test.
We can't run your code because it relies on a number of variables that haven't been provided.
Aside from that, you need to describe in what way the result of fzero is "wrong". Did you check whether func(solution)=0? Did you plot func() to see if it even has any roots?
Josh
2023년 2월 2일
Josh
2023년 2월 2일
답변 (1개)
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])
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
