1
$\begingroup$

I'm trying to optimize some kind of process in Mathematica. I did it without any problems in Matlab and now I'm trying to do the same in Mathematica which I don't know at all. I'm trying to find the best value of variable u for my proces, but that's not what I'm going to ask. My code at least should print eleven values of variable 'wynik' but it doesn't. I suppose that I've got some stupid and minor mistake in syntax, but as I've said earlier - I'm new at Mathematica environment. Any thoughts?

For[u == 1, u <= 1, u == u + 0.1,  
    s = NDSolve[{x1'[t] == u*(10*x2[t] - x1[t]),  x1[0] == 1,   
                x2'[t] == u*(x1[t] - 10*x2[t]) - (1 - u)*x2[t], x2[0] == 0}, 
                 {x1, x2}, {t, 0, 1}];  
    wynik == 1 - Evaluate[{x1[1] /. s} + {x2[1] /. s}]; 
    Print[wynik]]
$\endgroup$
3
  • 3
    $\begingroup$ But you wrote For[u == 1, u <= 1, so how do you expect this to run 11 times? try may be For[u = 0, u <= 1..... (note also difference between = and ==). These are not the same in Mathematica. see help on reference.wolfram.com/language/ref/For.html also better to use Do if you need to make explicit loops. $\endgroup$
    – Nasser
    Mar 18, 2016 at 14:14
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 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$ Mar 18, 2016 at 14:17
  • $\begingroup$ As far as I understand your code, I think you should really not use For, and probably not even Print (I admit, I used it quite a bit 20 years ago when I was green). Instead, you should consider Functions, Tables, or other functional concepts. My personal preference is to avoid imperative programming and explicit loops whenever practical. Mathematica is really suited for this approach, and as a result you'll feel a lot less like you're fighting against it. $\endgroup$
    – kirma
    Mar 19, 2016 at 15:17

2 Answers 2

7
$\begingroup$

Actually your system can be solved exactly:

s = DSolve[
           {x1'[t] ==  u (10 x2[t] - x1[t]),
            x2'[t] == -u (10 x2[t] - x1[t]) - (1 - u) x2[t],
            x1[0] == 1, x2[0] == 0},
           {x1, x2}, t];
sol = {x1, x2} /. First@s;
Plot3D[Through[sol[t]] /. u -> v, {t, 0, 1}, {v, 0, 10}]

Mathematica graphics

So your wynik behavior is

Plot[1 - Plus @@ (Through[sol[t]] /. u -> v) /. t -> 1, {v, 0, 1}]

Mathematica graphics

And the maximum is at

NMaximize[1 - Plus @@ (Through[sol[t]] /. u -> v) /. t -> 1, v]

( {0.0391638, {v -> 0.3144}} *)
$\endgroup$
1
  • $\begingroup$ One more questiion. How can I obtain the maximum value of wynik and which u responds to that? $\endgroup$
    – K.Kozieł
    Mar 18, 2016 at 14:26
4
$\begingroup$

Corrected version of the For-loop:

For[u = 0, u <= 1, u = u + 0.1, 
 s = NDSolve[{x1'[t] == u*(10*x2[t] - x1[t]), x1[0] == 1, 
    x2'[t] == u*(x1[t] - 10*x2[t]) - (1 - u)*x2[t], x2[0] == 0}, {x1, 
    x2}, {t, 0, 1}];
 wynik = 1 - Evaluate[{x1[1] /. s} + {x2[1] /. s}];
 Print[wynik]]

(* {{0.}}
{{0.0253818}}
{{0.0360628}}
{{0.0391236}}
{{0.037966}}
{{0.0343092}}
{{0.0290704}}
{{0.0227663}}
{{0.0157036}}
{{0.00807336}}
{{-2.22045*10^-16} *)

As Nasser said your use of = and == was wrong.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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