Please find the fault in the mentioned code

1 view (last 30 days)
% 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.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Apr 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 Comments
Walter Roberson
Walter Roberson on 17 Apr 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.

Sign in to comment.

More Answers (2)

J. Webster
J. Webster on 15 Apr 2016
Please format it so that it's easier to read.
  1 Comment
pankaj saha
pankaj saha on 17 Apr 2016
Edited: pankaj saha on 17 Apr 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

Sign in to comment.


Swathi Kulkarni
Swathi Kulkarni on 12 Oct 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.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!