Stiff Differential Equation solver (Euler?)
Show older comments
Hello, I'm looking for a fixed-step integrating function capable of solving stiff differential equations, with a really small step size (below e-14).
Is this possible? Does MATLAB have one? I couldn't make it work with the built-in ode*() functions.
Anything out there available? Maybe working on the Euler method?
NLN
14 Comments
Torsten
on 6 Oct 2022
No. But the Euler method (although completely inadequate for stiff differential equations) is easily programmed.
Nuno Nunes
on 7 Oct 2022
I read your other post in the forum.
You can easily output all variables you like from the ode function by just adding them as output arguments:
x0 = 1;
tspan = [0 1];
[T,Y] = ode45(@fun,tspan,x0);
for i = 1:numel(T)
[~,par1(i),par2(i)] = fun(T(i),Y(i));
end
plot(par1,par2)
function [dy,par1,par2] = fun(t,y)
par1 = t;
par2 = y;
dy = -y;
end
Maybe this makes your question here superfluous.
Nuno Nunes
on 7 Oct 2022
Edited: Nuno Nunes
on 8 Oct 2022
I don't understand what you want to tell us with your comment.
Do you have problems calling the function "StraightRun" after ode45 has finished and get the additional outputs ?
In other words: Does the way I suggested above not work ?
You know that you'd better call as
[T,Y] = ode45(@StraightRun,t,y0);
than
[T,Y] = ode45('StraightRun',t,y0);
?
Nuno Nunes
on 8 Oct 2022
Torsten
on 8 Oct 2022
As you can easily see from the code I included, you must call "StraightRun" for all the times returned from ODE45 with the results for t and y you got. Then your variables that you included in the output list will be recomputed for these t and y values you supplied.
Nuno Nunes
on 8 Oct 2022
Walter Roberson
on 8 Oct 2022
for i = 1:numel(T)
[~, information(i,:)] = StraightRun(T(i),Y(i));
end
Nuno Nunes
on 8 Oct 2022
I don't know the size of the array "information" that you return from StraightRun.
Adapt it according to what you have programmed.
for i = 1:numel(T)
[~, information] = StraightRun(T(i),Y(i,:));
end
Nuno Nunes
on 8 Oct 2022
If "information" in "StraightRun" is a row vector,
for i = 1:numel(T)
[~, information(i,:)] = StraightRun(T(i),Y(i,:));
end
Of course, your function StraightRun must have the form
function [dy,information] = StraightRun(t,y)
I hope all variables involved (V_A,beta_A,alpha_S,X,Y,N,X_AS,Y_AS,N_AS) are scalar values.
Nuno Nunes
on 8 Oct 2022
Answers (2)
John D'Errico
on 7 Oct 2022
Edited: John D'Errico
on 7 Oct 2022
You SERIOUSLY do not want to use a standard Euler's method to solve a stiff ODE. You will be wasting your time. Why do you think you want to use Euler here, when better methods are available for stiff problems?
Worse, trying to use a step size of 1e-16 is just asking for numerical problems. This will NEVER be a good idea. Period.
Honestly, seriously, you do NOT want to use a simple forwards Euler method here. I don't know why you think you do. But you DON'T.
Having said all of that, the backwards Euler method is an option.
I won't write the code for you. But the backwards (implicit) Euler method should generally be stable for stiff problems. You may still need a fine step size, but 1e-16 is just obscene.
Do some reading before you proceed, if you really think you need to write this yourself.
Having said all of that, why in the name of god and little green apples do you want to write an ODE solver code yourself? This is especially true if you don't even know the basics of these codes? Use existing code when it is available. Do you think you will write better code than that from professionals who know very well how to do the numerical analysis? Never look to write your own code, unless it is a homework assignment.
In this case, you will want to use tools like ode15s or ode23s.
help ode15s
help ode23s
5 Comments
Nuno Nunes
on 7 Oct 2022
If I were you, I'd check the equations you are trying to solve and the way you implemented them, but not try to write my own solver.
John D'Errico
on 7 Oct 2022
Edited: John D'Errico
on 7 Oct 2022
(NO. The method CANNOT be a standard forward Euler's method. It will not be stable for a stiff problem. It COULD POSSIBLY be a backwards Euler though. That might depend on your skill in coding, and you knowledge of the numerical method.)
However, you are using MATLAB. It is silly to be forced to wite your own code to replace a part of MATLAB. Your advisor is insane. Tell them I said so, not that it will really matter in some cases.
Would your adisor tell you to implement addition and multiplication of numbers on your own in MATLAB, because this is your thesis? Sorry, but that is just plain dumb. Ok, don't tell your advisor that. Or do tell them. I don't care what they think of me anyway. :)
My point is still fully valid. If you are using MATLAB to solve this problem, then just use MATLAB! Using MATLAB PROPERLY to solve the problem means you need to use the correct code from MATLAB to solve it. In this case, the solution is to use the stiff ODE solvers already part of MATLAB itself. Don't write your own code! I you learn nothing more here, it is that.
Freekin' bleepin advisors.
Anyway, IF you are seriously given no choice, then this is YOUR thesis in the end. You would need to spend the mental energy to learn to write a BACKWARDS (implicit) Euler method. You can surely find pseudo-code for a backwards Euler.
Nuno Nunes
on 7 Oct 2022
John D'Errico
on 7 Oct 2022
If there never would have been a complaint had you been able to successfully use ode45, then there cannot possibly be a valid problem if you use ode15s. Both tools are essentially part of the very same suite of codes. The only important factor lies in knowing which code is the correct tool to solve the problem.
Walter Roberson
on 8 Oct 2022
0 votes
Code for fixed step solvers is at https://www.mathworks.com/matlabcentral/answers/98293-is-there-a-fixed-step-ordinary-differential-equation-ode-solver-in-matlab-8-0-r2012b#answer_107643
If this is not able to operate at a fine enough time step then you may need to alter the code to use the symbolic toolbox.
1 Comment
Walter Roberson
on 8 Oct 2022
personally I think it likely that your equations, at least as implemented, have an unavoidable singularity.
- the equations might be wrong
- you might have coded them incorrectly
- the problem might possibly not be solvable using the techniques that you are using
I am a big fan of setting up the equations using the symbolic toolbox, which makes it much easier to follow the equations to be sure that they have been expressed correctly, especially if you use Livescript (better output format). Then use the work flow shown in the first example of odeFunction to convert the symbolic expressions for numeric solutions.
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!