Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]]
share|improve this question
3  
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. – Nasser Mar 18 at 14:14
    
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! – Dr. belisarius Mar 18 at 14:17
    
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. – kirma Mar 19 at 15:17
up vote 7 down vote accepted

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}} *)
share|improve this answer
    
thank you very much. – K.Kozieł Mar 18 at 14:17
    
One more questiion. How can I obtain the maximum value of wynik and which u responds to that? – K.Kozieł Mar 18 at 14:26

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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