Set of 2 nonlinear ODE in mathamtica 9

In summary: Continue ReadingIn summary, the conversation discusses using NDSolve to solve a set of 2 non-linear ordinary differential equations. The code provided is for m[t] and uses NDSolve to obtain a plot for m[t]. The conversation also mentions the use of rules and the ReplaceAll function in Mathematica. The importance of correctly interpreting examples and saving them for future reference is also emphasized.
  • #1
mazgan
5
0
i just signed up here so i hope this is the right place.

i need to solve a set of 2 non-linear ordinary differencial equations.

i tryed using NDSolve but it doesn't really work so I am not sure what's wrong with my code.

here is my code (copy paste):

c = 0.1;
Subscript[sys,
B] = {2*m[t]*(m'[t])*
Sin [\[Lambda][t]] + ((m[t])^2 )*(\[Lambda]'[t])*
Cos[\[Lambda][t]] == -(c/(4*m[t]))*Tan[\[Lambda][t]/2],
m'[t] == -(c/ (16*(m[t]^2)))*( Tan[\[Lambda][t]/2]^2),
Sin[\[Lambda][0]] == 0.95, m[0] == 1};
Subscript[sol, B] = NDSolve[Subscript[sys, B], {m}, t]

Plot[m[t], {t, 0, 50}]

as u can see from the code the equations are:

(1) 2*m*m'*sinλ+m^2*λ'*cosλ = -(c/4m)*tan(λ/2)
(2) m' = -(c/16m^2)*(tan(λ/2))^2

where c=0.1 and also its known that:

sin(λ(t=0))=0.95
m(t=0)=1

i want to find m(t).
 
Physics news on Phys.org
  • #2
Small changes to make NDSolve happy and get a plot.

Code:
In[1]:= c = 0.1;
   sysB = {2*m[t]*m'[t]*Sin[\[Lambda][t]] + m[t]^2*\[Lambda]'[t]*Cos[\[Lambda][t]] == -c/(4*m[t])*Tan[\[Lambda][t]/2], 
   m'[t] == -c/(16*m[t]^2)*Tan[\[Lambda][t]/2]^2, Sin[\[Lambda][0]] == 0.95, m[0] == 1};
   solB = m[t] /. NDSolve[sysB, m[t], {t, 0, 50}]

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

Out[3]= {InterpolatingFunction[{{0. 18.8496}}, <>][t]}

and the result is

Code:
In[4]:= Plot[solB, {t, 0, 50}]

Out[4]= ...PlotSnipped...

Now you need to study this and make sure it is really correct and the result makes sense. Check the changes character by character until you can see exactly what all the changes were. If you want to you can then try reversing those changes, one at a time, and see if it continues to work or if it breaks. If it breaks then back off, try to understand what went wrong and see if you can find a way to have your original and have it still work.
 
Last edited:
  • #3
Bill Simpson said:
Small changes to make NDSolve happy and get a plot.

Code:
In[1]:= c = 0.1;
   sysB = {2*m[t]*m'[t]*Sin[\[Lambda][t]] + m[t]^2*\[Lambda]'[t]*Cos[\[Lambda][t]] == -c/(4*m[t])*Tan[\[Lambda][t]/2], 
   m'[t] == -c/(16*m[t]^2)*Tan[\[Lambda][t]/2]^2, Sin[\[Lambda][0]] == 0.95, m[0] == 1};
   solB = m[t] /. NDSolve[sysB, m[t], {t, 0, 50}]

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

Out[3]= {InterpolatingFunction[{{0. 18.8496}}, <>][t]}

and the result is

Code:
In[4]:= Plot[solB, {t, 0, 50}]

Out[4]= ...PlotSnipped...

Now you need to study this and make sure it is really correct and the result makes sense. Check the changes character by character until you can see exactly what all the changes were. If you want to you can then try reversing those changes, one at a time, and see if it continues to work or if it breaks. If it breaks then back off, try to understand what went wrong and see if you can find a way to have your original and have it still work.
first of all, thank you for ur help.

can u explain to me this line the code u wrote:
solB = m[t] /. NDSolve[sysB, m[t], {t, 0, 50}]

what exactly is being signed to solB? if i want to plot for m'[t] , how do i do it?
and what exactly the /. does?

when i wrote my code i simply used examples i saw for NDSolve and tried to write something similar but it turns out i did it wrong because it didnt work. I am trying to understand what was missing and what i did wrong, so ill know the next time i try writing something similiar.
 
  • #4
mazgan said:
can u explain to me this line the code u wrote:
solB = m[t] /. NDSolve[sysB, m[t], {t, 0, 50}]

what exactly is being signed to solB? if i want to plot for m'[t] , how do i do it?
and what exactly the /. does?

NDSolve, and DSolve and several other "solvers" in Mathematica, return "rules", sometimes multiple sets of rules, sometimes single rules. If you change that line to just NDSolve[sysB, m[t], {t, 0, 50}] then you can see the rule nested inside layers of { }. You can compare that with the value that is assigned to solB and see how this is changed.

Rules are not a number, not like solving x+4==0 for x and getting -4. Understanding how rules work is an important part of learning to use Mathematica.

/. is Mathematica shorthand for the ReplaceAll function in Mathematica and you can find position the cursor to the right of either of those, tap F1 and read all the documentation. In a sentence, what it is doing here is turning a rule into a definition of the function m[t].

mazgan said:
when i wrote my code i simply used examples i saw for NDSolve and tried to write something similar but it turns out i did it wrong because it didnt work. I am trying to understand what was missing and what i did wrong, so ill know the next time i try writing something similiar.

Correctly interpreting examples, particularly for differential equations and interpolating functions, can be difficult. When I've figured out how to correctly do this for a particular kind of problem that I will probably need to do again someday I save that along with carefully thought out notes to myself in a little Mathematica notebook that I keep on my desktop for future reference.
 
  • #5
Bill Simpson said:
NDSolve, and DSolve and several other "solvers" in Mathematica, return "rules", sometimes multiple sets of rules, sometimes single rules. If you change that line to just NDSolve[sysB, m[t], {t, 0, 50}] then you can see the rule nested inside layers of { }. You can compare that with the value that is assigned to solB and see how this is changed.

Rules are not a number, not like solving x+4==0 for x and getting -4. Understanding how rules work is an important part of learning to use Mathematica.

/. is Mathematica shorthand for the ReplaceAll function in Mathematica and you can find position the cursor to the right of either of those, tap F1 and read all the documentation. In a sentence, what it is doing here is turning a rule into a definition of the function m[t].



Correctly interpreting examples, particularly for differential equations and interpolating functions, can be difficult. When I've figured out how to correctly do this for a particular kind of problem that I will probably need to do again someday I save that along with carefully thought out notes to myself in a little Mathematica notebook that I keep on my desktop for future reference.

im in abit of a problem now. I am trying to write something more general for my problem, using functions instead of manually deriving the 2 equations and just solving them, can u help me get this working?

basicly i have 2 first order ODE for 2 functions: j[tau] , m[tau] with 2 starting conditions, but i express the ODE with functions of j[tau] and m[tau] that i defined in the code.
also i have 2 different models for one of those functions of j[tau] and m[tau] so there's 2 different sets of ODE.

but somehow when i try running it i get a lot of messages and i can't get any results. i don't think its because of the subscripts or greek letters because I've tested it on the code u gave earlier and they didnt do harm (as it should be).

here is the code:

Code:
c = 0.1;
Subscript[\[Lambda], 0] = ArcSin[0.95];
Subscript[m, 0] = 
  2/(Sin[Subscript[\[Lambda], 0]]*Tan[Subscript[\[Lambda], 0]/2]);
Subscript[j, 0] = 1;
Subscript[J, 0] = 1; (*arbitrary*)
Subscript[M, 0] = (Subscript[J, 0]/Sin[Subscript[\[Lambda], 0]])^(1/2);
Subscript[\[CapitalOmega], H, 0] = 
  Tan[Subscript[\[Lambda], 0]/2]/(2*Subscript[M, 0]);

M[m_] := m*Subscript[J, 0]*Subscript[\[CapitalOmega], H, 0];
J[j_] := j*Subscript[J, 0];
\[Lambda][j_, m_] := ArcSin[J[j]/(M[m]^2)];

Subscript[z, 1][j_, m_] := 
  1 + ((Cos[\[Lambda][j, m]])^2)*( (1 + Sin[\[Lambda][j, m]])^(1/
         3) + (1 - Sin[\[Lambda][j, m]])^(1/3) );
Subscript[z, 2][j_, 
   m_] := (3*(Sin[\[Lambda][j, m]])^2 + 
     Subscript[z, 1][\[Lambda][j, m]]^2)^(1/2);
SubPlus[z][j_, m_] := 
  3 + Subscript[z, 
    2][\[Lambda][j, 
     m]] - ( (3 - Subscript[z, 1][\[Lambda][j, m]])*(3 + 
        Subscript[z, 1][\[Lambda][j, m]] + 
        2*Subscript[z, 2][\[Lambda][j, m]]) )^(1/2); 

Subscript[\[CapitalOmega], H][j_, m_] := 
  Tan[\[Lambda][j, m]/2]/(2*M[m]);
Subscript[\[CapitalOmega], ISCO][j_, m_] := 
  1/(M[m]*(SubPlus[z][\[Lambda][j, m]]^(3/2) + Sin[\[Lambda][j, m]]));
Subscript[\[CapitalOmega], T, A][j_, m_] := 
  Subscript[\[CapitalOmega], ISCO][j, m];
Subscript[\[CapitalOmega], T, B][j_, m_] := (1/2)*
   Subscript[\[CapitalOmega], H][j, m];

Subscript[\[Omega], H][j_, m_] := 
  Subscript[\[CapitalOmega], H][j, m]/Subscript[\[CapitalOmega], H, 0];
Subscript[\[Omega], T, A][j_, m_] := 
  Subscript[\[CapitalOmega], T, A][j, m]/Subscript[\[CapitalOmega], H,
    0];
Subscript[\[Omega], T, B][j_, m_] := 
  Subscript[\[CapitalOmega], T, B][j, m]/Subscript[\[CapitalOmega], H,
    0];

(* the normilized equations of motion using \[Tau]=Subscript[\
\[CapitalOmega], H,0]*t *)

Subscript[sys, 
  A] = {j'[\[Tau]] == -c*(Subscript[\[Omega], H][j[\[Tau]], 
        m[\[Tau]]] - Subscript[\[Omega], T, A][j[\[Tau]], m[\[Tau]]]),
    m'[\[Tau]] == 
    Subscript[\[Omega], T, A][j[\[Tau]], m[\[Tau]]]*j'[\[Tau]], 
   j[0] == Subscript[j, 0], m[0] == Subscript[m, 0]};
Subscript[sol, A] = 
 m[\[Tau]] /. NDSolve[Subscript[sys, A], m[\[Tau]], {\[Tau], 0, 50}]
Plot[Subscript[sol, A], {\[Tau], 0, 80}]

Subscript[sys, 
  B] = {j'[\[Tau]] == -c*(Subscript[\[Omega], H][j[\[Tau]], 
        m[\[Tau]]] - Subscript[\[Omega], T, B][j[\[Tau]], m[\[Tau]]]),
    m'[\[Tau]] == 
    Subscript[\[Omega], T, B][j[\[Tau]], m[\[Tau]]]*j'[\[Tau]], 
   j[0] == Subscript[j, 0], m[0] == Subscript[m, 0]};
Subscript[sol, B] = 
 m[\[Tau]] /. NDSolve[Subscript[sys, B], m[\[Tau]], {\[Tau], 0, 50}]
Plot[Subscript[sol, B], {\[Tau], 0, 80}]


i know its a lot to ask but I am kinda hopeless here i don't know what to do...

and btw, is there a way for me to write here the code so it will show the greek letters and the subscripts directly and not write the long tags for them instead, so it will be like i see it in mathmatica in my computer?
 
Last edited:
  • #6
What I would suggest is that you take the time to become an expert at understanding exactly how subscripts really work at all levels inside Mathematica. Hint: They are not variables, really, you might think they are variables, they might look like variables, in very simple cases they can pretend to act like variables, but they are not variables, really. I am told that they are kind of functions, but not in a way that I have ever really been able to understand. And in all the years I have never found tools that really show how they actually work at the deepest levels.

Different issues:

You define SubPlus[z] to accept two arguments, j and m, and then you use it SubPlus[z],[Lambda[j,m]]. I'm guessing one or the other of those is an error. Likewise for z2. Likewise for z1. If I guess what those might be, delete all your subscripts, try to correct all the mistakes I made trying to get rid of those and make a few more changes then I can at least get plots, but have no idea whether the result has anything to do with the real world. And the result is nothing like what you are going to want. <delete>

Good luck. I hope you succeed. If you do then perhaps you can write a brilliantly clear paper that even I could understand and which explains exactly how subscripts really really work deep down inside Mathematica. Someone needs to do it. You might include the more general answer of how people can desktop publish their Mathematica input and still get it to work. If you can do that right then you could probably sell that.
 
  • #7
Bill Simpson said:
What I would suggest is that you take the time to become an expert at understanding exactly how subscripts really work at all levels inside Mathematica. Hint: They are not variables, really, you might think they are variables, they might look like variables, in very simple cases they can pretend to act like variables, but they are not variables, really. I am told that they are kind of functions, but not in a way that I have ever really been able to understand. And in all the years I have never found tools that really show how they actually work at the deepest levels.

Different issues:

You define SubPlus[z] to accept two arguments, j and m, and then you use it SubPlus[z],[Lambda[j,m]]. I'm guessing one or the other of those is an error. Likewise for z2. Likewise for z1. If I guess what those might be, delete all your subscripts, try to correct all the mistakes I made trying to get rid of those and make a few more changes then I can at least get plots, but have no idea whether the result has anything to do with the real world. And the result is nothing like what you are going to want. <delete>

Good luck. I hope you succeed. If you do then perhaps you can write a brilliantly clear paper that even I could understand and which explains exactly how subscripts really really work deep down inside Mathematica. Someone needs to do it. You might include the more general answer of how people can desktop publish their Mathematica input and still get it to work. If you can do that right then you could probably sell that.

im not sure i have the time to become and expert in this.
if instead of subscript i use something like Ω_T' its going to be a proper variable? or i can't use greek letters either and i must write something like 'omega_T' ?

i have corrected the mistake in the functions of z+ z1 z2 and still gotten many messages. some how it drew a plot for the model B set of equations, I am not sure if its right, but I am sure that there's a lot of wrong things in the code somehow due to all those messages i get.

i will try converting all those subscripts to just simple names, but i need to know - using greek letters is a problem?
also - for the purpose of defining variables, mathmatica is case sensitive right? so i can use both OMEGA_T and omega_T for 2 different variables right?
 
  • #8
mazgan said:
if instead of subscript i use something like Ω_T' its going to be a proper variable? or i can't use greek letters either and i must write something like 'omega_T' ?

Underscore _ has a meaning buried deep in Mathematica and you are probably going to have nothing but trouble if you try that.

mazgan said:
i have corrected the mistake in the functions of z+ z1 z2 and still gotten many messages. some how it drew a plot for the model B set of equations, I am not sure if its right, but I am sure that there's a lot of wrong things in the code somehow due to all those messages i get.

That is probably a good guess.

mazgan said:
i will try converting all those subscripts to just simple names, but i need to know - using greek letters is a problem?
also - for the purpose of defining variables, mathmatica is case sensitive right? so i can use both OMEGA_T and omega_T for 2 different variables right?

Anything that already has a defined meaning for Mathematica is going to give you grief. Naming something I or E or N or + or Sin or Solve or ... is going to cause problems. If you think you have a good name you might try looking that up in the help system. If it isn't there then you are less likely to have a problem, but Omega_T isn't going to be in the help system and still isn't going to work. Yes it is case sensitive.

You might get yourself a good introduction to Mathematica book and learn some of the general knowledge about how to avoid problems. That will save you a lot of time in the future.

If it is possible then try doing things one step or one function at a time. Write that. Test that. Try to figure out what the errors mean. After you get that correct then add the next layer of complexity. Banging in a whole program, pushing Enter and hoping it somehow will work is usually going to take more time than getting it right one simple step at a time.
 
  • #9
Bill Simpson said:
Underscore _ has a meaning buried deep in Mathematica and you are probably going to have nothing but trouble if you try that.



That is probably a good guess.



Anything that already has a defined meaning for Mathematica is going to give you grief. Naming something I or E or N or + or Sin or Solve or ... is going to cause problems. If you think you have a good name you might try looking that up in the help system. If it isn't there then you are less likely to have a problem, but Omega_T isn't going to be in the help system and still isn't going to work. Yes it is case sensitive.

You might get yourself a good introduction to Mathematica book and learn some of the general knowledge about how to avoid problems. That will save you a lot of time in the future.

If it is possible then try doing things one step or one function at a time. Write that. Test that. Try to figure out what the errors mean. After you get that correct then add the next layer of complexity. Banging in a whole program, pushing Enter and hoping it somehow will work is usually going to take more time than getting it right one simple step at a time.

one thing I am still unsure of is whether or not using greek letters without any subscripts is fine, and won't cause the problems subscripts cause?

ive tried looking for a way to somehow still be able to work with those subscripts. according to here:
http://mathematica.stackexchange.co...th-subscript-overscript-superscript-etc/20208

i can use the line :

Needs["Notation`"]

if i understand correctly, it invokes the Notation package, and then for example i can use:

Symbolize[Subscript[m, 0]]

and after that perphaps Subscript[m, 0] will work properly as i need. are you familiar with this?
maybe i can try doing it for all the subscripts in my code and see if it works..
 
  • #10
mazgan said:
one thing I am still unsure of is whether or not using greek letters without any subscripts is fine, and won't cause the problems subscripts cause?

Try it and see what happens. Unfortunately it isn't like the stone age when we had documents which described exactly in detail what was and was not allowed in a language.

mazgan said:
ive tried looking for a way to somehow still be able to work with those subscripts.

I know. That is exactly what I said happens to some people several messages ago.

Try reading SubscriptedVariables101.nb written by one of the Wolfram people after the umpteenth "why is my code broke?" post. That document doesn't explain what any of the problems with subscript really are. All that does is describe how to use one method to get around one kind of problem with subscript. But perhaps that will be enough for you.

Good luck on your quest.
 
Last edited:

1. What is a nonlinear ODE?

A nonlinear ODE (Ordinary Differential Equation) is a mathematical equation that describes how one variable changes in relation to another variable, but with nonlinear terms (such as powers or products) involved. Nonlinear ODEs cannot be solved using traditional methods and require numerical methods or approximations.

2. How do you solve a set of 2 nonlinear ODEs in Mathematica 9?

To solve a set of 2 nonlinear ODEs in Mathematica 9, you can use the DSolve function. This function takes the equations, initial conditions, and variables as inputs and returns the general solution. It is important to note that not all nonlinear ODEs have analytical solutions and therefore may require numerical methods.

3. Can Mathematica 9 handle higher order nonlinear ODEs?

Yes, Mathematica 9 can handle higher order nonlinear ODEs. It has built-in functions like DSolve and NDSolve that can handle ODEs of any order. However, the complexity and accuracy of the solutions may vary depending on the specific ODE.

4. Is it possible to plot the solutions of a set of 2 nonlinear ODEs in Mathematica 9?

Yes, it is possible to plot the solutions of a set of 2 nonlinear ODEs in Mathematica 9. You can use the Plot function and input the solution obtained from DSolve or NDSolve as the function to be plotted. This allows you to visualize the behavior of the system over time or with different initial conditions.

5. Are there any resources available for learning how to use Mathematica 9 for solving nonlinear ODEs?

Yes, there are many resources available for learning how to use Mathematica 9 for solving nonlinear ODEs. You can refer to the official Mathematica documentation, online tutorials and guides, or attend workshops and seminars. There are also various books and online communities dedicated to Mathematica that can provide support and guidance for solving nonlinear ODEs in Mathematica 9.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
241
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
204
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Introductory Physics Homework Help
Replies
2
Views
873
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
925
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
Back
Top