# if, elseif, else question

3 ビュー (過去 30 日間)
S H 2020 年 4 月 17 日
コメント済み: Rik 2020 年 4 月 17 日
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);
dBdt=k1*(A^(n1)-Coe);
[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 件のコメント

Peter O 2020 年 4 月 17 日
I agree with James here. Structurally, your code is fine, and the outputs for A and B appear smooth.
Robert U 2020 年 4 月 17 日
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 2020 年 4 月 17 日
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.

サインインしてコメントする。

R2016b

### Community Treasure Hunt

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

Start Hunting!

Translated by