2
$\begingroup$

Currently I am working with Mathematica to learn the program, and I'm confused what exactly is wrong with this code.

s = NDSolve[{if[t < 1, Derivative[1][Ca][T] == -10*Ca[T] + 2 T, 
    Derivative[1][Ca] == -10*Ca[T]], 
   Derivative[1][Cb][T] == (10*Ca[T]) - 0.192, Ca[0] == 0, 
   Cb[0] == 0}, {Ca[T], Cb[T]}, {T, 0, 10}]

What I'm trying to do is set a conditional for the Ca derivative so that if T < 1 then it goes in the first derivative, but if not, it'll go in the second derivative.

Can anyone help me with this? Thanks.

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Apr 26 '15 at 23:52
  • $\begingroup$ You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. $\endgroup$ – Michael E2 Apr 26 '15 at 23:53
  • $\begingroup$ Please note that precise and accurate syntax is essential for any programming language. In Mathematica, built-in symbols and functions start with capital letters, such as If. It is better to avoid starting your own variable and function names with a capital (i.e. avoid T, but especially built-in functions like D and N). $\endgroup$ – Michael E2 Apr 27 '15 at 0:03
4
$\begingroup$

Use Piecewise for discontinuous right-hand sides and coefficients. If, with a capital I, more a programming construct than an algebraic/functional one. NDSolve does discontinuity processing, which improves error estimation and step size when done accurately; using Piecewise helps with that.

s = NDSolve[{
    Derivative[1][Ca][t] == Piecewise[{{-10*Ca[t] + 2 t, t < 1}}, -10*Ca[t]], 
    Derivative[1][Cb][t] == (10*Ca[t]) - 0.192, Ca[0] == 0, 
    Cb[0] == 0}, {Ca, Cb}, {t, 0, 10}];

Plot[{Ca[t], Cb[t]} /. s // Evaluate, {t, 0, 10}]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Thanks a ton! This helped a lot. Next time I'll use the Piecewise function instead of the if statement. $\endgroup$ – Joseph Kessler Apr 27 '15 at 0:11
  • $\begingroup$ @JosephKessler You're welcome. $\endgroup$ – Michael E2 Apr 27 '15 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.