Please find the fault in the mentioned code

1 visualización (últimos 30 días)
pankaj saha
pankaj saha el 15 de Abr. de 2016
Respondida: Swathi Kulkarni el 12 de Oct. de 2020
% code for solving a non-linear non-inhomogeneous(pulse I/P, U) ODE system %
syms t v u
g=@(t,v,u)[-((1.17*10^-6) + (9*10^-3*t^-0.5))*v(1) + (9*10^-3*(t^-0.5))*v(2) + 0.12*u; (9*10^-3*t^-0.5)*v(1) - (9*10^-3*t^-0.5)*v(2) + (5.8*10^-3)*u];
for u=[0 4 4 4 4 0 0 0 0 0]
[t,va] = ode45(@(t,v) g(t,v,u),[0 10],[0 0]);
end
That code always gives the ans NaN, which should not be. Any one can tell me where is the fault actually? And how to plot the output for the whole input (U)?
Used Matlab version is 2013a.
Thank you.

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Abr. de 2016
You have t^-0.5 in several places. That is going to give inf at t = 0. The subterms that give infinity are multiplied by v(1) or v(2), both of which start at 0, and inf * 0 is NaN.
You can change your initial conditions to both be non-zero so that the inf is not being multiplied by 0 and so will not give rise to NaN. If you do that for t = 0, then it so happens that the sub expressions (9*10^-3*t^-0.5))*v(1) and (9*10^-3*(t^-0.5))*v(2) give infinities with opposite sign, one +inf and the other -inf . +inf + -inf is also NaN . Therefore you cannot just fix this by changing your initial conditions to be non-zero.
You can change your initial time to be greater than 0 so that the inf does not occur. That will not lead to any computation problems. You would, however, then discover that if your initial conditions for v are both 0, that the integral will stay 0.
  2 comentarios
John BG
John BG el 17 de Abr. de 2016
what is v anyway?
Walter Roberson
Walter Roberson el 17 de Abr. de 2016
I do not know what it stands for in terms of the equation, but v corresponds to the standard "y" parameter for the ode functions, so the [0 0] is the initial conditions for the initial time.

Iniciar sesión para comentar.

Más respuestas (2)

J. Webster
J. Webster el 15 de Abr. de 2016
Please format it so that it's easier to read.
  1 comentario
pankaj saha
pankaj saha el 17 de Abr. de 2016
Editada: pankaj saha el 17 de Abr. de 2016
% code for solving a non-linear non-inhomogeneous(pulse I/P, U) ODE system %
syms t v u
g=@(t,v,u)[-((1.17*10^-6) + (9*10^-3*t^-0.5))*v(1) + (9*10^-3*(t^-0.5))*v(2) + 0.12*u; (9*10^-3*t^-0.5)*v(1) - (9*10^-3*t^-0.5)*v(2) + (5.8*10^-3)*u];
for u=[0 4 4 4 4 0 0 0 0 0]
[t,va] = ode45(@(t,v) g(t,v,u),[0 10],[0 0]);
end

Iniciar sesión para comentar.


Swathi Kulkarni
Swathi Kulkarni el 12 de Oct. de 2020
clear
clc
Patient='Sarah';
Temp=102.6;
SBP=138;
DBP=76;
Charge =35;
fprintf('Sarah had a temperature of %4.1f and a blood pressure of %d / %d.\n The Charge for this visit is $%d.\n',Patient,Temp,SBP,DBP,Charge)
please tell me the fault in the code
The ouput should display Sarah had a temperature of102.6 and a blood pressure of 138/76.
The charge for this visit is $35.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by