Re: [Scilab-users] Avoiding a loop

2017-05-12 Thread Frieder Nikolaisen
Hello Tim, Yes. batt is the is the State of Charge of the battery. P(n,2) (kN) is the power taken rom battery, engine or both. I will calculate the battery in As, so P(n,2) is only an in alternate value - but thats fare to complicated for the example code. "Proving that it's correct will be

Re: [Scilab-users] Avoiding a loop

2017-05-11 Thread Tim Wescott
Depending on how often you switch between battery and generator, and how icky-picky you're willing to be, there may be a way to reduce computation. It looks like the term P(n,2) * (P(n+1,1) - P(n,1)) is always there, and you're either adding it to 'gen' (is it energy production?) or subtracting it

Re: [Scilab-users] Avoiding a loop

2017-05-11 Thread Frieder Nikolaisen
Thanks for all the answers. I feared that there is no way around a loop. During the process batt (Battery) is charged and discharged. In my example, it is only discharged. I will code the entire problem with a loop, maybe somebody knows something to speed up the process with the full problem.

Re: [Scilab-users] Avoiding a loop

2017-05-10 Thread Samuel Gougeon
Le 10/05/2017 à 17:25, Frieder Nikolaisen a écrit : There was a piece of code missing: P = [ 1, 0; 2, 50; 5, 110; 10, 80; 11, 200 15, 0]; batt = 1000; gen = 0; n = 1 for n=1:5 if P(n,2) > 100 then if batt > 800 then batt

Re: [Scilab-users] Avoiding a loop

2017-05-10 Thread Amanda Osvaldo
What it's the equation you need to compute ?Perhaps I can help. I think it's possible to compute with something in this way: map = find (P(:,2) > 100 ); if batt > 800 then batt = batt - P(map,2) * (P(map+1,1) - P(map,1)); end On Wed, 2017-05-10 at 17:23 +0200, Frieder Nikolaisen wrote: > Hello

Re: [Scilab-users] Avoiding a loop

2017-05-10 Thread Tim Wescott
I had a cool solution for you, but then I saw that you're implementing a difference equation, with current values of batt (for instance) depending on prior values. I think that no, you can't eliminate the loop.  If speed is a concern and if you're using the code heavily, you can put it in a C func

Re: [Scilab-users] Avoiding a loop

2017-05-10 Thread Frieder Nikolaisen
There was a piece of code missing: P = [ 1, 0; 2, 50; 5, 110; 10, 80; 11, 200 15, 0]; batt = 1000; gen = 0; n = 1 for n=1:5 if P(n,2) > 100 then if batt > 800 then batt = batt - P(n,2) * (P(n+1,1) - P(n,1)) else gen = gen + P(n,2) * (P(n+1,1) - P(n,1)) end else batt = batt - P(n,2

[Scilab-users] Avoiding a loop

2017-05-10 Thread Frieder Nikolaisen
Hello, I did write an example code, but I do not like the time consuming way I solved the problem. With 50 000 lines in the matrix, it wouldn't be fun. How can I avoid using the for-loop? 10, 80; 11, 200 15, 0]; batt = 1000; gen = 0; n = 1 for n=1:5 if P(n,2) > 100 then if batt > 800