Solving ODEs in Matlab for a Non-System Problem

  • Context: MATLAB 
  • Thread starter Thread starter Allamarein
  • Start date Start date
  • Tags Tags
    Matlab Odes System
Click For Summary

Discussion Overview

The discussion revolves around solving a system of ordinary differential equations (ODEs) in Matlab, focusing on the challenges faced due to the nonlinearity of the equations and the initial conditions provided. Participants explore various methods for numerical solutions, including the Runge-Kutta method, and discuss the implications of numerical errors and the behavior of the system.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a Matlab script for a system of ODEs, expressing uncertainty about the correctness of the results despite having defined initial conditions.
  • Another participant suggests using the Runge-Kutta method, emphasizing the importance of knowing initial conditions for accurate solutions.
  • Concerns are raised about the conditioning of the functions and the potential impact of having initial conditions that differ by orders of magnitude.
  • Participants discuss the nonlinearity of the equations and suggest linearizing them to simplify the problem and improve solution robustness.
  • There is a suggestion to expand trigonometric terms near the initial condition to better understand the behavior of the equations.
  • One participant expresses disappointment in Matlab's lack of warnings for numerical issues, questioning the reliability of the solutions provided by the software.
  • Another participant clarifies that nonlinearity does not imply multiple solutions but highlights the sensitivity of numerical methods to errors in such cases.
  • Discussion includes the need for careful selection of solvers in Matlab, as different solvers are suited for different types of problems.
  • Participants share insights on the complexity of the mathematical model and the necessity of mastering simpler problems before tackling more complicated systems.

Areas of Agreement / Disagreement

Participants express a range of views regarding the challenges of solving the ODEs, with some agreeing on the importance of linearization and careful numerical analysis, while others highlight different aspects of the problem, such as initial conditions and solver selection. No consensus is reached on a definitive solution or approach.

Contextual Notes

Participants note that the equations are highly nonlinear, which complicates the solution process. There are discussions about the conditioning of the functions and the behavior of the system near specific initial conditions. The conversation reflects a variety of assumptions and conditions that may affect the outcomes.

Allamarein
Messages
11
Reaction score
0
Let's suppose I know every coefficients this script:
and
y1 alfa
y2 v
y3 A
y4 T

function dy=isaacsimply(s,y)
dy = zeros(4,1)
global ...
dy(1)=(Cd_for*q_inf*h*(sin(y(1)))^2+...
g*y(3)*(rho-rho_inf)*cos(y(1))...
+E*U_inf*sin(y(3)))/...
(-rho*y(3)*y(2)^2); %dalfa
d_alfa=(Cd_for*q_inf*h*(sin(y(1)))^2+...
g*y(3)*(rho-rho_inf)*cos(y(1))...
+E*U_inf*sin(y(3)))/...
(-rho*y(3)*y(2)^2);

dy(2)=(q_inf*y(3)*sin(y(1))*cos(y(1))*d_alfa...
-g*y(3)*(rho-rho_inf)*sin(y(1))+...
E*U_inf*cos(y(1))-...
pi*h*rho*beta*(y(2)-U_inf*cos(y(1)))^2-...
y(2)*E)/(rho*y(2)*y(3)); %dv
d_v= (q_inf*y(3)*sin(y(1))*cos(y(1))*d_alfa...
-g*y(3)*(rho-rho_inf)*sin(y(1))+...
E*U_inf*cos(y(1))-...
pi*h*rho*beta*(y(2)-U_inf*cos(y(1)))^2-...
y(2)*E)/(rho*y(2)*y(3));

dy(3)=(E-y(3)*rho*d_v)/(rho*y(2)); %dA
d_A=(E-y(3)*rho*d_v)/(rho*y(2));

dy(4)=(E*Cp*y(4)+Nu/d*k*C*(T_inf-y(4))...
-rho*y(2)*Cp*y(4)*d_A-rho*y(3)*Cp*y(4)*d_v)...
/(rho*y(3)*Cp*y(2)); % dT

This script solves the system.jpg?
 

Attachments

  • system.jpg
    system.jpg
    17.8 KB · Views: 493
Physics news on Phys.org
I'd use Runge-Kutta on this one as long as you know the initial conditions...
 
Dr Transport said:
I'd use Runge-Kutta on this one as long as you know the initial conditions...

ICs:

y1=pi/2;
y2=51.6;
y3=1.96e-7
y4=300

I'd know my transposition from mathematical problem to Matlab is correct. My results tell me I am wrong.
 
At a quick look what you have listed as d_v is actually y(2)...

You also must look and see how well conditioned your functions are, i.e. your initial conditions are orders of magnitude different...That makes a huge difference in how well the solution is calculated.
 
Dr Transport said:
At a quick look what you have listed as d_v is actually y(2)...
d_v=dy(2);
I used this expedient because I thought typing dy(2) directly, Matlab uses that dy(2) and not that calculated at precedent step. In a word it is a complicated story, but now I suppose defining d_v is useless.

Dr Transport said:
You also must look and see how well conditioned your functions are, i.e. your initial conditions are orders of magnitude different...That makes a huge difference in how well the solution is calculated.
You'd be right, but these are my ICs. I supposed Matlab should give a warning (i.e an out tollerance value) if it finds troubles. If Matlab gives me a solution, I should trust that is correct (and fortunately I don't)
Anyway, maybe should I try to convert my system in a dimensionless one?
 
Last edited:
Your equations are highly nonlinear, there are many issues with getting a robust solution. Linearize them to get a 1st order, then add in layers of complexity to ensure you don't get off track.
 
Dr Transport said:
Your equations are highly nonlinear, [...]. Linearize them to get a 1st order, then add in layers of complexity to ensure you don't get off track.

It seem a good hints. May you suggest a first guess of a simpler system? How could I linearize my system.jpg? I am not very experienced in this kind of thing.
Anyway I am disapponited: I thought Matlab reports these kind of troubles when it solves ODE
 
Since your initial conditions for \alpha are at \pi/2 try expanding all of you trigonometric terms near that point to get an idea of how the set of equations behaves initially.


As for Matlab reporting troubles, it is just a numerical analysis tool, it doesn't know when a solution goes off in a wrong direction as long as it is getting numbers, it'll keep spitting them out until you tell it to stop. Judging the correctness of the solution is what you have to figure out. this is why I suggested working on linearizing first, you build up a confidence level in your solution space. You linearize, you set variables to the max and min that they will ever be to eliminate terms, etc... all of a sudden, you have mapped the solution space and you have understanding.
 
Dr Transport said:
Since your initial conditions for \alpha are at \pi/2 try expanding all of you trigonometric terms near that point to get an idea of how the set of equations behaves initially.

Dr Transport seems very stong in this kind of problems.
Please be patient with me: it' s the first time I tackle ODEs system and I should join the fray without a solid preparation.
Could you suggest a reference to learn how solve this kind of problem?

Anyway if I understand I should write a Taylor's expansion about \pi/2, therefore:
\sin(\alpha) \approx 1-1/2\, \left( \alpha-1/2\,\pi \right) ^{2}
\cos(\alpha) \approx -\alpha+1/2\,\pi
\sin ^2 (\alpha ) \approx 1 - \left( {\alpha - 1/2\,\pi } \right)^2
or better at the first order:
\sin (\alpha ) \approx 1 - {1 \over 2}\left( {{{\pi ^2 } \over 4} - \alpha \pi } \right)
\cos(\alpha) \approx -\alpha
and try to see where my plot goes.

If I understand my ODEs system is non-linear, so it has more than a solution. Hence Matlab could offer a solution different from that I can read on my reference (and that is confirmed in experimental tests).
I typed bunkum or more or less that's the point?
Let's suppose this is the point, nevertheless my ODEs is derived by governing equations. Therefore, given ICs, the behavior of my unknows (angle, velocity, area, temperature) should be only.
Probably I typed a lot of bunkum :-)
 
Last edited:
  • #10
No, non-linear does not mean that it has more than one solution.
It does however mean that you have to be very careful when you solve it because it will be very susceptible to numerical errors (and your system is likely to be chaotic).
You also need to be careful when you choose which solver the use, there is a reason for why there are so many different solvers available in Matlab, they are good a different things.
 
  • #11
f95toli said:
No, non-linear does not mean that it has more than one solution.
It does however mean that you have to be very careful when you solve it because it will be very susceptible to numerical errors (and your system is likely to be chaotic).
You also need to be careful when you choose which solver the use, there is a reason for why there are so many different solvers available in Matlab, they are good a different things.

I suspected this behavior. In fact I tried to use different solver (ode45, ode113, ode15s,...).
In order to learn ODE solving in Matlab I read a lot of tutorials, but they offer easy problems. I consider Matlab should be helpful when the system is complicated (like mine).
I wouldn't be a nuisance, but if it is only a numerical errors matter, I expect that Matlab gives me a warning when it has a problem, otherwise I never trust in its solutions.
Because in other situations, Matlab warns me when it has a large tollerance error and the solution si not reliable, I supposed it warns me in this case too, or they are two different circumstances?

Anyway I think to need a noble soul has a browse through my M.files or else a pithy tutorial.
 
Last edited:
  • #12
My suggestion is to expand the sinusiodal terms to low order, and go from there. Now, another trick to get some insight is to set all of the derivative terms on the RHS to a constant and plot the RHS as a function of \alpha etc... You might see something interesting. Chaotic behavior in coupled equations comes about in very subtle ways, and given the complexity, I'd bet that numerical solution will take some time to nail down.
 
  • #13
Allamarein said:
In order to learn ODE solving in Matlab I read a lot of tutorials, but they offer easy problems. I consider Matlab should be helpful when the system is complicated (like mine).

But Matlab can never be more than a very good tool. It is a software package, not a mathematician-in-a-box.
The "easy problems" you refer to is a good start and teaches you how to use Matlab, definitely you need to understand how to solve "uncomplicated" problems before you tackle something as complicated as your system.
The main problem here is not the software; the problem is simply that the math/physics is very complicated.
 
  • #14
I tried to use your wise hints.
I used taylor series for trigonometric functions
<br /> \eqalign{<br /> &amp; \sin \left( \alpha \right) \approx 1 - {1 \over 2}\left( {\alpha - {1 \over 2}\pi } \right)^2 \cr <br /> &amp; \cos \left( \alpha \right) \approx - \alpha + {1 \over 2}\pi \cr} <br />
I didn't discover a large difference.

I uploaded results for the case with taylor expansion and for the "exact" system.
The path.jpg is another story:
when I calculated \alpha I can plot my path in X-Y plane, moving of a ds in the direction suggested from the angle.
X\left( i \right) = X(i - 1) + \cos \left( \alpha \right)ds
Y\left( i \right) = Y\left( {i - 1} \right) + \sin \left( \alpha \right)ds

Black line is the path born by ODEs
Yellow,green and red paths are calculated with correlation equation or read from reference.
Black line should be similar at the red one ultimately.
 

Attachments

  • path.jpg
    path.jpg
    20.5 KB · Views: 503
  • results.jpg
    results.jpg
    25.2 KB · Views: 473
  • results_taylor.jpg
    results_taylor.jpg
    25.2 KB · Views: 479
  • #15
Now your getting there, keep plugging along, you've made progress.
 
  • #16
Dr Transport said:
Now your getting there, keep plugging along, you've made progress.

Plugging? Progress?
Sincerely I think to be in the middle of the ocean and I know where is the coast.
Would you like to see my M.files?
 
  • #17
Part of the learning/education process is working these issues to an end and becoming comfortable in working on your own. You think you have not gotten anywhere, but in all actuality, you have made a bunch of progress towards your goal.
 
  • #18
Dr Transport said:
Part of the learning/education process is working these issues to an end and becoming comfortable in working on your own. You think you have not gotten anywhere, but in all actuality, you have made a bunch of progress towards your goal.

You are right. I hope to achieve my goal quickly.
Early I submit you a new problem! It's very similar..but it is not a system.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K