Please find the fault in the mentioned code

1 次查看(过去 30 天)
% 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.

采纳的回答

Walter Roberson
Walter Roberson 2016-4-17
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 个评论
Walter Roberson
Walter Roberson 2016-4-17
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.

请先登录,再进行评论。

更多回答(2 个)

J. Webster
J. Webster 2016-4-15
Please format it so that it's easier to read.
  1 个评论
pankaj saha
pankaj saha 2016-4-17
编辑:pankaj saha 2016-4-17
% 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

请先登录,再进行评论。


Swathi Kulkarni
Swathi Kulkarni 2020-10-12
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.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by