# Problem with matlab

1 view (last 30 days)
Javier on 26 Jul 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello, can anyone help me with the following problem matlab:
I have the equation: dy/dt=((-m/0.6)*(y/(23500+y))*X)+((1/10)*(47806-y))
Where m and X have a specific value for each time If t=45 --> m=6607 --> X=0,17 If t=47--> m=6407 --> X=0,19 t=49--> m=6483 --> X=0,19 . . . If t=84--> m=6367--> X=0,19
y(45)=20000
I need to get matlab values of "y" for each time through a ode45: [t, y] = ode45 ('function', [45.84], 20000);
But the program always ends giving error, If anyone knows how to solve the problem, please I need to solve it urgent.
Thank
Javier on 27 Jul 2011
I tried two ways to solve it:
The 1st I defined the parameters one by one as follows:
function dy=mifun(t,y)
if t==45;
m(45)=0.17;
X(45)=6607;
elseif t==47;
m(47)=0.19;
X(47)=6407;
...
elseif t==61;
m(61)=0.19;
X(61)=6300;
y(45)=20000;
dy=((-m/0.6)*(y/(23500+y))*X)+((1/10)*(47806-y));
[t,y]=ode45(@mifun,[45,61],20000);
end
But to run in matlab do not get anything, not results or error.
The 2nd way I tried is defining the parameters "t", "X" and "m"
from an excel file:
function dy=rhs(t,y)
y(45)=20000;
dy=((-m/0.6)*(y/(23500+y))*X)+((1/10)*(47806-y));
[t,y]=ode45(@rhs,[45,61],20000);
end
The error message is as follow:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> rhs at 14
dy=((-m/0.6)*(y/(23500+y))*X)+((1/10)*(47806-y));

Ivan van der Kroon on 26 Jul 2011
Your function should assign values to m and X depending on the value of t. Note that t in the solver can take any value in your interval hence you should specify the function in a continuous sense.
For instance, if you specified them with a if-statement like
if t==47,
elseif t==47,
end
it will not work as you did not specify the values in between. I would suggest defining the function your ode calls like this:
function dy=rhs(t,y)
m=funm(t);
X=funX(t);
dy=((-m/0.6)*(y/(23500+y))*X)+((1/10)*(47806-y));
end
And make two more functions funm and funX. I guess it gives you more overview in this way. If these functions are not continuous, you can make them so by interp1 (which even allows you to make them like a step function).
##### 2 CommentsShowHide 1 older comment
Jan on 26 Jul 2011
@Javier: Strange question. If your function funm and funX use INTERP1, INTERP1 is used, otherwise it is not used. It's your decision.
@Ivan: This method will not produce an accurate integration. The stepsize control and integration scheme of ODE45 will fail tremendously if the integrated function is not smooth. Imagine a step from 46.99 to 47.01, when the parameters change at 47.0: The intermediate ODE evaluations use different parameters and if the step if rejected the new stepsize is calculated based on the evaluated trajectory. This leads to unpredictable results. It is very important for intergration not to insert any discontinuities in the integrand: No IF, no MIN or MAX, no CLOCK or other external dependencies.
If the integrand is discontinuous, use events to separate the different intervals. See the documentation of ODE45.

### Categories

Find more on Programming 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!