if, elseif, else question

4 views (last 30 days)
S H
S H on 17 Apr 2020
Commented: Rik on 17 Apr 2020
Hi everyone,
I have a question regarding "if, elseif, else".
The condition I have is in the following:
When t<65.4, T=473+5*t
t>65.4, T=800
and t<61.4, Coe=0.05+(2*10^(-5))*T
t>61.4, Coe=-10.4+0.024*T-(10^(-5))*T^2
Something is wrong with my code. It did not give the correct answer I've expected. The code I've written is in the following:
function dydt=Hi(t,y)
format long
if t>0 & t< 61.4
T=473+5*t;
Coe=0.05+(2*10^(-5))*T;
elseif 61.4<t & t<65.4
T=473+5*t;
Coe=-10.4+0.024*T-(10^(-5))*T^2;
else
T=800;
Coe=-10.4+0.024*T-(10^(-5))*T^2;
end
E1=90000;
k10=15000;
k1=k10*exp(-E1/(8.314*T));
n1=1;
A=y(1);
B=y(2);
dAdt=-k1*(A^(n1)-Coe);
dBdt=k1*(A^(n1)-Coe);
dydt=[dAdt; dBdt];
[t,y]=ode45('Hi',[0 250],[14.25 0.75]);
A=y(:,1);
B=y(:,2);
Does anyone know what mistake I've made and how can I revise it? Thank you!
  4 Comments
Robert U
Robert U on 17 Apr 2020
According to given inequalities values for T are undefined for t = 65.4 whereas values for Coe are undefined for t = 61.4. Your code is assigning the "else"-case for these undefined values. Furthermore it is not defined that first interval is valid only for t > 0 (Even though, t seems to be time. But you did not define that.)
I assume, the inequalities given are not entirely correct since you want to have a functional description that is steadily defined.
Your code jumps into "else" statement for t = 0 which leads to an unsteady output for T and Coe.
Please, elaborate on "It did not give the correct answer I've expected." What have you expected, and what have you got.
Kind regards,
Robert
Rik
Rik on 17 Apr 2020
Is t a vector or a scalar? A conditional should be a scalar, otherwise the result is probably not what you think it would be.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!