if, elseif, else question

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 Commenti

James Tursa
James Tursa il 17 Apr 2020
Please post the ode equation you are trying to solve.
Peter O
Peter O il 17 Apr 2020
I agree with James here. Structurally, your code is fine, and the outputs for A and B appear smooth.
Robert U
Robert U il 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 il 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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange

Prodotti

Release

R2016b

Richiesto:

S H
il 17 Apr 2020

Commentato:

Rik
il 17 Apr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by